【问题标题】:How to Pass two values from the list in the Jenkins groovy function with loop如何通过循环从Jenkins groovy函数中的列表中传递两个值
【发布时间】:2020-07-31 06:00:09
【问题描述】:

我想在 Jenkinsfile 中循环这两个列表并使用 1:1 mapping 获取值。我的代码正在运行,但我可以在输出中看到重复的条目。

我在 Jenkinsfile 中有以下两个列表

app = ["app1","app2","app3"]
env = ["prod1","prod2","prod3"]

我的 Jenkinsfile-

    #!/usr/bin/env groovy
    @Library(['jenkinsGlobalLibrary@master']) _
    app = ["app1","app2","app3"]
    env = ["prod1","prod2","prod3"]
    (branchType, branchName) = env.BRANCH_NAME.tokenize('/')
    
    node('java180u161-maven325-pythonanaconda352') {
    
        stage ( 'Checkout' ) {
    
            checkout scm
        }
    
        stage ('Generating list environment wise'){
             pull_from_dev(app,env)
        }
    
    def pull_from_dev(app,env) {
        sh "echo Going to echo a list"
        for (int i = 0; i < app.size(); i++) {
             for (int j = 0; j < env.size(); j++) {
            sh """
            echo "Retrieving   ${app[i]} of ${env[j]} properties "
            """   
      }  }
}

我的输出 -

Retrieving app1 of prod1 properties 
Retrieving app1 of prod1 properties 
Retrieving app1 of prod1 properties 
Retrieving app2 of prod2 properties 
Retrieving app2 of prod2 properties 
Retrieving app2 of prod2 properties 
Retrieving app3 of prod3 properties 
Retrieving app3 of prod3 properties 
Retrieving app3 of prod3 properties 

使用上面的代码,我可以循环“app”和“env”列表,因为我基于 list.size 循环,它循环 3*2 次并生成结果。但我只需要 3 个结果

预期输出 -

Retrieving app1 of prod1 properties 
Retrieving app2 of prod2 properties 
Retrieving app3 of prod3 properties 

请在此代码上帮助我。

【问题讨论】:

    标签: for-loop groovy jenkins-pipeline nested-for-loop


    【解决方案1】:

    在我改变了如下功能后我得到了这个工作-

    def pull_from_dev(app,env) {
        sh "echo Going to echo a list"
            for (int i=0; i < app.size(); i++) {
            sh """
            echo "Retrieving  ${app[i]} of ${env[i]} properties "
              """ 
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-09
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      相关资源
      最近更新 更多