Home > front end >  Restart the pipeline from a particular stage in jenkins scripted pipeline
Restart the pipeline from a particular stage in jenkins scripted pipeline

Time:06-16

I have 4 different stages(build test deploy monitor) in my pipeline which is build using scripted pipeline. Pipeline runs till stage 3 completion and then waits for user input. Based on user input, I need to restart from stage 2 if the user confirms with pressing proceed after stage 3. is there any way to achieve this in scripted pipeline?

CodePudding user response:

You can do something like the below to cater your requirement.

node {
    def mvnHome
    def flag = false
    stage('Stage 1') {
       echo "This stage executed always"
    }
    // Lets execute stage 2
    generateStage2AndCall3("First Execution").call()
    
    stage('Get User Input') {
        echo "Getting user Input"
        input(message: "Should we continue?")
        generateStage2AndCall3("Second Execution").call()
    }
    
    stage('Stage 4') {
        echo "Result"
    }
}

def generateStage2AndCall3(name) {
    return {
        stage("Stage 2 ${name}") {
            echo "Stage 2 ${name}"
            generateStage3(name).call()
        }
    }
}

def generateStage3(name) {
    return {
        stage("Stage 3 ${name}") {
            echo "Stage 3 ${name}"
        }
    }
}

The execution will look like the below.

enter image description here

  • Related