Continuous Integration Setup with Jenkins for a Node.js Project – Part 2

In my previous post, we went over how to install Jenkins on your own server.  Now, we’ll set up all the basic configurations necessary in Jenkins and integrating it with GitHub.

Basic configurations in Jenkins

At this point, anybody who’s anybody can do things in our Jenkins server.  So let’s set up security in Jenkins.  We’re going to use a project-based matrix security which will allow us to see users and their permissions in a matrix format.

From the Jenkins dashboard:

  1. Click “Manage Jenkins”
  2. Click “Setup security” button on the top right (or alternatively, click on “Configure Global Security”).

In Configure Global Security page:

  1. Check “Enable security”
  2. Scroll to “Security Realm,” select “Jenkins’ own user database” and check “Allow users to sign up.”
  3. Click “Save” button

Next, we’ll set up an administrative account.

  1. Click “Sign up” in the top-right corner of the screen.
  2. Fill out the form (username, password, confirm password, full name, e-mail address)
  3. Click “Submit” button

Go back to the “Configure Global Security” page:

  1. Scroll to “Jenkins’ own user database,” un-check “Allow users to sign up.”
  2. Click “Save” button.  That means the “Sign up” link in the top-right corner of the screen will no longer show.
  3. Still on the same page, scroll to “Authorization” select “Project-based Matrix Authorization Strategy.”  A matrix diagram will appear.
  4. Fill out your username in “User/group to add” input box and click “Add”
  5. Your username will appear in the matrix.  Next to your username, check “Administer.”
  6. Click “Save” button.  You are now the admin!

Installing GitHub Plugin

In order to integrate with GitHub, we need to install the plugin.

  1. Click “Manage Jenkins” on the menu on the left.
  2. In the “Manage Jenkins” page, click “Manage Plugins.”
  3. In “Manage Plugins” page, click on the “Available” tab.
  4. Search for the “GitHub plugin” and checkmark it as it appears in the search results.  Click “Install without restart” and checkmark “Restart Jenkins when installation is complete and no jobs are running” if it requires a Jenkins restart.

Integrating with GitHub

  1. Click “Manage Jenkins”
  2. Click “Configure System”
  3. Scroll to “Jenkins location,” ensure that it has a public facing URL so that GitHub can push events to it.
  4. Click “Save” button.

Next, go to your account in GitHub.  You will need to generate a token to use with the GitHub API.

  1. Go to “Settings”
  2. Go to “Personal access tokens”
  3. Click “Generate new token”
  4. Token description can be something like “Jenkins”
  5. Ensure that repo, public_repo, gist, and user are all checked.  The rest are un-checked.
  6. Copy the generated token.

Go back to your Jenkins dashboard.

  1. Click “Configure System”
  2. Scroll to “GitHub Web Hook”
  3. Select “Let Jenkins auto-manage hook URLs
  4. Under “GitHub credentials,” fill out Username and OAuth token (the token you just copied from GitHub) and click “Test credential.”
  5. Click “Save” button

GitHub web hooks should work now.  More setup is needed for pulling from private repositories.

  1. Click “Credentials” in side menu
  2. Click “Global Credentials”
  3. Click “Add Credentials”
  4. Fill out the form for Username, Password, Description
    1. Kind: username with password
    2. Scope – Global
    3. Username – GitHub username
    4. Password – use your token
    5. Description – You can call it “<GitHub username> (GitHub)” and replace <GitHub username> with your GitHub username
  5. Click OK button

Congratulations, GitHub has been integrated into Jenkins!  In my next post, we’ll learn how to set up our first Node.js project in Jenkins.

Continuous Integration Setup with Jenkins for a Node.js Project – Part 1

For continuous integration for Node.js projects, there’s Travis and there’s Wercker.  But sometimes, you need to have your continuous integration tool hosted on your own server, and that is where Jenkins comes at an advantage.  It’s been around way before Travis and Wercker, and some of you may remember it as Hudson.  Also, Jenkins is free for both public and private repos.  Furthermore, it has tons of plugins such as unit testing, code coverage, Javascript linting/hinting, etc etc.  On the downside, Jenkins is the hardest to set up among the three, and that is why I wrote this post.  We’ll learn how to set up continuous integration with Jenkins for a Node.js project.  In the first part of this series, we will install Jenkins on our own server, whether it be on Heroku or your own web host or what have you.

There are 3 files to place on your server – jenkins.war, pom.xml, and Procfile, each of them being on the same directory level on your server.  The jenkins.war can be downloaded from the Jenkins site at https://jenkins-ci.org/.  The pom.xml is very basic and has what the server needs to run the war.  The Procfile contains the necessary commands to run the jenkins.war.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.example</groupId>
 <version>1.0-SNAPSHOT</version>
 <artifactId>example-ci</artifactId>
</project>

Procfile

# Only listen on http; disable ajp and https
web: java -jar jenkins.war --httpPort=$PORT --ajp13Port=-1 --httpsPort=-1

The files can be found at – https://github.com/melkybee/jenkins-ci.

Of course, this is just one way to install Jenkins on your server.  Alternate ways to install it on your server (i.e., Ubuntu/Debian) can be found at the Jenkins website, https://jenkins-ci.org/.

At this point, Jenkins should be installed and running on your server.  You’ll see the default Jenkins dashboard with the mustache guy at the top left.  In part 2, we’ll set up all the basic configurations necessary in Jenkins and installing and integrating with GitHub.