A Jenkins plugin for interfacing with GitLab
This plugin allows GitLab to trigger builds in Jenkins when code is committed or merge requests are opened/updated. It can also send build status back to GitLab.
This plugin is Open Source Software, developed on a volunteer basis by users of Jenkins and GitLab. It is not formally supported by either GitLab Inc. or CloudBees Inc.
GitLab performs a new major release about every six to nine months, and they are constantly fixing bugs and adding new features. As a result, we cannot support this plugin when used with GitLab versions older than N-2, where N is the current major release.
If you have a problem or question about using the plugin, please make sure you are using the latest version. Then create an issue in the GitHub project.
To enable debug logging in the plugin:
The plugin tracks current issues with the GitHub issue tracker. Some issues are reported in the Jenkins Jira issue tracker. When searching for existng issues, please check both locations.
Please report issues and enhancements through the GitHub issue tracker.
When GitLab triggers a build via the plugin, various environment variables are set based on the JSON payload that GitLab sends. You can use these throughout your job configuration. The available variables are:
…NOTE: These variables are not available in Pipeline Multibranch jobs.
The plugin requires authentication to connect from GitLab to Jenkins. This prevents unauthorized persons from triggering jobs.
APITOKENS and other secrets MUST not be send over unsecure connections. So, all connections SHOULD use HTTPS.
Note: Certificates are free and easy to manage with LetsEncrypt.
https://USERID:APITOKEN@JENKINS_URL/project/YOUR_JOBIf you want to create separate authentication credentials for each Jenkins job:
https://JENKINS_URL/project/YOUR_JOB) and paste the token in the Secret Token fieldIf you want to disable this authentication (not recommended):
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:
https://your.gitlab.serverThere are two aspects of your Jenkins job that you may want to modify when using GitLab to trigger jobs. The first is the Git configuration, where Jenkins clones your git repo. The GitLab Plugin will set some environment variables when GitLab triggers a build, and you can use those to control what code is cloned from Git. The second is the configuration for sending the build status back to GitLab, where it will be visible in the commit and/or merge request UI.
If you want to be able to run jobs both manually and automatically via GitLab webhooks, you will need to configure parameters for those jobs. If you only want to trigger jobs from GitLab, you can skip this section.
Any GitLab parameters you create will always take precedence over the values that are sent by the webhook, unless you use the EnvInject plugin to map the webhook values onto the job parameters. This is due to changes that were made to address security vulnerabilities, with changes that landed in Jenkins 2.3.
In your job configuration, click 'This build is parameterized' and add any parameters you want to use. See the defined variables list for options - your parameter names must match these .e.g sourceBranch and targetBranch in the example Groovy Script below. Then, having installed EnvInject, click 'Prepare an environment for the run' and check:
In the Groovy Script field insert something similar to:
def env = currentBuild.getEnvironment(currentListener)
def map = [:]
if (env.gitlabSourceBranch != null) {
map['sourceBranch'] = env.gitlabSourceBranch
}
if (env.gitlabTargetBranch != null) {
map['targetBranch'] = env.gitlabTargetBranch
}
return mapYou can then reference these variables in your job config, e.g. as ${sourceBranch}. You will need to update this code anytime you add or remove parameters.
Note: If you use the Groovy Sandbox, you might need to approve the script yourself or let an administrator approve the script in the Jenkins configuration.
In the Source Code Management section:
[email protected]:gitlab_group/gitlab_project.gitorigin and Refspec to +refs/heads/*:refs/remotes/origin/* +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*origin/${gitlabSourceBranch}merge-requests/${gitlabMergeRequestIid}origin${gitlabTargetBranch}A Jenkins Pipeline bug will prevent the Git clone from working when you use a Pipeline script from SCM. It works if you use the Jenkins job config UI to edit the script. There is a workaround mentioned here: https://issues.jenkins-ci.org/browse/JENKINS-33719
Use the Snippet generator, General SCM step, to generate sample Groovy code for the git checkout/merge etc.
Example that performs merge before build:
checkout changelog: true, poll: true, scm: [
$class: 'GitSCM',
branches: [[name: "origin/${env.gitlabSourceBranch}"]],
extensions: [[$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: 'origin', mergeStrategy: 'DEFAULT', mergeTarget: "${env.gitlabTargetBranch}"]]],
userRemoteConfigs: [[name: 'origin', url: '[email protected]:foo/testrepo.git']]
]Note: There is no way to pass external data from GitLab to a Pipeline Multibranch job, so the GitL
No open issues yet, or sync has not completed.