Jenkins & node.js | Part 2

Once Jenkins is up and running, we are ready to configure it for our first free style node.js project. Jenkins installation guide in Part 1.

Before we begin integrating your node.js project, we need to install two plugins:

  1. Node.js plugin, for node.js installations under jenkins user.
  2. BitBucket plugin, for integrating with bitbucket.

Let’s start:

  1. From the dashboard select create new item.
  2. Enter the name of the item and select freestyle project for now.
creating a freestyle project

3. Once selected, item’s configuration dashboard is launched. Where you can configure various parameters as per you requirements. For now follow the basic configurations.

4. Here we are using GIT as a SCM tool, thus linking with a private repo hosted in bitbucket, under Source Code Management Section. Add the repository url and create new username/password credentials under add button.

5. Next, Jenkins should automatically build the node.js project ever time one pushes the code to bitbucket. Select Build when a change is pushed to Bitbucket.

6. Next, select the node.js version require to run your node.js project:

Note: Before this you have to add new node.js installations in your jenkins global tool configuration section.

You can install as many node installations as you need and select the respective when you setup your item.

Once, installed node.js installations will show up in Build Environment.

7. Lastly, under BUILD. We need to execute a shell commands to restart our node.js project automatically every time new build is deployed. Here we are using forever to monitor and start node.js server.

It’s a very basic shell commands, you can write them in a .sh script file and execute it below also, just don’t forget to add `#!/bin/sh` on top:

All the lines are executed sequentially, most important things to understand is:

BUILD_ID=dontKillMe forever -a -l myforever.log index.js &

Here we tell Jenkins not the stop the newly forked child process as it’s execution is over. Using `BUILD_ID=dontKillMe` flag. As this process needs to be up and running even are the build is over. Do note & at the end, it used to make the command a background process, thus jenkins process will not wait for it and skip to next command, thus allow the build to be complete.

Few things to note for using forever and starting node.js server using this method. The forever process/node.js server is started under Jenkins user in your ubuntu server. Thus to access the list of forever processes you need to switch to jenkins user.

sudo su -s /bin/bash jenkins
forever list

Even the logs are created under jenkins user under repective forever directory.

8. The only remaining setup is to hook your bitbucket repository to jenkins.

Note: url is your jenkins url and path: /bitbucket_hook

Now, all is setup, simple push a new commit to the remote repository and see the magic happen.

Thanks for reading, if you like it please share!

Show your support

Clapping shows how much you appreciated Nivesh’s story.