In this post, we are going to use Jenkins to build and deploy our node application on production server. Here are the steps we are going to do:

  1. Push our code to Github
  2. As soon as the code is pushed, Jenkins will pull the code, build it and run test cases
  3. Once build is successfully, Jenkins will push built code to production server

I’m using following code for this example:

To achieve above, we need to setup environments. Here they are:

  1. We need to have a production server with Node environment installed
  2. We need to have a Jenkins server with Git, Node and NPM installed
  3. We need to have a GitHub repository to push our code
  4. We need to generate secret key on Jenkins server and provide it to GitHub
  5. We need to configure GitHub repository and branch in Jenkins to pull our code and build.

We are going to run production code and Jenkins server on an Ubuntu machine.

1. Setup Production Node Server

I’m assuming that we already have an Ubuntu machine available for production server. Execute following commands for installing required software:

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
nodejs -v

2. Setup Jenkins Server

I’m assuming that we already have an Ubuntu machine available for Jenkins server. Execute following commands on Ubuntu for installing required software:

wget -q -O - | sudo apt-key add -

sudo sh -c 'echo deb binary/ > \

sudo apt-get update

sudo apt install openjdk-8-jdk
java -version

sudo apt-get install jenkins

sudo systemctl status jenkins
sudo systemctl start jenkins

Jenkins by default runs on port 8080. You may have to allow port 8080 on your server. You can access it using the URL:


3. Setup Jenkins Server private key in GitHub

We need to generate public key on our Jenkins server and add it to GitHub. This allows our Jenkins server to access GitHub repository and pull code. On Jenkins server, do following steps:


Choose a file name or press enter key for default. To get the generated secret key, execute following command:

cat ~/.ssh/file-name  (default is  ~/.ssh/

Now follow these steps:

  • Copy the key displayed
  • Log into your GitHub account and go to your repository
  • Settings -> Deploy keys
  • Add deploy key -> paste the key and give it a title

Next we will configure credentials in Jenkins. Follow these steps:

  • Click Manage Jenkins
  • Click Manage Credentials
  • Click Global Credentials
  • Click Add Credentials
  • Choose SSH Username with private key
  • Select Enter directly
  • Paste the public key, give any username and enter passphrase if you have specified during creating the key.
  • Click OK

4. Create project in Jenkins to build our code

Now we are going to create a new project in Jenkins. Follow these steps:

  • On Jenkins home page, click New Item
  • Select Freestyle project
  • Click Source Code Management
  • Click Git
  • Enter Repository URL
  • Choose Credentials configured earlier if it is a private repository
  • Go to Build step and enter following in command text box:
rm -rf node-jenkins*.tar.gz
npm install
tar -czf node-jenkins.tar.gz node_modules dist

The above commands will do following:

  • Removes any built compressed file if exist
  • Install node dependencies
  • Compiles the code
  • Compresses the required files/folders

In the Post-build Actions, click Add post-build action and select Archive the artifacts. This will allow us to save the generated compresses files for future.
Enter following command:


Click Build Now to build your project.
Click latest build in Build History list to check the status of build.
Console Output will show the output of the build process.
Click the Workspace link to see all your files.

5. Create GitHub webhook for auto-building

What we have done till now allow us to manually build our project by logging into Jenkins.
This can be automated by using GitHub webhook.
For this, we need to make two changes:

1. Add a GitHub build trigger in our project

2. Create a webhook in GitHub repository specifying our Jenkins URL.
Go to your repository -> Settings -> Webhook -> Add Webhook

Note: Adding /github-webhook/ at the end is necessary.

Now, whenever you will push your code on master branch, Jenkins build will run automatically.