Depending on your use-case then IMHO when {}
is currently the best solution to this problem. Just skip all stages when the parameters aren't set correctly and do the validation/aborting/triggering error of each parameter required within steps{}
then or just let the command there failing.
Example:
pipeline {
agent any
parameters {
string(name: 'FOO', defaultValue: '', description: 'FUBAR', trim: true)
}
stages {
stage('Test') {
when {
anyOf {
triggeredBy 'BuildUpstreamCause'
expression { return params.FOO }
}
}
steps {
script {
if (params.FOO.length() < 8) {
error("Parameter FOO must be at least 8 chars")
}
}
echo 'Hello World'
}
}
}
}
It's of cause a bit annoying when you need to do this on multiple steps, but at least you can move it into a function:
def stageWhen() {
return (triggeredBy('BuildUpstreamCause') || params.FOO)
}
def validateParameterFoo() {
if (params.FOO.length() < 8) {
error("Parameter FOO must be at least 8 chars")
}
}
pipeline {
agent any
parameters {
string(name: 'FOO', defaultValue: '', description: 'FUBAR', trim: true)
}
stages {
stage('Test') {
when {
expression { return stageWhen() }
}
steps {
script {
validateParameterFoo()
}
echo 'Hello World'
}
}
stage('Test 2') {
when {
expression { return stageWhen() }
}
steps {
script {
validateParameterFoo()
}
echo 'Hello World'
}
}
}
}