Quantcast

Iteration over list or map in Pipeline script

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Iteration over list or map in Pipeline script

Jenkins users mailing list
Hello,

I have a freestyle job with one step of the kind "Execute Groovy Script". Its Groovy code is

def x = ['a', 'b', 'c']
println x
x
.each { println it }


The output of this job is (not surprinsingly):
[Test-Groovy2] $ groovy /var/lib/jenkins/workspace/Test-Groovy2/hudson3825239812036801886.groovy
[a, b, c]
a
b
c
Finished: SUCCESS

But if I create a pipeline job with the pipeline script set to the same Groovy code, its output is:
[Pipeline] echo
[a, b, c]
[Pipeline] echo
a
[Pipeline] End of Pipeline
Finished: SUCCESS

The .each() gets the first element in the list and nothing more.
What's going on here? I thought that pipeline scripts are just Groovy plus some pipeline DSL, but I seem to be wrong.

BTW: my Jenkins is 2.27.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ca2dd238-550f-46a0-8f56-1bc167402eb8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Iteration over list or map in Pipeline script

slide
Pipeline is a SUBSET of Groovy, not a superset. There are things (like iterators) that are not serializable, so they can't be used in a pipeline. You can create a method that is marked with @NonCPS to do things that are not serialized, but sometimes it is difficult to get back out what you need without the full Groovy functionality. You need to switch to using C style loops.

On Mon, Mar 13, 2017 at 7:26 AM 'Martin Schmude' via Jenkins Users <[hidden email]> wrote:
Hello,

I have a freestyle job with one step of the kind "Execute Groovy Script". Its Groovy code is

def x = ['a', 'b', 'c']
println x
x
.each { println it }


The output of this job is (not surprinsingly):
[Test-Groovy2] $ groovy /var/lib/jenkins/workspace/Test-Groovy2/hudson3825239812036801886.groovy
[a, b, c]
a
b
c
Finished: SUCCESS

But if I create a pipeline job with the pipeline script set to the same Groovy code, its output is:
[Pipeline] echo
[a, b, c]
[Pipeline] echo
a
[Pipeline] End of Pipeline
Finished: SUCCESS

The .each() gets the first element in the list and nothing more.
What's going on here? I thought that pipeline scripts are just Groovy plus some pipeline DSL, but I seem to be wrong.

BTW: my Jenkins is 2.27.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ca2dd238-550f-46a0-8f56-1bc167402eb8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVfS8Z2ceE%3Dok93rBxVzWfhQv5LxSRx0k-kRXS8Ozx1mRQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Iteration over list or map in Pipeline script

Bill Dennis
In reply to this post by Jenkins users mailing list
You can do it with a for loop. There are issues using Groovy iterators like each {}. Try something like this:

pipeline {
    agent any
   
    stages
{
        stage
('loop') {
            steps
{
                script
{
                   
def x = ['a', 'b', 'c']
                    println x
                   
for(String item: x) {
                        println item
                   
}
               
}
           
}
       
}
   
}
}


On Monday, 13 March 2017 14:26:44 UTC, Martin Schmude wrote:
Hello,

I have a freestyle job with one step of the kind "Execute Groovy Script". Its Groovy code is

def x = ['a', 'b', 'c']
println x
x
.each { println it }


The output of this job is (not surprinsingly):
[Test-Groovy2] $ groovy /var/lib/jenkins/workspace/Test-Groovy2/hudson3825239812036801886.groovy
[a, b, c]
a
b
c
Finished: SUCCESS

But if I create a pipeline job with the pipeline script set to the same Groovy code, its output is:
[Pipeline] echo
[a, b, c]
[Pipeline] echo
a
[Pipeline] End of Pipeline
Finished: SUCCESS

The .each() gets the first element in the list and nothing more.
What's going on here? I thought that pipeline scripts are just Groovy plus some pipeline DSL, but I seem to be wrong.

BTW: my Jenkins is 2.27.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/de886784-ca86-4872-a310-0924a7a25d9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Iteration over list or map in Pipeline script

Jenkins users mailing list
In reply to this post by Jenkins users mailing list
Thank you all for the clarification.


Am Montag, 13. März 2017 15:26:44 UTC+1 schrieb Martin Schmude:
Hello,

I have a freestyle job with one step of the kind "Execute Groovy Script". Its Groovy code is

def x = ['a', 'b', 'c']
println x
x
.each { println it }


The output of this job is (not surprinsingly):
[Test-Groovy2] $ groovy /var/lib/jenkins/workspace/Test-Groovy2/hudson3825239812036801886.groovy
[a, b, c]
a
b
c
Finished: SUCCESS

But if I create a pipeline job with the pipeline script set to the same Groovy code, its output is:
[Pipeline] echo
[a, b, c]
[Pipeline] echo
a
[Pipeline] End of Pipeline
Finished: SUCCESS

The .each() gets the first element in the list and nothing more.
What's going on here? I thought that pipeline scripts are just Groovy plus some pipeline DSL, but I seem to be wrong.

BTW: my Jenkins is 2.27.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/b3fb9d74-feb4-448c-841d-559a7a33ad32%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Iteration over list or map in Pipeline script

Baptiste MATHUS
In reply to this post by Bill Dennis
It might have been fixed, I didn't check very recently, but not long ago that kind of loop (Java 5 style) was not working either. So either double check or use c-style loops only in general.

Le 14 mars 2017 1:03 AM, "Bill Dennis" <[hidden email]> a écrit :
You can do it with a for loop. There are issues using Groovy iterators like each {}. Try something like this:

pipeline {
    agent any
   
    stages
{
        stage
('loop') {
            steps
{
                script
{
                   
def x = ['a', 'b', 'c']
                    println x
                   
for(String item: x) {
                        println item
                   
}
               
}
           
}
       
}
   
}
}


On Monday, 13 March 2017 14:26:44 UTC, Martin Schmude wrote:
Hello,

I have a freestyle job with one step of the kind "Execute Groovy Script". Its Groovy code is

def x = ['a', 'b', 'c']
println x
x
.each { println it }


The output of this job is (not surprinsingly):
[Test-Groovy2] $ groovy /var/lib/jenkins/workspace/Test-Groovy2/hudson3825239812036801886.groovy
[a, b, c]
a
b
c
Finished: SUCCESS

But if I create a pipeline job with the pipeline script set to the same Groovy code, its output is:
[Pipeline] echo
[a, b, c]
[Pipeline] echo
a
[Pipeline] End of Pipeline
Finished: SUCCESS

The .each() gets the first element in the list and nothing more.
What's going on here? I thought that pipeline scripts are just Groovy plus some pipeline DSL, but I seem to be wrong.

BTW: my Jenkins is 2.27.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/de886784-ca86-4872-a310-0924a7a25d9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CANWgJS4Aa8bSo4bDm%3Dp6ko9-%3DMD8o47oMu-BGe3s%3D8uEn2U89Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...