Gitlab Jenkins Tests

A quick summary of Gitlab Jenkins Tests

GITLAB and Jenkins CI

Install plugins in Jenkins

Two plugins should be installed in Jenkins:

Jenkins-to-GitLab authentication

PLEASE NOTE: This auth configuration is only used for accessing the GitLab API for sending build status to GitLab. It is not used for cloning git repos. The credentials for cloning (usually SSH credentials) should be configured separately, in the git plugin.

This plugin can be configured to send build status messages to GitLab, which show up in the GitLab Merge Request UI. To enable this functionality:

  • Create a new user in GitLab
  • Give this user 'Developer' permissions on each repo you want Jenkins to send build status to
  • Log in or 'Impersonate' that user in GitLab, click the user's icon/avatar and choose Settings
  • Click on 'Access Tokens'
  • Create a token named e.g. 'jenkins' with 'api' scope; expiration is optional
  • Copy the token immediately, it cannot be accessed after you leave this page
  • On the Global Configuration page in Jenkins, in the GitLab configuration section, supply the GitLab host URL, e.g. http://your.gitlab.server
  • Click the 'Add' button to add a credential, choose 'GitLab API token' as the kind of credential, and paste your GitLab user's API key into the 'API token' field
  • Click the 'Test Connection' button; it should succeed

GitLab-to-Jenkins authentication

  • Create a user in Jenkins or use yours, ensure it has, at a minimum, Job/Build permissions
  • Log in as that user (this is required even if you are a Jenkins admin user), then click on the user's name in the top right corner of the page
  • Click 'Configure,' then 'Show API Token...', and note/copy the User ID and API Token
  • In GitLab, when you create webhooks to trigger Jenkins jobs, use this format for the URL and do not enter anything for 'Secret Token': http://USERID:APITOKEN@JENKINS_URL/project/YOUR_JOB
  • After you add the webhook, click the 'Test' button, and it should succeed

Add webhooks in Gitlab

Check the Build when a change is pushed to GitLab in Jenkins job. Copy webhook url in this job and add a webhook in a view of http://USERID:APITOKEN@JENKINS_URL/project/YOUR_JOB.

Activate Jenkins CI in gitlab

Create a new GitLab project or choose an existing one. Then, go to Integrations -> Jenkins CI.

Check the ‘Active’ box. Select whether you want GitLab to trigger a build on push, Merge Request creation, tag push, or any combination of these. We recommend unchecking ‘Merge Request events’ unless you have a specific use-case that requires re-building a commit when a merge request is created. With ‘Push events’ selected, GitLab will build the latest commit on each push and the build status will be displayed in the merge request.

Enter the Jenkins URL and Project name. The project name should be URL-friendly where spaces are replaced with underscores. To be safe, copy the project name from the URL bar of your browser while viewing the Jenkins project.

Optionally, enter a username and password if your Jenkins server requires authentication.

Update Build status in Pipeline

  • For Pipeline jobs, surround your build steps with the gitlabCommitStatus step like this:
node() {
    stage('Checkout') { checkout <your-scm-config> }

    gitlabCommitStatus {
       // The result of steps within this block is what will be sent to GitLab
       sh 'mvn install'
    }
}
  • Or use the updateGitlabCommitStatus step to use a custom value for updating the commit status. You could use try/catch blocks or other logic to send fine-grained status of the build to GitLab. Valid statuses are defined by GitLab and documented here: https://docs.gitlab.com/ce/api/pipelines.html
node() {
    stage('Checkout') { checkout <your-scm-config> }

    updateGitlabCommitStatus name: 'build', state: 'pending'
    // Build steps
    
    updateGitlabCommitStatus name: 'build', state: 'success'
}
  • Or you can mark several build stages as pending in GitLab, using the gitlabBuilds step:
node() {
    stage('Checkout') { checkout <your-scm-config> }

    gitlabBuilds(builds: ["build", "test"]) {
        stage("build") {
          gitlabCommitStatus("build") {
              // your build steps
          }
        }

        stage("test") {
          gitlabCommitStatus("test") {
              // your test steps
          }
        }
    }
}

Note: If you put the gitlabBuilds block inside a node block, it will not trigger until a node is allocated. On a busy system, or one where nodes are allocated on demand, there could be a delay here, and the 'pending' status would not be sent to GitLab right away. If this is a concern, you can move the gitlabBuilds block to wrap the node block, and then the status will be sent when Jenkins starts trying to allocate a node.

Defined variables

gitlabBranch
gitlabSourceBranch
gitlabActionType
gitlabUserName
gitlabUserEmail
gitlabSourceRepoHomepage
gitlabSourceRepoName
gitlabSourceNamespace
gitlabSourceRepoURL
gitlabSourceRepoSshUrl
gitlabSourceRepoHttpUrl
gitlabMergeRequestTitle
gitlabMergeRequestDescription
gitlabMergeRequestId
gitlabMergeRequestIid
gitlabMergeRequestState
gitlabMergedByUser
gitlabMergeRequestAssignee
gitlabMergeRequestLastCommit
gitlabMergeRequestTargetProjectId
gitlabTargetBranch
gitlabTargetRepoName
gitlabTargetNamespace
gitlabTargetRepoSshUrl
gitlabTargetRepoHttpUrl
gitlabBefore
gitlabAfter
gitlabTriggerPhrase