在gitlab MR的合并事件上触发Jenkins Pipeline作业

r6l8ljro  于 2023-08-03  发布在  Jenkins
关注(0)|答案(2)|浏览(144)

这应该是相当基本的,但当我研究时,我遇到了像Gerrit Trigrs之类的东西,这些东西对于做这样简单的事情来说似乎太复杂了。
我想在JobDSL脚本中执行类似以下操作:

pipelineJob('deploy-game') {
    definition {
        environmentVariables {
          env('ENVIRONMENT', "${ENVIRONMENT}")

          keepBuildVariables(true)
        }
        cpsScm {
            scm {
                git{
                    remote {
                        url('https://blabla.git')
                        credentials('gitlab-credentials')
                    }
                    branches('${gitlabsourcebranch}')
                }
            }
            scriptPath('path/to/this.jenkinsfile')
        }
        triggers {
            gitlabPush {
                buildOnMergeRequestEvents(true)
                if ($gitlabMergeRequestState == 'merged')  // this part
            }
        }
    }
}

字符串
或者,触发所有MR事件,然后在管道脚本中过滤掉:

pipeline {
  agent none
  environment {
    ENVIRONMENT   = "${ENVIRONMENT}"
  }
  triggers {
    $gitlabMergeRequestState == 'merged' // this one
  }
  stages {
        stage ('do-stuff') {
          agent {
            label 'agent'
          }
          steps {
            sh 'some commands ...'
          }
        }
   }
}


我该怎么做?

yfjy0ee7

yfjy0ee71#

这就是它应该是什么样子,我希望这就是你正在寻找的。

pipelineJob('Job_Name') {
          definition {
            cpsScm {
                lightweight(true)
                triggers {
                    gitlabPush {
                        buildOnMergeRequestEvents(true) // it will trigger build when MR is opened.
                        buildOnPushEvents(true)
                        commentTrigger('retry a build') // When you write the comment on MR on gitlab. it will also trigger build
                        enableCiSkip(true)
                        rebuildOpenMergeRequest('source')
                        skipWorkInProgressMergeRequest(false)
                        targetBranchRegex('.*master.*|.*release.*') //This mean only push happened to master or release then only trigger jenkins build. Do not trigger build on normal feature branch push until the MR is opened.                   
                    }
                }
                configure {
                    it / triggers / 'com.dabsquared.gitlabjenkins.GitLabPushTrigger' << secretToken('ADD_TOKEN_FROM_JENKINS_JOB')
                }
                scm {      
                    git {
                        remote {
                            credentials('ID')
                            url("git@URL.git")
                            branch("refs/heads/master")
                        }
                    }
                }
                scriptPath("jenkinsfile")
            }
          }
        }

字符串

xv8emn3q

xv8emn3q2#

要在打开合并请求或向具有打开合并请求的分支进行推送时触发构建,请将以下内容添加到Jenkinsfile

triggers
{
    gitlab(triggerOnMergeRequest: true, triggerOpenMergeRequestOnPush: "source", branchFilterType: 'All')
    cron(cron_string)
}

字符串
有关更多“触发器”选项,请访问https://github.com/jenkinsci/gitlab-plugin#defined-variables
要只执行从合并请求触发的stage,可以检查是否设置了gitlabMergeRequestId。如果在when块中执行此操作(如下所示),则只有在设置了ID(这意味着它来自合并请求)的情况下才会执行stages。这个when块只会在它是master或develop分支,或者它是由合并请求触发的情况下执行stage

stage("Build")
{
    when
    {
        anyOf
        {
            branch 'master'
            branch 'develop'
            changeRequest() // Jenkins documentation claims this will check if a
                            // GitLab Merge Request triggered the build, but it
                            // was not working for me. It could be the version of
                            // Jenkins I am using. I have seen this work with
                            // Bitbucket. Anyone know?
            expression{gitlabMergeRequestId != null} // Is a merge request
        }
    }
    stages // Will no execute if no develop, master or a merge request
    {
        stage('MyStage')
        {
            :
            :
        }
    }
}

相关问题