'Merge when pipeline succeeds' functionality merges the MR despite the pipeline is still running when GitLab pipeline is externally managed by jenkinsci/gitlab-plugin
Summary
'Merge when pipeline succeeds' functionality merges the MR despite the pipeline is still running when GitLab pipeline is externally managed by jenkinsci/gitlab-plugin.
Steps to reproduce
- Create project in GitLab
- Setup interop with Jenkins (add HTTP hooks to trigger Jenkins on Merge Request creation) and setup GitLab service user for registering pipelines. declarative pipeline should look like:
// ...
triggers {
gitlab(
triggerOnPush: true,
triggerOnMergeRequest: true,
triggerOpenMergeRequestOnPush: 'never',
triggerOnNoteRequest: true,
noteRegex: '//rebuild',
skipWorkInProgressMergeRequest: false,
ciSkip: false,
setBuildDescription: true,
addNoteOnMergeRequest: true,
addCiMessage: true,
addVoteOnMergeRequest: true,
acceptMergeRequestOnSuccess: false,
branchFilterType: 'All',
includeBranchesSpec: '',
excludeBranchesSpec: '',
cancelPendingBuildsOnUpdate: false,
secretToken: 'jenkins-token'
)
}
// ...
stage('stagename') {
steps {
gitlabCommitStatus('stagename') {
container('builder') {
// ...
}
}
}
}
// ...
- Issue Merge Request
- HTTP hooks would fire starting Jenkins job, once first stage completes GitLab will show running pipeline (Jenkins uses gitlabCommitStatus to report the status)
- Click 'Merge when pipeline succeeds' and wait.
description to be continued below
Example Project
Cannot provide, it's company's project.
What is the current bug behavior?
- When next step is reported to GitLab (by gitlabCommitStatus) with status success GitLab still shows pipeline as running at the same time merges the request (!!!). Apparently 2 different mechanisms are used to determine if pipeline is running (to show in the GUI) and to determine if change can be merged to master. Additionally such pipeline after merge is attributed to master branch instead of Merge Request branch.
Please mind that there is no interop problem at all when 'Merge when pipeline succeeds' is not used.
What is the expected correct behavior?
- GitLab should not attempt at merge the Merge Request but wait for pipeline to finish.
Workaround
Use
post {
success {
updateGitlabCommitStatus name: 'pipelineStatus', state: 'success'
}
unsuccessful {
updateGitlabCommitStatus name: 'pipelineStatus', state: 'failed'
}
}
triggers {
gitlab(
// ...
pendingBuildName: 'pipelineStatus',
// ...
clause to register "artificial" Job in GitLab which is updated then when pipeline finises (post) clause.