Contents


Bluemix fundamentals: Set up team collaboration and automatic deployments for your Node.js app

Introduction to DevOps Services

Comments

Overview

Learn how to create a DevOps Services project where you can store your code in a Git repository and create a Delivery Pipeline that automatically deploys the latest version of your app whenever anyone on your team pushes a change to your Git repository.

This tutorial is part of the "Bluemix fundamentals" tutorial series on developerWorks.

 

What you'll do in this tutorial

Step 1. Download the code

Step 2. Create a DevOps Services project

Step 3. Push code to a Git repository

Step 4. Configure the Delivery Pipeline

Step 5. Trigger the Delivery Pipeline by pushing a change to the Git repository

Step 6. Observe the Delivery Pipeline

Quiz

What's next?

Let's get started

As you learned in the previous tutorial, "Deploy a Node.js application to the cloud," you can run, modify, test, and deploy web apps to the cloud using Node.js and the Cloud Foundry command-line. Now that you know how to deploy an app to Bluemix so that anyone on the Internet can access it, you might be wondering what DevOps support is available to make your development and operations processes easy and efficient.

IBM Bluemix DevOps Services is a software as a service (SaaS) on the cloud that supports continuous delivery. With DevOps Services, you can develop, track, plan, and deploy software in one place.

Knowledge that you need for this tutorial

Software that you need for this tutorial

  • A Bluemix account (register for your free trial account or log in to Bluemix if you already have an account).
  • The following software installed on your computer:
    • A text editor — preferably one with JavaScript syntax highlighting, such as Sublime Text (available in a free trial version) or an open source editor such as Atom.
    • Node.js 0.12.6 or later (Node.js distributions typically install a matching version of npm).
    • Git command line client 2.5.3 or later.

Step 1. Download and unzip the code

To begin this tutorial, you’ll download and unzip the code. The code is very similar to the code from the previous tutorial, with minor changes in the package.json descriptor.

  1. Click this button to download the code for the sample Node.js web app, nodetutorial2.zip:
  2. Unzip the code into a working directory of your choice.

Step 2. Create a DevOps Services Project

In a DevOps Services project, one or more people can build, share, organize, and manage versions of resources to achieve a goal. Typically, that goal is to create an app. In this step, you will create a DevOps Services project that will contain your Git repository and Delivery Pipeline.

  1. Navigate to DevOps Services.
  2. Log in using your IBM ID (the same ID you use for Bluemix) if you are not already authenticated.
  3. On the MY PROJECTS page, click CREATE PROJECT.
  4. On the Create a project page:
    1. Type a name for your project.
    2. Click Create a new repository.
    3. Click Create a Git repo on Bluemix.
    4. Make sure that the "Add features for Scrum development" box is checked.
    5. Make sure that the "Make this a Bluemix Project" box is checked.
    6. Select the Bluemix Region, Organization, and Space where you want your app to be deployed.
    7. Click CREATE.

A DevOps Services project is now created for you. You can explore your project, invite others to work with you on your project, and update your project’s image and description.

Step 3. Push code to a Git repository

Storing code in source control has a variety of benefits including providing a backup of your code, allowing you to roll back to previous versions, enabling you to view a history of your code to understand why changes were made, and allowing you to easily work with teammates on the same code base.

In this tutorial, you will store your code in a Git repository hosted on Bluemix. You will use git command-line client to manage your connection to the Git repository. If you prefer, you can use another tool such as a GUI tool for your operating system.

Get the Git URL for your repo on DevOps Services

  1. On your project's page on DevOps Services, click the gear icon in the upper-right to open the project settings.
  2. Copy the Git URL.

Create a local Git repository

  1. Change directory to the root of the code.
  2. Set up the user name and email that will be associated with your code commits, if you have not yet done so; for example:
    git config --global user.name "Sam Cooke"
    git config --global user.email "scooke@bluemix.net"
  3. Initialize the local git repository:
    git init

    You should see a confirmation message similar to:

    Initialized empty Git repository in <path to your code directory>

Control what files get added to your repository

Next, you'll select which files should be tracked in source control and which files should be ignored.

  1. Create a .gitignore file in your editor.

    Any file or directory you specify will be ignored by version control. This feature allows you to avoid saving large, generated artifacts to the repository.

  2. Add the node_modules directory, which should not be included in the repository, to the .gitingore file.

    You can always regenerate the content of the node_modules directory via npm install; no need to clutter your repository with a large number of dependency library source files.

Commit your code to the local Git repository

Now that you have indicated which files should be tracked in source control, you can commit your changes to your local Git repository.

  1. Add all the project files to the local git repository:
    git add .
  2. Commit the added files to the local git repository with a commit comment:
    git commit –m 'initial commit of code'

    Git will verify this command by listing all the changes you've committed.

Set up a remote repository reference

Your local repository is ready. The next step is to setup a reference to the DevOps remote repository.

  1. Alias origin with the remote DevOps Services Git repository:
    git remote add origin <the DevOps project Git URL>
  2. Verify that you have added the remote repository successfully:
    git remote –v

    You should see a listing of the remote repository reference and its alias.

Push code to the DevOps Services Git repository

Next you'll push your commit to the remote Git repository on DevOps Services.

  1. Push your code to the remote repository and enter your login credentials when prompted:
    git push origin master

    This step pushes all the committed changes on the master branch from the local repository to origin, the alias for your Git repository on DevOps Services, and writes to the DevOps Services Git repository.

  2. Verify that your local repository is in sync with the DevOps Cit repository:
    git status

    The status report should show that your local repository is in sync, with nothing new to commit to the DevOps repository.

    Your project code is now stored in the Git repository of your DevOps Services project. Navigate to DevOps Services and select your project to see a read-only view of the code.

Step 4. Configure the Delivery Pipeline

One of the keys to a successful DevOps approach is having automated builds and deployments. Luckily, DevOps Services has the Delivery Pipeline service you can configure to build, test, and deploy your app to Bluemix through either an automatic or manual trigger.

In Bluemix, organizations and spaces are organizational units in the Cloud Foundry infrastructure that you can use to store and track application resources. An organization contains domains, spaces, and users; a space contains applications and services.

By default, a dev (development) space is created for you when you log in to Bluemix for the first time. For this tutorial, think of the dev space as representing a place for testing code live in a production-quality environment without putting it into actual production. (Some might call this a quality-assurance or test space.) In this step, you’ll create a new space named prod to represent your production space: the place where you keep apps that your end users will use.

You'll also configure the Delivery Pipeline service to build and deploy your app to both your dev space and prod space in Bluemix whenever you or someone on your team pushes code to your project’s Git repository.

Create a build stage that will build your app

  1. On your project's page on DevOps Services, click BUILD & DEPLOY in the upper-right corner.
  2. Click ADD STAGE.
  3. Click MyStage at the top of the Stage Configuration dialog box and name your stage by typing Build Stage.
  4. Notice that the Input Type is set to "SCM Repository" by default. This setting means that the input for the stage will be your Git repository.
  5. Notice that "Run jobs whenever a change is pushed to Git" is selected by default. This selection means that whenever you or someone else on your team pushes code to your project's repository, the build stage will automatically be triggered.
  6. Click JOBS to open the Jobs section of the Stage Configuration dialog box.
  7. Click ADD JOB and select Build.
  8. In the Builder Type drop-down list, select npm, which tells DevOps Services to use the npm install as the Build Shell Command to execute the job. npm install fetches all the dependencies of the app and places them into the node_modules directory.

    Note that you'll leave the Working Directory and Archive Directory fields empty, as his build will use the root directory. Likewise, you'll leave Enable Test Report unchecked for now; we'll cover test jobs in the next section.

  9. Notice also that the "Stop stage execution on job failure" check box is selected by default. This selection means that if a failure occurs, any jobs further down the pipeline will not run, and the whole stage will be marked as failed.
  10. Click SAVE.

Create a test job within the Build stage that will run your unit tests on Bluemix

  1. Click the gear icon located at the top-right of your Build Stage tile, and select Configure Stage from the drop-down list.
  2. Click JOBS to open the Jobs section of the Stage Configuration dialog box.
  3. Click ADD JOB and select Test.
  4. In the Tester Type drop-down list, select Simple, which will allow you to run your unit tests with Mocha and then display the results file.
  5. In the Test Command box, add the following commands to install all the Node.js dependencies, like Mocha and the xunit-file plugin, and then run the unit tests:
    npm installnpm test

    Leave Working directory empty to use the root directory.

  6. Make sure Enable Test Report is checked.
  7. For Test Result File Pattern, enter xunit.xml
  8. Notice that the "Stop stage execution on job failure" check box is selected by default. This selection means that if a failure occurs, any jobs further down the pipeline will not run, and the job will be marked as failed.
  9. Click SAVE.

Create a deploy stage that will deploy your app to your dev space in Bluemix

  1. Click ADD STAGE.
  2. Click MyStage at the top of the Stage Configuration dialog box, and name your stage by typing Dev Deployment.
  3. Notice that the Input Type is set to "Build Artifacts." This setting means that build artifacts created in the previous stage will be the input to this stage.
  4. Notice that "Run jobs when the previous stage is completed" is selected by default. This setting configures your pipeline so that whenever someone pushes code to your project's repository, the build step will run, and, if it succeeds, a deployment to the development environment will be kicked off.
  5. Click JOBS to open the Jobs section of the Stage Configuration dialog box.
  6. Click ADD JOB and select Deploy.
  7. Change the Application Name to a name that is unique to your Bluemix dev space.
  8. You want your app to deploy to a consistent URL where developers on your team can test their code, so in the Deploy Script section, after cf push "${CF_APP}", add:
    -n whateverYouWantYourUrlToBe

    Note: The URL must be unique across all Bluemix apps, so be creative.

  9. Click SAVE

Create a prod stage that will deploy your app to your prod space in Bluemix

  1. Click ADD STAGE.
  2. Click MyStage at the top of the Stage Configuration dialog box, and name your stage by typing Prod Deployment.
  3. Notice that the Input Type is set to "Build Artifacts." This setting means that build artifacts created in the Build Stage will be the input to this stage.
  4. Notice that "Run jobs when the previous stage is completed" is selected by default. This setting configures your pipeline so that if the Dev Deployment stage succeeds, this stage will run.
  5. Click JOBS to open the Jobs section of the Stage Configuration dialog box.
  6. Click ADD JOB and select Deploy.
  7. In the Space field, select Create a new space.
  8. In the Enter space name field, type prod, and click CREATE.
  9. Change the Application Name to a name that is unique to your Bluemix prod space.
  10. You want your app to deploy to a consistent URL where end users can access your app, so in the Deploy Script section, after cf push "${CF_APP}", add:
    -n whateverYouWantYourUrlToBe

    Note: Remember that the URL must be unique across all Bluemix apps, so be creative.

  11. Click SAVE.

You now have a Delivery Pipeline that will build your app (including running Mocha unit tests), deploy your app to your dev space, and then deploy your app to your prod space.

Step 5. Trigger the Delivery Pipeline by pushing a change to the Git repository

Now that you’ve configured the Delivery Pipeline, it’s time to try it out! One way is to manually trigger the pipeline by clicking the play button in the Build Stage. Another option is to trigger the pipeline by pushing a change to the Git repository, which is what you’ll do in this step.

Make a change to the code

The first thing you need to do is make a change to a file.

  1. Open the <your code directory>/views/australia.jade file in your text editor.
  2. Scroll down the editor until you see:
    <div id="price">120.00</div>
  3. Change the price from 120.00 to 130.00 (demand just went up!).
  4. Save the file.

Commit your code to the local Git repository

Now that you have changed a file, you can commit your change to your local Git repository and push it to the remote Git repository hosted at DevOps Services.

  1. Change directory to the root of the code and add all the changes to be committed:
    git add .
  2. Commit the changes with a comment:
    git commit –m 'increased price of print'
  3. Push the changes to the DevOps Git repository:
    git push origin master

    Remember, you will be prompted to login before writing to your DevOps repository.

Step 6. Observe the Delivery Pipeline

The change you pushed to the Git repository hosted at DevOps Services in the previous step automatically triggered the Delivery Pipeline. In this step, you’ll explore the results of the pipeline. You can view some of the logs and results while the pipeline is still running. The instructions below assume the pipeline has completed, but feel free to explore the pipeline while it’s running.

  1. On your project’s page on DevOps Services, click BUILD & DEPLOY in the upper-right corner.
  2. In the Build Stage tile, observe that the last Build job succeeded. If you switch to this "Pipeline : All Stages" screen while the build is running, you will see the status changes in real time. Click View logs and history.

    On the LOGS tab, you can see the output of the build job. In this case, you can see the dependencies being added to the node_modules directory.

  3. Click Back to the Pipeline and notice in the Build Stage tile that the Test job also succeeded. Click on the Test job.

    You can see the output of this test job on the LOGS tab.

  4. Click the TESTS tab and notice that two tests passed.
  5. Click Mocha Tests to see the list of tests included in the file.
  6. Click Back to Pipeline at the top of the page.
  7. In the Dev Deployment tile, observe that the Deploy succeeded. Click View logs and history.

    On the LOGS tab, you can see the output of the deployment.

  8. Click Back to Pipeline at the top of the page.
  9. In the Dev Deployment tile, notice that you can click the URL for your running app to view it. You can also view the runtime log, which is helpful when debugging issues.
  10. Explore the Prod Deployment's logs and running app.

Quiz

Test your knowledge of what you've learned in this tutorial. Answers are below.

  1. The IBM Bluemix DevOps Services supports which type of version control repository:
    1. Git
    2. Subversion
    3. Mercurial
    4. CVS
    5. Bazaar
  2. How do you trigger a build using the DevOps Services pipeline?
    1. Manually from the DevOps Services dashboard
    2. Whenever someone pushes code the DevOps Git repository
    3. Based on a timer that can be configured
    4. Both b and c
    5. Both a and b
  3. To control files or directories that get excluded when pushing code to a Git repository, you use the following file:
    1. manifest.yml
    2. config.ignore
    3. .gitignore
    4. git.ignoore
    5. my.cnf
  4. Which of the following command is used to create a local copy of a DevOps Git repository?
    1. git init
    2. git clone
    3. git status
    4. git push
    5. git config
  5. The Build stage, Dev Deployment, and the Production Deployment stages are all stages in a DevOps Service ________?
    1. assembly line
    2. delivery pipeline
    3. lifeline delivery
    4. job
    5. repository

Answers to quiz

1. a, 2. e, 3. c, 4. b, 5. b

What's next?

In this tutorial, you learned how to leverage DevOps Services to:

  • Create a project on DevOps Services that hosts a Git repository
  • Push code to the remote Git repository
  • Create a Delivery Pipeline that builds your app, runs unit tests, and deploys the app to multiple stages whenever you or someone on your team pushes code to your project’s repository

Now, continue learning "Bluemix fundamentals" on developerWorks with this next tutorial. You'll see how to enhance and add functionality to your app via cloud services:

More Bluemix resources

For a steady stream of tutorials and other technical resources to help you learn, develop, and connect with Bluemix, see the Bluemix page on developerWorks.


Downloadable resources


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Cloud computing, Java development, Web development
ArticleID=1016384
ArticleTitle=Bluemix fundamentals: Set up team collaboration and automatic deployments for your Node.js app
publish-date=10052015