Jenkins Build Pipeline - Restart At Stage

后端 未结 7 1751
忘了有多久
忘了有多久 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 14:59

    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.

    1. 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()
        }
    }
    
    1. 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
        }
    
    1. 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)

提交回复
热议问题