Jenkins Build Pipeline - Restart At Stage

后端 未结 7 1760
忘了有多久
忘了有多久 2020-12-02 14:55

I have the following build pipeline set up as a job:

Stage 1 - verify all dependencies exist
Stage 2 - build the new jar
Stage 3 - Run integration tests
Stag         


        
7条回答
  •  旧巷少年郎
    2020-12-02 15:01

    Here's another sketch to run stages conditionally without breaking the Stage View Plugin history.

    As they say:

    Dynamic stages: in general, if you want to visualize dynamically changing stages, make it conditional to execute the stage contents, not conditional to include the stage

    Here's what I've come up with so far. Seems to work mostly: (Just ignore the other dummy steps)

    We define a little conditionalStage helper function that neatly wraps up the stage name checking from the JP_STAGE Jenkins Job parameter.

    Notice how conditionalStage first opens the stage and then checks stageIsActive within the stage, just skipping all steps. This way, the Stage View Plugin sees all stages and soesn't mess up, but the stages' steps are still skipped.

    def stageSelect = JP_STAGE.toLowerCase()
    
    // test if stage or any of sub-stages is active
    def stageIsActive(theStage, theStages) { 
        // echo "pass: $theStages"
        // ARGL: https://issues.jenkins-ci.org/browse/JENKINS-26481
        // def lcStages = theStages.collect {it.toLowerCase()}
        def lcStages = []
        for (def s : theStages) { lcStages += s.toLowerCase() }
        def lcAllStages = lcStages + ['all']
        // echo "check: $lcAllStages"
        // echo JP_STAGE.toLowerCase()
        if (JP_STAGE.toLowerCase() in lcAllStages) {
            echo "Run: Stage '$theStage' is active through '$JP_STAGE'."
            return true
        } else {
            echo "Skip: Stage '$theStage' is NOT active through '$JP_STAGE'."
            return false
        }
    }
    
    // 1st element should be the stage, optionally followed by all sub-stages
    def conditionalStage(names, stageBody) {
      stage(names[0]) { if (stageIsActive(names[0], names)) {
        stageBody()
      }}  
    }
    
    timestamps {
    // --S--
    
    conditionalStage(['Intro']) { 
        echo 'Outside Node'
    
        build job: 'FreeX', wait: true
        sleep 3
    }
    
    // --S--
    conditionalStage(['AtNode', 'Hello', 'Done']) {
        node {
            // Cloudbees Enterprise Only: checkpoint 'Now'
            conditionalStage(['Hello']) {
                echo 'Hello World @ Node'
                sleep 4
            }
            conditionalStage(['Done']) {
                dir('C:/local') {
                    echo pwd()
                }
            }
        }
    }
    
    }//timestamps
    

提交回复
热议问题