A bit old topic but since Jenkins still (!) doesn't support this I'm sending another solution for scripted pipeline implementations.
It's based on building stages list dynamically when running pipeline.
- step - stages definition enum
enum Steps {
PREPARE(0, "prepare"),
BUILD(1, "build"),
ANALYSE(2, "analyse"),
CHECKQG(3, "checkQG"),
PROVISION(4, "provision"),
DEPLOY(5, "deploy"),
ACTIVATE(6, "activate"),
VERIFY(7, "verify"),
CLEANUP(8, "cleanup")
Steps(int id, String name) {
this.id = id
this.name = name
}
private final int id
private final String name
int getId() {
id
}
String getName() {
name
}
public static Steps getByName(String name) {
println "getting by name " + name
for(Steps step : Steps.values()) {
if(step.name.equalsIgnoreCase(name)) {
return step
}
}
throw new IllegalArgumentException()
}
}
- method creating the final steps list
def prepareStages(def startPoint){
println "preparing build steps starting from " + startPoint
Set steps = new LinkedHashSet()
steps.add(Steps.PREPARE)
steps.add(Steps.BUILD)
steps.add(Steps.ANALYSE)
steps.add(Steps.CHECKQG)
steps.add(Steps.PROVISION)
steps.add(Steps.DEPLOY)
steps.add(Steps.ACTIVATE)
steps.add(Steps.VERIFY)
steps.add(Steps.CLEANUP)
List finalSteps = new ArrayList()
steps.each{
step ->
if (step.id >= startPoint.id) {
finalSteps.add(step)
}
}
return finalSteps
}
- and u can use it like this
def stages = prepareStages(Steps.getByName("${startStage}"))
node {
try {
//pipelineTriggers([pollSCM('${settings.scmPoolInterval}')]) //this can be used in future to get rid build hooks
sh "echo building " + buildVersionNumber(${settings.isTagDriven})
tool name: 'mvn_339_jenkins', type: 'maven'
script {
println "running: " + stages
}
stage('Prepare') {
if (stages.contains(Steps.PREPARE)) {
script { currentStage = 'Prepare' }
//.....
}
} //...
the "startStage" is a build parameter defined as follows
parameters {
choiceParam('startStage', [
'prepare',
'build',
'analyse',
'checkQG',
'provision',
'deploy',
'activate',
'verify',
'cleanup'
], 'Pick up the stage you want to start from')
}
This allows me to pick up the stage I want to start the pipeline from (prepare stage is set by default)