View on GitHub

jenkins-getting-started

Tutorial for getting started with Princeton University's Jenkins Server

Table of Contents

  1. Automatic Testing with Jenkins
  2. Your Internet Connection
  3. Credentials for the Agent Node, GitHub and Jenkins
    1. GitHub Setup
      1. Clone the Tutorial Repository on GitHub
      2. Add buildbot-princeton as a collaborator
      3. Generate the ssh public and private keys
      4. Copy the public key to your GitHub account
      5. Copy the private key on your Jenkins account
    2. Agent Node Configuration
  4. Setting Up A Build in Jenkins
    1. Creating a Simple Build with Manual Trigger
      1. Project Identity And Build Rotation
      2. Agent Node
      3. Source Code Management (Git)
      4. Add a test
      5. Add e-mail notifications
    2. Automatically Trigger A Build When Someone Opens a Pull-Request
      1. Changes on the Jenkins Server
      2. Changes on GitHub
      3. Open a Pull-Request To Test the New Settings

Automatic Testing with Jenkins

Princeton University’s Jenkins server is available to all campus members, but before you can use the server you need to request an account by sending an email to cses@princeton.edu with the subject line: Jenkins account request.

The system for automatic testing involves three components:

  1. the source code, it usually comes from a Source Control Management (SCM) system, which is either git or subversion.
  2. the computer where the tests are run, this is called the agent node.
  3. the Continuous Integration server, which is the Jenkins server.

A typical sequence of events for a build is:

  1. a code change is pushed by a developer to the SCM repository.
  2. the SCM system sends a message to the Jenkins server saying that something has changed.
  3. Jenkins starts running a test, this is called a build by:
    1. fetching the changes from the SCM repository.
    2. running the tests on the agent nodes
    3. notifying the developers of the results of the tests.

The goal of this tutorial is to demonstrate how to configure the different components to set up a test. We will use a Python code, available on GitHub, to define some simple tests. So the SCM system for this tutorial is git on GitHub.

First we will check that you can connect to the Jenkins server in Section 2. Then in Section 3 we will configure the authentication between the three components of the testing system—GitHub, the agent nodes and the Jenkins server—so they can communicate with each other. In Section 4 we go through the steps to configure a build.

Throughout this tutorial, the figures are scaled to fit on the current screen, if you need a larger figure, you can usually right click on it and choose Open Image in New Tab or Open Image in New Window.

Your Internet Connection

For security reasons, the Jenkins server is only accessible when your computer is connected to the campus network or when using a VPN like GlobalProtect.

Credentials for the Agent Node, GitHub and Jenkins

GitHub Setup

Clone the Tutorial Repository on GitHub

To follow this tutorial, you need an account on GitHub. You then need to fork the repository for the code used in this tutorial on your GitHub account. To do so:

Add buildbot-princeton as a collaborator

You need to add the GitHub user “buildbot-princeton”, which is the GitHub id for Jenkins at Princeton, as a collaborator to your fork. This explains how to do it: Adding collaborators to a personal repository

It is recommended that you give buildbot-princeton push (or write) access. We will talk about why later.

Your Collaborators and page on GitHub should look like this:

img

Generate the ssh public and private keys

Now we are going to give the Jenkins server access to this GitHub repository using an ssh public/private key pair.

For more help on generating ssh keys, check the GitHub documentation.

Copy the public key to your GitHub account

GitHub also has some help on this subject: GitHub help on ssh deploy key.

Here is a summary:

Copy the private key on your Jenkins account

Now we need to copy the private part of the key to your Jenkins account.

img

Agent Node Configuration

Now we are going to give Jenkins access to the agent node. The name of the agent node is agent.

The Jenkins system administrator will send you a file containing an ssh public key named id_rsa_agent_punetid.pub that you need to copy on your account on agent. You can do it with the following steps:

  1. copy the ssh public key on agent

    $ scp id_rsa_agent_punetid.pub punetid@agent.princeton.edu:~/.
    
  2. connect onto the agent host

    $ ssh punetid@agent.princeton.edu
    
  3. make sure the directory ~/.ssh exists and that the permissions are correct:

    [punetid@agent] $ mkdir -p ~/.ssh
    [punetid@agent] $ chmod 700 ~/.ssh
    
  4. Append the public key to the file ~/.ssh/authorized_keys and make sure the permissions are correct:

    [punetid@agent] $ cat id_rsa_agent_punetid.pub >> ~/.ssh/authorized_keys
    [punetid@agent] $ chmod 600 ~/.ssh/authorized_keys
    
  5. Create a directory called jenkins in your home directory

    [punetid@agent] $ mkdir ~/jenkins
    

Setting Up A Build in Jenkins

Now that we have set up all the necessary permissions between GitHub, Jenkins and the agent node, we are ready to configure some tests for the simple factorial code that is in your fork of the PrincetonUniversity/jenkinstutorial repository.

When you save your configure, you will see a note pop-up like this one:

img

This is because the Jenkins configuration is saved in a Git repository itself. This way if you ever make a mistake in your configuration, and you cannot remember what you changed, you can ask the Jenkins administrator to revert to a previous configuration. In the pop-up menu, you can either enter a comment for this commit or leave it blank. Then click on Submit comment.

Creating a Simple Build with Manual Trigger

img

Project Identity And Build Rotation

First, to save disk space on the Jenkins server, we will only keep the build logs for 5 days with a maximum of 10 builds. You can change this to something that makes more sense to you.

Agent Node

Check the box Restrict where this project can be run. In the Label Expression box enter the name of the agent that the Jenkins administrator gave you. In our case, tiger1_luet because the agent node name is tiger1 and my Princeton netid is luet.

img

Source Code Management (Git)

img

img

The build should run for a while and when it stops, it should have a blue ball on the left of the number. You can also ssh onto the agent node directly and you should see that the jenkinstutorial repository was cloned in the directory jenkins in your home directory.

Add a test

We will now add a test. We don’t specfify any Build Triggers for now, we will be testing by triggering a build manually with the BuildNow button.

img

Add e-mail notifications

Now we will have Jenkins send you an e-mail each time a build is run.

img

img

Automatically Trigger A Build When Someone Opens a Pull-Request

Using the Build Now button is useful for testing, but in production mode you want the tests to be run automatically.

We will now add a build trigger that will start a build automatically when someone opens a Pull-request toward your clone on GitHub. A great advantage of testing on a pull-request is that the changes are tested before they are committed to the git repository.

Changes on the Jenkins Server

In the Configure menu:

Changes on GitHub

You need to add a Webhook to your GitHub account. A Webhook is a mechanism for GitHub to send a message to the Jenkins server when a Pull-Request has been opened.

img

Open a Pull-Request To Test the New Settings

Now we will make a change to the code and open a Pull-request to merge the changes. We will do it directly on the GitHub web site but you could do everything on the command line if you are familiar with git. We will simply add a README file to the repository.