Search code examples
jenkinsjenkins-pipelinejenkins-groovyjenkins-job-dsl

Jenkins stage for loop not continuing if for loop fails


I have a Jenkins stage requirement is for loop should continue even it fails on 1st index value. I tried below code but the for loop doesn't continue if REPO_LIST failed on "A". Any suggestions. Thanks for the help in advance.

stages {
    stage('A') {
        when {
            expression {params.ENV == true}
        }
        steps {
            script {
             try {
                              
               def REPO_LIST = [ "A", "B" ]
               
               for ( REPO in REPO_LIST ) {
                    CODE xxxxxxxx
                 }
               }   catch(all) {
                      println "hello"
                      continue;
         }
      }
    }
  }
}

Solution

  • You need to have the try-catch block within your loop surrounding the CODE xxxx. Have look at the below.

    stages {
        stage('A') {
        when {
            expression {params.ENV == true}
        }
          steps {
              script {              
                 def REPO_LIST = [ "A", "B" ]
                 
                 for ( REPO in REPO_LIST ) {
                      try {
                        CODE xxxxxxxx
                        } catch(all) { println "hello"}
                    }
              }
           }
       }
    }