Ok, so we have our app running with the test. Let’s push it to Github.

First add all files.

git add .

Add new commit.

git commit -m "first push"

Push it.

git push

Adding Github webhook to push events to Jenkins

When any changes i.e commits are pushed to Github repository, we need a mechanism to notify that event to our Jenkins Server which we going to configure in next section.

Github allows us to add Webhook services to achieve same. Open GitHub repository, go to settings and click “Webhooks and Services” from side panel. Click on “Add Services” and search Jenkins, click on “Jenkins Github Plugin”.

Note: Don’t choose Git one.

After that, add the webhook URL which is in the following format.

http://yourdomain.com-OR-IP/github-webhook

Click on Add Service and you are good.

Creating droplet and Installing Jenkins

I am using DigitalOcean as Web Hosting. You can use any Server as you like, in case you want to host your app on DigitalOcean and looking forward to buying it, you can use this link to get $10 discount on our side.

After installation, Jenkins will automatically start itself and you can visit the domain-name:8080 to access it.

We have installed Jenkins, let’s add Github plugin in Jenkins in order to integrate the project.

Adding Github plugin in Jenkins

Click on “Manage Jenkins” and then click on “Manage Plugins”.

Now search for “GitHub” from search bar placed at right side of the screen and choose the Github plugin. There are much more with similar names but choose the one shown below.

Click on “Install without restart” and in few seconds plugin will be installed.

Configuring project with Jenkins

We have our project ready and hosted at Github and Jenkins installed, all we need now is to integrate both of them so that when we push any changes to Github, Jenkins know about it and do the testing and deployment.

Click on “New Item” and fill in the proper project name. Choose “Freestyle project” among the options.

In that for instance type “npm install” and then click on Save. Here is full-page screenshot.

Let’s check if we have Github and Jenkins talking to each other.

Make any changes in the project and do the commit and push. As soon as you push new changes Jenkins will show you new build in the project dashboard.

You can replace the command with mocha in order to do testing first.

Configuring deployment process

We have Github communicating to Jenkins, it’s time to perform the deployment to our Server. One thing, Jenkins will not directly communicate to your web server. All it can do is to run instruction which you want it to run.

So let’s first list down the steps we manually perform to do the deployment to any Server.

Login to Server using SSH.

Switching to project directory.

Pulling code from Github.

Restarting process manager ( say pm2 ).

We will write down all of these commands in one file and tell Jenkins to execute it.

But, what about SSH username and password? Of course, Jenkins won’t enter it as we do. We need to make sure Jenkins can perform SSH login without using the password to our Server.

To achieve that, we will generate SSH key manually from Jenkins Server and add it into our Development Server.

Let’s do it.

Login to Jenkins Server using Terminal and switch to root user using this command.

sudo su

Jenkins automatically creates a new user after installation. Switch to it using this command.

su jenkins

Let’s generate RSA key, run following command.

ssh-keygen -t rsa

Press Enter for the location and do not type any password when it asks to, just hit enter.

Once the process is completed, print the public key information using this command.

Save the file. Each line of the code in this file is what you actually do manually, I don’t think so I need to explain this.

Make the file executable using the following command. This is important for Shell script to execute.

chmod +x deploy

Go to Jenkins project page, from left navigation click on “Configure” and scroll down to “Build” section.

Add “./deploy” in the build commands. Here is screenshot for same.

Click on Save.

Now in the project directory, add the deploy file, commit and push it to Github.

Check the second build in Jenkins.

Refresh your project page and you shall see the changes updated in the project.

It Works. We have automated the process to do the testing and deployment every time we make some changes in the project.

Tips for integrating Github private repository or organization with Jenkins

Setting up Jenkins for the private repository is similar to what we did above. I personally configured the continuous integration and deployment environment for my organization using the same steps I mentioned above except some little extra efforts due to Server configuration ( This is my guess ).

When Jenkins was trying to pull the code, it was throwing an error due to HTTPS method used by Github. I stumbled upon various methods and nothing worked for me. I changed the HTTPS origin to SSH origin and updated the repository origin too in Development server.

I also configured my account to use the SSH key in order to perform the Git operation without authentication and that you may need in order to let Jenkins do the code update.

Here is what I did.

Go to this official doc of Github to generate and add the SSH key in Github. You need to generate SSH key in your development Server and add the Public key in Github.

Conclusion

Continuous integration and deployment using Jenkins are super easy and this approach would save a lot of time of developers. They need to just focus on writing better code and Jenkins will do the rest boring stuff.

Thanks for the great article. After reading many other articles, now I seems to get the understanding. I would like to get few advice on process that I would like to create in Jenkins.
– With Jenkin Build, I would like to create tars/zip of the code for specific environments. Each build tars need to have a specific name.
– When QA team requires, they can select one of the successful build to be deployed to a specific environment (QA/STG).

For second process I use the promoted builds plugin https://wiki.jenkins-ci.org/display/JENKINS/Promoted+Builds+Plugin. Our build server generates a set of build artifacts (in this case zip files) and the promoted plugin then controls the promotion step of the build. In this case you can have the a step to promote build to QA or STG. I have promotions of DevDeployed, ReadyForQA, QADeployed, PassedQA, and ReleaseCandidate and they all have dependencies to ensure that each promotion is followed. So once build promoted to ReleaseCandidate that is the build that is then deployed to Production with no rebuild.

I found that the mistake is to start the app with “node app.js” as you do in the tutorial after the express code, the correct way is like your solution, starting it with pm2 start app.js, other way it will tell there’s no pm2 process.

This is my Console Output in Jenkins after run the Task, I set in the Shell Script of the Task:
SAMPLE unit test
1) should return home page
double callback!
0 passing (42ms)
1 failing
1) SAMPLE unit test should return home page:
Uncaught TypeError: Cannot read property ‘status’ of null

The error is pointing me in to this line :
res.status.should.equal(200);

Okay I figured it out! I realize this is old, but someone might fight it. I realized after hating my life whenever I have to do devops that I should just always write down everything I do until it works and then every time I do the thing again modify the steps until I fully understand.

What was wrong in my case was that I had to do

sudo su; su jenkins

to become the jenkins user and then connect to the new server as jenkins

Hello i have one problem i added one nodejs application to jenkins from git repository but if i m trying to build that app server started but the build doesn’t stop can you please help me out how to resolve this issue..?

Hi, really good, it helped me a lot!
I’ve got a question: Is it possible to package a nodeJs app in a RPM file and then to use jenkins to deploy it?
I’ve take this idea from nebula-ospackage project.
Thanks in advance!