debian-mirror-gitlab/doc/integration/jenkins.md
2020-05-24 23:13:21 +05:30

9.3 KiB
Raw Blame History

Jenkins CI service (STARTER)

NOTE: Note: This documentation focuses only on how to configure a Jenkins integration with GitLab. Learn how to migrate from Jenkins to GitLab CI/CD in our Migrating from Jenkins documentation.

From GitLab, you can trigger a Jenkins build when you push code to a repository, or when a merge request is created. In return, Jenkins shows the pipeline status on merge requests widgets and on the GitLab project's home page.

To better understand GitLab's Jenkins integration, watch the following video:

For a real use case, read the blog post Continuous integration: From Jenkins to GitLab using Docker.

Moving from a traditional CI plug-in to a single application for the entire software development life cycle can decrease hours spent on maintaining toolchains by 10% or more. For more details, see the 'GitLab vs. Jenkins' comparison page.

Configure GitLab integration with Jenkins

GitLab's Jenkins integration requires installation and configuration in both GitLab and Jenkins. In GitLab, you need to grant Jenkins access to the relevant projects. In Jenkins, you need to install and configure several plugins.

GitLab requirements

Jenkins requirements

Grant Jenkins access to GitLab project

Grant a GitLab user access to the select GitLab projects.

  1. Create a new GitLab user, or choose an existing GitLab user.

    This account will be used by Jenkins to access the GitLab projects. We recommend creating a GitLab user for only this purpose. If you use a person's account, and their account is deactivated or deleted, the GitLab-Jenkins integration will stop working.

  2. Grant the user permission to the GitLab projects.

    If you're integrating Jenkins with many GitLab projects, consider granting the user global Admin permission. Otherwise, add the user to each project, and grant Developer permission.

Configure GitLab API access

Create a personal access token to authorize Jenkins' access to GitLab.

  1. Log in to GitLab as the user to be used with Jenkins.
  2. Click your avatar, then **Settings.
  3. Click Access Tokens in the sidebar.
  4. Create a personal access token with the API scope checkbox checked. For more details, see Personal access tokens.
  5. Record the personal access token's value, because it's required in Configure the Jenkins server.

Configure the Jenkins server

Install and configure the Jenkins plugins. Both plugins must be installed and configured to authorize the connection to GitLab.

  1. On the Jenkins server, go to Manage Jenkins > Manage Plugins.
  2. Install the Jenkins GitLab Plugin.
  3. Go to Manage Jenkins > Configure System.
  4. In the GitLab section, check the Enable authentication for /project end-point checkbox.
  5. Click Add, then choose Jenkins Credential Provider.
  6. Choose GitLab API token as the token type.
  7. Enter the GitLab personal access token's value in the API Token field and click Add.
  8. Enter the GitLab server's URL in the GitLab host URL field.
  9. Click Test Connection, ensuring the connection is successful before proceeding.

For more information, see GitLab Plugin documentation about Jenkins-to-GitLab authentication.

Jenkins GitLab plugin configuration

Configure the Jenkins project

Set up the Jenkins project youre going to run your build on.

  1. On your Jenkins instance, go to New Item.
  2. Enter the project's name.
  3. Choose between Freestyle or Pipeline and click OK. We recommend a Freestyle project, because the Jenkins plugin will update the build status on GitLab. In a Pipeline project, you must configure a script to update the status on GitLab.
  4. Choose your GitLab connection from the dropdown.
  5. Check the Build when a change is pushed to GitLab checkbox.
  6. Check the following checkboxes:
    • Accepted Merge Request Events
    • Closed Merge Request Events
  7. Specify how build status is reported to GitLab:
    • If you created a Freestyle project, in the Post-build Actions section, choose Publish build status to GitLab.

    • If you created a Pipeline project, you must use a Jenkins Pipeline script to update the status on GitLab.

      Example Jenkins Pipeline script:

      pipeline {
         agent any
      
         stages {
            stage('gitlab') {
               steps {
                  echo 'Notify GitLab'
                  updateGitlabCommitStatus name: 'build', state: 'pending'
                  updateGitlabCommitStatus name: 'build', state: 'success'
               }
            }
         }
      }
      

Configure the GitLab project

Configure the GitLab integration with Jenkins.

  1. Create a new GitLab project or choose an existing one.

  2. Go to Settings > Integrations, then select Jenkins CI.

  3. Turn on the Active toggle.

  4. Select the events you want GitLab to trigger a Jenkins build for:

    • Push
    • Merge request
    • Tag push
  5. Enter the Jenkins URL.

  6. Enter the Project name.

    The project name should be URL-friendly, where spaces are replaced with underscores. To ensure the project name is valid, copy it from your browser's address bar while viewing the Jenkins project.

  7. Enter the Username and Password if your Jenkins server requires authentication.

  8. Click Test settings and save changes. GitLab tests the connection to Jenkins.

Plugin functional overview

GitLab does not contain a database table listing commits. Commits are always read from the repository directly. Therefore, it's not possible to retain the build status of a commit in GitLab. This is overcome by requesting build information from the integrated CI tool. The CI tool is responsible for creating and storing build status for Commits and Merge Requests.

Steps required to implement a similar integration

Note: All steps are implemented using AJAX requests on the merge request page.

  1. In order to display the build status in a merge request you must create a project service in GitLab.
  2. Your project service will do a (JSON) query to a URL of the CI tool with the SHA1 of the commit.
  3. The project service builds this URL and payload based on project service settings and knowledge of the CI tool.
  4. The response is parsed to give a response in GitLab (success/failed/pending).

Troubleshooting

Error in merge requests - "Could not connect to the CI server"

This integration relies on Jenkins reporting the build status back to GitLab via the Commit Status API.

The error 'Could not connect to the CI server' usually means that GitLab did not receive a build status update via the API. Either Jenkins was not properly configured or there was an error reporting the status via the API.

  1. Configure the Jenkins server for GitLab API access
  2. Configure the Jenkins project, including the 'Publish build status to GitLab' post-build action.

Merge Request event does not trigger a Jenkins Pipeline

Check service hook logs for request failures or check the /var/log/gitlab/gitlab-rails/production.log file for messages like:

WebHook Error => Net::ReadTimeout

or

WebHook Error => execution expired

If those are present, the request is exceeding the webhook timeout, which is set to 10 seconds by default.

To fix this the gitlab_rails['webhook_timeout'] value will need to be increased in the gitlab.rb config file, followed by the gitlab-ctl reconfigure command.

If you don't find the errors above, but do find duplicate entries like below (in /var/log/gitlab/gitlab-rail), this could also indicate that webhook requests are timing out:

2019-10-25_04:22:41.25630 2019-10-25T04:22:41.256Z 1584 TID-ovowh4tek WebHookWorker JID-941fb7f40b69dff3d833c99b INFO: start
2019-10-25_04:22:41.25630 2019-10-25T04:22:41.256Z 1584 TID-ovowh4tek WebHookWorker JID-941fb7f40b69dff3d833c99b INFO: start