GitLab Merge Request触发Jenkins pipeline

文章目录

  • GitLab Merge Request触发Jenkins pipeline
    • 前言
    • 配置Build Triggers
    • 配置Jenkins webhook
    • 配置Merge request settings (可选)
    • 配置Jenkins流水线
    • 测试
    • 参考文档

GitLab Merge Request触发Jenkins pipeline

前言

在 GitLab push自动触发Jenkins构建与持续集成 文章中,我们介绍了GitLab push触发Jenkins构建。

与push触发Jenkins构建相比,merge request触发Jenkins构建使得在代码合并进目标分支(target branch)前就可以检查改动的代码是否会导致CI失败,为代码评审提供了重要的依据。

其它的步骤都可以参考上面的GitLab push自动触发Jenkins构建与持续集成 文章。

下面只说一些特别需要注意的地方。

配置Build Triggers

打开Jenkins job

勾选Build when a change is pushed to GitLab. GitLab webhook URL

勾选“ Opened Merge Request Events ”

打开“Advanced…",在Allow branches中勾选Filter branch by regex,来输入Source branch和Target branch限定只有某些source branch或target branch才会触发Jenkins构建。

配置Jenkins webhook

参考上面的文章,在GitLab配置Jenkins webhook,不同的是要选择Trigger为Merge Request Events,取消勾选Enable SSL verification。

在GitLab上测试webhook是否能够成功通知Jenkins。

配置Merge request settings (可选)

在GitLab上,打开项目配置,打开Settings / General / Merge request settings

勾选”Only allow merge requests to be merged if the pipeline succeeds “,使得只有Jenkins pipeline运行成功时才允许在界面上merge。

这个功能需要Jenkins pipeline做些特别支持,参见:

  • https://github.com/jenkinsci/gitlab-plugin

配置Jenkins流水线

配置GitLab Connection:

options {
	gitLabConnection('GitlabAccess')
}

Checkout merge request的代码:

stage('Checkout') {
    steps {
        checkout changelog: true, poll: true, scm: [
          $class: 'GitSCM',
          branches: [[name: "origin/${env.gitlabSourceBranch}"]],
          doGenerateSubmoduleConfigurations: false,
          extensions: [[
            $class: 'PreBuildMerge',
            options: [
              fastForwardMode: 'FF',
              mergeRemote: 'origin',
              mergeStrategy: 'default',
              mergeTarget: "${env.gitlabTargetBranch}"
            ]
          ]],
          extensions: [[
            $class: 'UserIdentity', 
            email: "${env.gitlabUserEmail}", 
            name: "${env.gitlabUserName}"
          ]],
          submoduleCfg: [],
          userRemoteConfigs: [[
            credentialsId: "${GIT_CREDENTIALS_ID}",
            name: 'origin',
            url: "${env.gitlabSourceRepoURL}"
          ]]
        ]
    }
}

其中PreBuildMerge会在拉取source branch的代码后,在Jenkins workspace下做一个本地的git merge操作

更新GitLab状态的post-actions:

post {
    success {
        updateGitlabCommitStatus(name: 'build', state: 'success')
    }
    failure {
        updateGitlabCommitStatus(name: 'build', state: 'failed')
        addGitLabMRComment comment: "Something unexpected happened. Please inspect Jenkins logs."
    }        
}

测试

在GitLab上创建一个新的merge request来测试是否会自动触发Jenkins pipeline。

注意:如果一个GitLab merge request还在open状态,每次往这个merge request的source branch提交代码,都会触发一个新的merge request event通知到Jenkins。

参考文档

  • https://blog.ljdelight.com/gitlab-trigger-jenkins-builds-on-merge-request/

  • https://github.com/jenkinsci/gitlab-plugin

  • https://wiki.jenkins.io/display/JENKINS/Git+Plugin#GitPlugin-AdvancedFeatures

  • https://vetlugin.wordpress.com/2017/01/31/guide-jenkins-pipeline-merge-requests/

  • https://medium.com/@teeks99/continuous-integration-with-jenkins-and-gitlab-fa770c62e88a

  • https://docs.bitnami.com/aws/how-to/create-ci-pipeline/

  • https://www.diycode.cc/projects/jenkinsci/gitlab-plugin

  • https://support.cloudbees.com/hc/en-us/articles/226122247-How-to-Customize-Checkout-for-Pipeline-Multibranch-

你可能感兴趣的:(Jenkins,GitLab)