Jenkinsfile - Conditional stage execution in the Script Pipeline syntax

前端 未结 4 2081
南笙
南笙 2020-12-28 22:01

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 w

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 22:27

    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)
        })
    }
    

提交回复
热议问题