Setting Up a New Project

Table of Contents

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

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

CommitComment.png

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.

1 Creating a Simple Build with Manual Trigger

  • Go to https://jenkins.princeton.edu
  • You should see the folder that was assigned to you. On the figure below, it's tutorial_folder

    tutorial_folder.png

  • Click on the folder.
  • Click on New Item

NewItem.png

  • Create a new Freestyle project named jenkins_tutorial. Click OK.

    NewFreeStyleProject.png

1.1 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.

  • Check the box Discard Old Builds:
    • Set Days to keep builds to 5.
    • Set Max # of builds to keep to 10.

      DiscardOldBuilds.png

1.2 Slave Node

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

GitLabProjectRestrict.png

1.3 Source Code Management (Git)

  • In Source Code Management select Git.
  • In the Repository URL enter
    git@gitlab.com:GitLabLogin/jenkins_tutorial.git
    

    where you should replace GitHubLogin with your GitHub login, which is luet in my case.

  • In the Credentials box select the Credential you entered in Section [[jenkins_creds_for_GitLab]. It appears as luet (jenkins_tutorial deploy) in the screen snapshot below.
  • In Branches to build, leave */master for now.
  • At the bottom of the page click Apply.

SCMGitSetup.png

  • At this point you have enough to test whether the repository can be cloned on the slave. To do so, click the Build Now button on the upper left hand side toolbar.

    BuildNow.png

    You should see a build starting in the left-hand sidebar:

FirstBuild.png

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 slave node directly and you should see that the jenkins_tutorial repository was cloned in the directory jenkins in your home directory.

1.4 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.

  • Go to Build and click Add build step and select Execute shell

    AddBuildStep_ExecuteShell.png

    With the Execute shell you are basically given a shell on the slave. The current directory for this shell is $HOME/jenkins/folder_name/job_name.

  • In the Execute shell box we enter bash commands to run the test that comes with the git repository in the tests directory. Note that there is no space between #! and /bin/bash, Jenkins will fail if there is a space.
    #!/bin/bash
    module load python/2.7
    python test.py
    

    Here we are running a Python unit test. This test uses the Python module unittest which is only available in Python >= 2.7.

  • Then click Save to save this step.

    BuildStepSimpleTest.png

  • Test this build step by clicking Build Now at the top of the left-hand sidebar.
  • After the run is done, the build history should show your test with a blue ball next to it

SimpleBuildNowTest.png

  • On the figure above the last build is #2. Click on the build number to get more details about the build.

    BuildDetails.png

  • Click on Console Output to standard output of your run. The build consists in two steps:
    1. cloning (the first time) or updating (subsequent times) the code.
    2. running the test.

    In that case the status of the test is Finished: SUCCESS.

    SimpleTestConsoleOutput.png

1.5 Add e-mail notifications

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

  • Go to the bottom of the page, in the section Post-build Actions.

    PostBuildActions.png

  • Click on the Add post-build action button and select Editable Email Notification. There is another choice, E-mail Notification, but it is less configurable than Editable Email Notification.

EditableEmailNotification.png

  • In the Editable Email Notification box:
    • In the Project Recipient List, delete $DEFAULT_RECIPIENTS and enter your e-mail address instead

      EmailRecipientList.png

      If you leave the $DEFAULT_RECIPIENTS, Jenkins will look through the logs of the git repository and find the e-mails of all the developers.

    • Select Advanced Settings...

      EmailAdvancedSettings.png

  • In the Triggers box, remove the default trigger, by clicking Remove Trigger

    RemoveDefaultEmailTrigger.png

    The Failure - Any trigger sends an e-mail only in case the tests fails. In this tutorial, we want to receive an e-mail even in case of the tests were successful.

  • Select Add Trigger and Always

AddEmailTriggerAlways.png

  • Click Delete under Developers,

    DeleteDevelopersEmail.png

  • Click Save at the bottom of the page.
  • Click Build Now to run a build and you should receive an e-mail.

2 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 merge request toward your clone on GitLab. A great advantage of testing on a merge request is that the changes are tested before they are committed to the git repository.

2.1 Changes on the Jenkins Server

In the Configure menu:

  • Go to the Source Code Management and click the Advanced button

    SCMAdvancedSettings.png

  • In the Name box enter: origin
  • In the Refspec box enter: +refs/heads/*:refs/remotes/origin/* +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*. It is a regular expression that tells Jenkins to fetch the merge requests from GitHub.

    MRTriggerNameRefspec.png

  • In the Branches to build section, box Branch Specifier, replace */master with origin/${gitlabMergeRequestIid}. This tells Jenkins to use the variable gitlabMergeRequestIid that is sent to Jenkins by GitHub. Note that at that point you cannot use the Build Now button anymore.

    MRTriggerBranchSpecifier.png

  • In the Build Triggers section:
    • check Build when a change is pushed to GitLab. GitLab CI Service URL: https://jenkins.princeton.edu/project/....
    • check Merge Request Events

    GitLabMRTrigger.png

  • In the Post-build Actions section, select Add post-build action, then Publish build status to GitLab commit (GitLab 8.1 + required)

    PostBuildAction.png

  • Click Save at the bottom of the page.

2.2 Changes on GitLab

You need to add a service to your GitLab account. A service is a mechanism for GitLab to send a message to the Jenkins server when a merge request has been opened.

  • Go to your fork on GitHub and select the settings button (SettingsButton.png) then Webhooks

    GitLabWebhooksSettings.png

  • The URL is defined from the URL of your project on Jenkins—that is; what appears in your browser's navigation bar when you are on Jenkins. For example, in my case the URL for my Jenkins project on the jenkins site is:
    https://jenkins.princeton.edu/job/tutorial_folder/job/jenkins_tutorial/
    

    and the project name for the GitLab webhook is:

    https://jenkins.princeton.edu/project/tutorial_folder/jenkins_tutorial
    
  • In the Trigger section, select Merge Request events.
  • In the SSL verification section, select Enable SSL verification.
  • Click Add Webhook.

    GitLabWebhookJenkinsURL.png

  • Once the Webhook is added click Test to test your settings.

    GitLabWebhookTest.png

    Something like the following figure should appear at the top of your screen.

    GitLabWebhookTestResult.png

2.3 Open a Pull-Request To Test the New Settings

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

  • First, we create a new branch on your GitLab repo, we call it devel

    GitLabNewBranch.png

    GitLabNewBranch-2.png

  • Now create the file README.md in the branch devel

    GitLabNewFile.png

    GitLabAddREADME.png

  • Now create the Merge Request

    GitLabCreateMergeRequest.png

    GitLabSubmitMergeRequest.png

  • If all goes well:
    1. On the Jenkins server you will see that a job was triggered

      GitLabJenkinsHistory.png

    2. On the status of the test will be reported on the GitLab site

      GitLabMergeRequestSuccess.png

      Then you can merge the changes by clicking Merge When Build Succeeds.