1

As stated in the title, I'm attempting to loop over an ArrayList of strings in a Jenkins Groovy Pipeline script (using scripted Pipeline syntax). Let me lay out the entire "problem" for you.

I start with a string of filesystem locations separated by spaces: "/var/x /var/y /var/z ... " like so. I loop over this string adding each character to a temp string. And then when I reach a space, I add that temp string to the array and restart. Here's some code showing how I do this:

def full_string = "/var/x /var/y /var/z"
def temp = ""
def arr = [] as ArrayList
full_string.each {
    if ( "$it" == " " ) {
      arr.add("$temp")           <---- have also tried ( arr << "$temp" )
      temp = ""
    } else {
      temp = "$temp" + "$it"
    }
}
 // if statement to catch last element

See, the problem with this is that if I later go to loop over the array it decides to loop over every individual char instead of the entire /var/x string like I want it to.

I'm new to Groovy so I've been learning as I build this pipeline. Using Jenkins version 2.190.1 if that helps at all. I've looked around on SO and Groovy docs, as well as the pipeline syntax docs on Jenkins. Can't seem to find what I've been looking for. I'm sure that my solution is not the most elegant or efficient, but I will settle for understanding how it works first before trying to squeeze the most performance out of it.

I found this question but this was similarly unhelpful: Dynamically adding elements to ArrayList in Groovy.

Edit: I'm trying to translate old company c-shell build scripts into Jenkins Pipelines. My initial string is an environment variable available on all our nodes that I also need to have available inside the Pipeline.

TL;DR - I need to be able to create an array from space separated values in a string, and then be able to loop over said array and each "element" be a complete string instead of a single char so that I can run pipeline steps properly.

3
  • it kind of sounds like you are trying to convert your string into an array with a space delimiter. Is that correct? Commented Apr 10, 2020 at 18:57
  • @MattSchuchard Yes, in a way. The string was originally an environment variable (in the script I'm trying to translate from cshell) and shell allows you to loop over strings with space delimiters. Groovy not so much Commented Apr 10, 2020 at 19:00
  • Ok, could you clarify your question? Your title, your description, and your code all seem to be conflicting. All three of those are certainly possible though. Commented Apr 10, 2020 at 19:03

1 Answer 1

3

Try running this in your Jenkins script console (your.jenkins.url.yourcompany.com/script):

def full_string = "/var/x /var/y /var/z"
def arr = full_string.split(" ")
for (i in arr) {
  println "now got ${i}"
}

Result:

now got /var/x
now got /var/y
now got /var/z
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I attempted your solution and it partially worked. (didn't know there was a split function so that's good to know). I have separated my pipeline into 5 stages (init, set-up, build, deploy, clean) and do as much of my variable setting in the init stage. I tried the split function there and immediately echo'd it out to the console in init and it worked as expected, then when I did another echo in my setup stage, it went back to printing every letter instead of every string. Is there some behaviour between stages that I need to look into?
It looks like this is a known behaviour of Jenkins Pipelines according to this ticket: issues.jenkins-ci.org/browse/JENKINS-41335. If this is the case, how would I access my array from a later stage?
This didn't completely work unfortunately but I was able to use the full_string.split() in my solution. My solution was to comment out the variables in the init stage and declare/initialize them in the stages in which they are used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.