如何使用循环在 Jenkins groovy 函数中传递列表中的两个值

How to Pass two values from the list in the Jenkins groovy function with loop

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

我在 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 

请帮助我编写此代码。

我按如下方式更改功能后,我开始工作了 -

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 "
          """ 
   }
}