Skip to content
Amol Dighe
Go back

Jenkins - Deploying App

Edit page

Jenkins is the most widely used build-n-deploy tool used in Devops arena. In past, I have used Jenkins for infrastructure code deployments, where we were deploying compute nodes in openstack infrastructure. If I have to sum up the high level build & deploy stages, we were getting the openstack packages from one of our local repo, saltstack is called to build the configuration file for the compute nodes, the packages are deployed with the necessary configuration and the compute node is rebooted. Sounds simple, but it’s actualy quite complex considering the jenkins configuration and various build scripts playing their part for the deployment. Recently while working with the application team, I came across a deployment requirement which was quite intresting.

Requirement

Solution

We will be using our Jenkins server for syncing the code from github repository to the node js server which ia a VPS instance on AWS.

First we need to prepare the Jenkins server to get the code from github using the SCM (Source Code Management) option.

Add the repository under SCM along with an existing or new credential key.

Add the public key of the keypair as deploy key to repository. Click here to know more about deploy key

While creating the Jenkins job, we need to specify that, it runs on Master, by selecting “Restrict where this project can be run” under General option and select the “Label Expression” as “master”

Under Source Code Management, add the git repository URL and select the key as credential. Jenkins will immediatly try to talk to the git repository and check if it can connect.

We are going to use ‘Poll SCM’ and select the Schedule to sync and build the repo every hour

H * * * *

Under “Build Environment” we will be using Binding option to assign a variable (SSHKEY) to our credential key file. This is the credential key file which will be used to talk to the node js server instance. The credential is added using Jenkins control panel using “Jenkins >> Credential >> Add credentials”

rsync -avhpzi -e "ssh -i ${SSHKEY}" --delete --exclude ".git" --exclude "logs" --exclude ".session" ./ "user@node-vps-instance:/home/ubuntu/ReportScript/"

ssh -i "${SSHKEY}" user@node-vps-instance "screen -X -S reportscript quit"

ssh -i "${SSHKEY}" user@node-vps-instance "screen -S reportscript -d -m bash -c 'cd /home/user/nodedata; sudo NODE_ENV=prod forever bin/www'"

Execution

Lets see what happens when we execute the Jenkins job

rsync will use the SSHKEY and copy changes in code from the workspace directory on Jenkins master to the mentioned path on node js server

second shell command is to kill if there is any existing screen session on the node js server

thrid shell command will set the node js environment start a forever session to execute the node js code under a screen session.

Please note the screen session will be named and started in detached mode.


Edit page
Share this post:

Previous Post
Elasticsearch Cheatsheet
Next Post
Moving Proxmox-LXC container