Reputation: 2006
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 ?
Upvotes: 2
Views: 3672
Reputation: 347
To trigger your build when a merge request is opened or when a push is made to an branch that has an open merge request, add the following to your Jenkinsfile
triggers
{
gitlab(triggerOnMergeRequest: true, triggerOpenMergeRequestOnPush: "source", branchFilterType: 'All')
cron(cron_string)
}
See https://github.com/jenkinsci/gitlab-plugin#defined-variables for more "trigger" options
To only execute stages if it was triggered from a merge request, you can check if the gitlabMergeRequestId
is set. If you do this in a when block, like the following, it will only execute your stages if the ID is set, which means it came from a merge request. This when block will only execute the stages if it is the master or develop branch, or it was triggered by a merge request
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')
{
:
:
}
}
}
Upvotes: 2
Reputation: 9174
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("[email protected]")
branch("refs/heads/master")
}
}
}
scriptPath("jenkinsfile")
}
}
}
Upvotes: 2