Create a Continuous Deployment Pipeline with Node.js and Jenkins
Learn how to plug in and configure Node.js in a continuous deployment pipeline, using Jenkins on a Couchbase server, in this tutorial.
Join the DZone community and get the full member experience.
Join For Freepreviously i had written about using jenkins for continuous deployment of java applications , inspired by a keynote demonstration that i had developed for couchbase connect 2016 . i understand that java isn’t the only popular development technology that exists right now. node.js is a very popular technology and a perfect candidate to be plugged into a continuous deployment pipeline using jenkins.
we’re going to see how to continuously deploy a node.js application with jenkins based on changes made to a github repository. so let’s figure out the plan here. we’re going to be using an already existing node.js repository that i had uploaded to github a while back. when changes are made to this repository, jenkins will build the application and deploy or run the application. because of the nature of node.js, the build process will consist of making sure the npm modules are present.
the requirements
there are a few software requirements that must be met in order to be successful with this guide. they are as follows:
- node.js 6+
- jdk 8+
- jenkins 2.32+
- couchbase server 4.6+
since this is a node.js pipeline, of course, we’ll need it installed. however, since jenkins is a java application, we’ll also need java installed. my sample application does use couchbase, but that won’t be the focus of this guide. however, if you’re using the same application i am, you’ll need couchbase server installed. all software listed should reside on the same host. in a production environment, you will probably want them dispersed across multiple machines.
installing and configuring couchbase server as the nosql database
at this point, you should have already downloaded couchbase server. after installing it and configuring it, you’ll need to create a bucket called
restful-sample
and that bucket should have a primary index. for instructions on configuring couchbase and getting this bucket created, check out a
previous tutorial
i wrote on the subject. it is actually the tutorial that went with creating this couchbase, express framework, angular, and node.js (cean) stack application. with couchbase ready to go, we can focus on configuring jenkins and creating our workflow.
configuring jenkins with the necessary plugins
you should have already downloaded jenkins by now. if you haven’t, go ahead and obtain the war file from the jenkins website. to start jenkins, execute the following command from your command prompt or terminal:
java -jar jenkins.war -httpport=8080
this will make jenkins accessible from a web browser at
http://localhost:8080
. upon first launch, you’ll be placed in a configuration wizard.
the first screen in this configuration wizard will ask you for the password that jenkins generates. find it in the location presented on the screen. the second screen will ask you which plugins you’d like to install.
for now, we’re going to install the suggested plugins. we’ll be installing extra plugins later. the third screen will ask us to create our first administrative user. technically, the generated password you’re using is an administrative user, but you may want to create a new one.
after you create a user, jenkins is ready to go. however, we are going to need another plugin and it can vary depending on how we wish to build and deploy the node.js application. from the main jenkins screen, choose "manage jenkins" to see a list of administration options.
what we care about is managing the available plugins. after choosing "manage plugins" we want to search for and install a plugin by the name of post-build script.
this plugin allows us to execute shell commands or scripts after the build stage has completed. in this example we’ll be building and deploying to the same host, we can run everything locally via shell commands. in a production environment, you might want to use the ssh plugin to migrate the code to a remote server and run it there. with the plugins available, let’s create our continuous deployment workflow for node.js in jenkins.
creating a jenkins continuous deployment workflow for node.js
just to reiterate, our goal here is to create a workflow that will pull a project from github, build it by installing all the dependencies, and deploy it by running it on a server, in this case, our local machine. start by creating a new item, otherwise known as a new job or workflow.
we’re going to be creating a freestyle project, but you can give it any name you want. there are three things that need to be done on the next screen. the source of our workspace will come from github. in your own project it can come from elsewhere, but for this one, we need to define our source control information.
the github project is one that i had previously created and written about, like mentioned before. the project can be found at:
https://github.com/couchbaselabs/restful-angularjs-nodejs
now in a production environment, you’ll probably want to set up github hooks to trigger the job process, but since this is all on localhost, github won’t allow it. instead, we’ll be triggering the job manually.
after configuring the source control section we’ll need to configure the build step. for node.js, building only consists of installing dependencies, but you could easily have unit testing and other testing in this step as well. in my previous java example , the build step had a little more to it. in this node.js example we have the following:
npm install
finally, we get to define what happens after the project is built.
in this example, we will be deploying the application locally on our machine. probably not the case in your production scenario. so you’ll notice in our post-build step we have the following commands:
npm stop
npm start
before starting the application we are stopping any already running instance of it. once stopped we can start the new version. however, where do these
stop
and
start
tasks come from?
"scripts": {
"start": "forever start app.js",
"stop": "forever stopall"
}
the above was taken from the github project’s package.json file. each task starts and stops a
forever
script for node.js. go ahead and try to run the job choosing build now from the list of options. it should obtain the project, install the dependencies, and make the project available at
http://localhost:3000
. just make sure couchbase server is running for this project- otherwise, you’ll get errors.
you just saw how to use jenkins to continuously deploy your node.js applications based on changes that have been made in github. a similar version of this guide was created for java applications called, create a continuous deployment pipeline with jenkins and java , which is worth reviewing if you’re a java developer.
Published at DZone with permission of Nic Raboy, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments