Why an each loop in a Jenkinsfile stops at first iteration

前端 未结 4 828
终归单人心
终归单人心 2021-02-04 04:34

Here is the content of my Jenkinsfile :

node {
    // prints only the first element \'a\'
    [ \'a\', \'b\', \'c\' ].each {
        echo it
    }
}         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 04:44

    Here is example loop example with curl without NonCPS :

    #!/usr/bin/env groovy
    
    node('master') {
        stagesWithTry([
            'https://google.com/',
            'https://github.com',
            'https://releases.hashicorp.com/',
            'https://kubernetes-charts.storage.googleapis.com',
            'https://gcsweb.istio.io/gcs/istio-release/releases'
        ])
        stage ('ALlinOneStage'){
            stepsWithTry([
                'https://google.com/',
                'https://github.com',
                'https://releases.hashicorp.com/',
                'https://kubernetes-charts.storage.googleapis.com',
                'https://gcsweb.istio.io/gcs/istio-release/releases'
            ])
        }
    }
    //loop in one stage
    def stepsWithTry(list){
        for (int i = 0; i < list.size(); i++) {
            try {
            sh "curl --connect-timeout 15 -v -L ${list[i]}"
            } catch (Exception e) {
                echo "Stage failed, but we continue"
            }
        }
    }
    //loop in multiple stage
    def stagesWithTry(list){
        for (int i = 0; i < list.size(); i++) {
            try {
                stage(list[i]){
              sh "curl --connect-timeout 15 -v -L ${list[i]}"
                }
            } catch (Exception e) {
                echo "Stage failed, but we continue"
            }
        }
    }
    
    

提交回复
热议问题