Issue
In a declarative pipeline, using a multibranch job and a Jenkinsfile, can the job be aborted with a success
exit code rather than a failure
exit code?
In the below stage, I basically check if the commit that started the job contains "ci skip", and if it does I want to abort the job.
Using error
aborts the job but also marks it as such (red row). I'd like this job to be marked with a green row.
stage ("Checkout SCM") {
steps {
script {
checkout scm
result = sh (script: "git log -1 | grep '.*\\[ci skip\\].*'", returnStatus: true)
if (result == 0) {
error ("'ci skip' spotted in git commit. Aborting.")
}
}
}
}
Edit:
Instead of the above, I am now trying to simply skip all stages in case the git commit contains "ci skip". My understanding is that if the result in expression
is false
, it should skip the stage...
pipeline {
environment {
shouldBuild = "true"
}
...
...
stage ("Checkout SCM") {
steps {
script {
checkout scm
result = sh (script: "git log -1 | grep '.*\\[ci skip\\].*'", returnStatus: true)
if (result == 0) {
echo ("'ci skip' spotted in git commit. Aborting.")
shouldBuild = "false"
}
}
}
}
stage ("Unit tests") {
when {
expression {
return shouldBuild
}
}
steps {
...
}
}
...
...
}
Solution
Okay, so the way to get this to work is not with the environment
directive but rather to use the parameters
directive.
That is, adding parameters
at the top of the pipeline
:
parameters {
booleanParam(defaultValue: true, description: 'Execute pipeline?', name: 'shouldBuild')
}
When checking for the git commit, if it contains "ci skip" I change the value of shouldBuild
:
env.shouldBuild = "false"
Then in expression
:
expression {
return env.shouldBuild != "false"
}
And that's it. In case the git commit contains "ci skip" the stages are skipped and the job finishes with SUCCESS
.
Answered By - Idan Adar