We’re going to see how you can easily get started with Jenkins and Node.js can be made to work together to enable continuous deployment 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 React JS repository from our popular “React From Scratch” workshop. When changes are made to this repository, Jenkins could build the application and deploy or run the application. Because of the nature of Node.js, the build process will ensure the NPM modules are present.
The following software requirements must be met in order to use this guide:
Since this is a Node.js with Jenkins pipeline, we’ll need the latest version of Node.js installed. Since Jenkins is a Java application, we’ll also need Java installed. My sample application does not use Couchbase, this will ensure anyone can easily use this guide.
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 the 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 to 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 PostBuildScript.
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. 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. You can also select the branch if your project is not using the master branch.
The GitHub project is one that I had previously created and written about, like mentioned before. The project can be found at:
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 or other tests in this step. In the Jenkins Java project, the build step has more to it. In this Node.js example we just have the following:
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:
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.
Want information on using Node.js with Couchbase NoSQL? Check out the Couchbase Developer Portal for documentation and examples.