jenkinsgitlabjenkins-pipelinejenkins-job-dsl

Trigger Jenkins Pipeline job on merge event for gitlab MR


this should be fairly basic, but when I research I come to things like gerrit triggrs and whatnot, which seem way too complicated for doing something simple like this.

I would like to do something like either this in the JobDSL script:

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
            }
        }
    }
}

Or, trigger on all MR events, and then filter out in the pipeline script:

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

How do I do this ?


Solution

  • So this is how it should be, I hope this is what you are looking for it.

    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")
                }
              }
            }