Harish P C
Harish P C

Reputation: 127

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

I want to loop this two lists in Jenkinsfile and get the values with 1:1 mapping . My code is working but i can see repetitive entries in the output.

I have following two lists in Jenkinsfile

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

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

My output -

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 

With the above code i can loop the "app" & "env" list , Since i am looping based on list.size its looping 3*2 times and generating the result. But i need only 3 results

Expected Output -

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

Kindly help me on this code.

Upvotes: 0

Views: 1043

Answers (1)

Harish P C
Harish P C

Reputation: 127

I got this working after i changed the function as below -

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

Upvotes: 1

Related Questions