Issue
We are using the Script Pipeline syntax for our Jenkinsfile
which has a lot of stage defined to build and deploy our code. We have a use case where I want to run all my stages if I am doing a Full Build but only run one specific stage if I need to perform some AWS routing. I know I can use the if(<expression>)
to skip a stage or run a stage. Problem is I don't want to apply that if
condition to every stage in my Jenkinsfile
.
In the new Declarative Pipeline syntax this is easily possible using the stage..when
option. We have a lot of custom Groovy helper function used in our infrastructure so it is not possible at this point for me to switch from the Script Pipeline syntax to the new Declarative Pipeline syntax. What I ended up doing on my existing Jenkinsfile
is something like this..
Original Jenkinsfile
stage('Checkout Code') {}
stage('Build') {}
parallel(
stage('Sonar Analysis') {}
stage('CLM Analysis') {}
stage('Security Analysis') {}
)
stage('Build Docker Image') {}
...
...
stage('QA Deploy') {}
stage('QA Routing') {}
...
...
stage('Prod Deploy') {}
stage('Prod Routing') {}
Changed to
if (fullDeploy){
stage('Full Build') {
stage('Checkout Code') {}
stage('Build') {}
parallel(
stage('Sonar Analysis') {}
stage('CLM Analysis') {}
stage('Security Analysis') {}
)
stage('Build Docker Image') {}
...
...
stage('QA Deploy') {}
stage('QA Routing') {}
...
...
stage('Prod Deploy') {}
stage('Prod Routing') {}
}
}
if (routeOnly){
stage('Prod Routing') {}
}
This feels a little hacky to me and I couldn't find any things on the Jenkins docs that provides a good way to do this.
Has anyone got a better way in which I can incorporate this?
Solution
I also disliked the idea of having a redundant if{} block inside my stage{}. I solved this by overwriting the stage as follows
def stage(name, execute, block) {
return stage(name, execute ? block : {echo "skipped stage $name"})
}
now you can disable a stage as follows
stage('Full Build', false) {
...
}
Update You could also mark stage skipped using below def
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
def stage(name, execute, block) {
return stage(name, execute ? block : {
echo "skipped stage $name"
Utils.markStageSkippedForConditional(STAGE_NAME)
})
}
Answered By - culmat