Issue
I'm trying to create a Jenkinsfile that runs a release build step only if a boolean parameter isRelease
is true, otherwise skip this step.
My Jenkinsfile looks like this (extract):
pipeline {
agent { label 'build' }
tools {
jdk 'OpenJDK 11'
}
parameters {
booleanParam( name: 'isRelease', description: 'Run a release build?', defaultValue: false)
}
stages {
stage('Build') {
steps {
sh 'mvn clean compile'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Release') {
when {
beforeAgent true
expression { return isRelease }
}
steps {
sh 'echo "######### Seems to a release!"'
}
}
}
}
However, I don't seem to understand how to use the parameters variable properly. What happens is that the release step is always executed.
I changed expression { return isRelease }
to expression { return "${params.isRelease}" }
which did not make a difference. Changing it to expression { return ${params.isRelease} }
causes the step to fail with java.lang.NoSuchMethodError: No such DSL method '$' found among steps
.
What's the right way to use a parameter to skip a step?
Solution
You were closest on your first attempt. The latter two failed because:
- Converting a Boolean to a String would always return truthiness because a non-empty String is truthy.
- That is invalid Jenkins Pipeline or Groovy syntax.
The issue here with the first attempt is that you need to access isRelease
from within the params
object.
when {
beforeAgent true
expression { params.isRelease }
}
The when
directive documentation actually has a very similar example in the expression
subsection for reference.
Answered By - Matt Schuchard Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.