We have a Jenkins pipeline setup using the Jenkinsfile to define what needs to be done for the different builds.
In a lot of our stages, we have some work that needs to be done when we're going to be doing a release, but can be skipped if a release isn't going to be done.
Currently we have this code:
when {
allOf {
anyOf {
branch 'master';
branch 'develop';
}
expression {
return params.DBTESTS
}
}
}
repeated across all of the stages that can be skipped.
How would I convert that into a function, so that the logic is in one place similar to something like this:
def isReleasePlanned() {
return allOf {
anyOf {
branch 'master';
branch 'develop';
}
expression {
return params.DBTESTS
}
}
}
}
and then used like this:
when {
isReleasePlanned()
}
In each of the stages. Currently that is a syntax error when Jenkins tries to read the Jenkinsfile.
You could try something like
def isReleasePlanned(branch, params){
return branch ==~ "master|develop" && params.DBTESTS
}
pipeline {
...
stages {
...
stage("For release"){
when {
expression{ isReleasePlanned(GIT_BRANCH, params) }
}
}
}
...
}
Or export the function from a custom library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With