Home > Mobile >  How can I add the ability to start from a certain stage in a jenkins pipeline?
How can I add the ability to start from a certain stage in a jenkins pipeline?

Time:10-20

In the example below, I'd like to have the ability to choose, from a parameter, to start the job on any of the stages. the 'when' conditional does help, but I don't know how to have it start at say stage2, and then complete all the stages after it. Currently as it is below, it will only do the stage that is selected for the START_AT parameter and skip all of the others.

pipeline {
    agent any
    parameters {
        choice(name: 'START_FROM', choices: ['stage1', 'stage2', 'stage3'], description: 'Choose which step to start the job on.')
    }
    stages {
        stage('stage1') {
            when {
                expression { params.START_FROM == 'stage1' }
            }
            steps {
                echo "stage1 executing"
            }
        }

        stage('stage2') {
            when {
                expression { params.START_FROM == 'stage2' }
            }
            steps {
                echo "stage1 executing"
            }
        }

        stage('stage3') {
            when {
                expression { params.START_FROM == 'stage3' }
            }
            steps {
                echo "stage1 executing"
            }
        }
    }
}

CodePudding user response:

You may have to get clever with your when{}s, which is a pain. First having a param for "which stage to start" is going to be the first way to indicate where the pipeline will begin. For this example let's call it stageStart

Then you can have another flag that indicates whether one can "continue". Let's call it continueStages.

For each of your stages, within your when{} you can format it like this:

when {expression {return "${stageStart}" == "stage2" || "${continueStages}" == "true"}}

Then the logic here will be you set continueStage to false. When you enter a stage, set that var to true. The first stage will not be entered unless the stageStart matches the stage name. Then every stage after will be entered. It'll look like this:

continueStages = "false"
pipeline {
agent any
parameters {
    choice(name: 'START_FROM', choices: ['stage1', 'stage2', 'stage3'], description: 'Choose which step to start the job on.')
}
stages {
    stage('stage1') {
        when {
            expression { return {"${stageStart}" == "stage1"} }
        }
        steps {
            continueStages = "true"
            echo "stage1 executing"
        }
    }

    stage('stage2') {
        when {
            expression { return {"${stageStart}" == "stage2" || "${continueStages}" == "true"}}
        }
        steps {
            continueStages = "true"
            echo "stage1 executing"
        }
    }

    stage('stage3') {
        when {
            expression { return {"${stageStart}" == "stage3" || "${continueStages}" == "true"} }
        }
        steps {
            continueStages = "true"
            echo "stage1 executing"
        }
    }
}


}

Addendum, I'm imagining setting this as a param you can set in Jenkins upon building. If you're implementing it a different way, this logic will still work, you'll just change up your syntax (like don't use return{})

  • Related