Issue in pipeline step of jenkinsfile

42 views
Skip to first unread message

spratap singh

unread,
Apr 16, 2021, 11:26:15 AM4/16/21
to Jenkins Users
registeries = [1,2,3,4] 
 Map tasks = [:]
 for (j=0;j<=registeries.size();j++) 
 tasks["Code ${j}"] = { -> sh "touch ${j}" }
 } 
 parallel(tasks) 
 sh "ls -lrt"

 

Above code should create files 0,1,2,3,4,5

but the last line ls -lrt only prints 5 which means it is only taking the last iteration value in all values of map tasks

Can anyone tell what I am doing wrong here ?

Devin Nusbaum

unread,
Apr 16, 2021, 11:37:35 AM4/16/21
to Jenkins Users
This is a classic issue that a lot of people run into in various programming languages. https://groovyconsole.appspot.com/script/5078409050849280 shows the same problem and one way to fix it (click "edit in console" and then you can execute the script). This kind of issue is why in Java, anonymous classes and lambda expressions can only reference variables that are final or effectively final.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/bfde4364-d93d-4b7a-9ff6-7b2e7bd3c7a2n%40googlegroups.com.

spratap singh

unread,
May 11, 2021, 8:59:56 AM5/11/21
to jenkins...@googlegroups.com
Hey Devin,
Sorry for the late reply , I was down with covid for some time.
the code that you have given i tried and it works , but how to handle the string parameters ?
some string parameters which I am assigning they are also interfering with each other


You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/ao0SUF9L0H4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/FABC5220-69E1-449A-AA08-5C0BA770D025%40cloudbees.com.


--
Shashipratap Singh

Björn Pedersen

unread,
May 12, 2021, 7:34:14 AM5/12/21
to Jenkins Users
You need to add an intermediate var in your first loop and use this in the sh step
Reply all
Reply to author
Forward
0 new messages