How to iterate over multiple-values parameter in Free-style project shell script

In my Free-style Project Jenkins job I have a parameter called “COMPONENETS” that takes one or multiple strings divided by space.

If I use this parameter passed as single string (first) in “Execute shell” stage I can achieve what I need (/path/to/first-*.tar.gz will be handled):

<DO SOMETHING WITH> /path/to/${COMPONENETS}-*.tar.gz

but when I try to update my Job with handling, for instance, string with two values (first second):

for COMPONENT in $(echo "${COMPONENTS}"); do
    <DO SOMETHING WITH> /path/to/$COMPONENT-*.tar.gz
done

that doesn’t work as values seem to be empty - I got error

'/path/to/-*.tar.gz': No such file or directory
'/path/to/-*.tar.gz': No such file or directory

I also tried simple for COMPONENT in ${COMPONENTS} but that also didn’t work. Both approaches work from Ubuntu Terminal, but not in Jenkins… :slightly_frowning_face:

So how should I iterate through list parameter on Jenkins correctly?

I would check if your variable is set - try running

echo  ${COMPONENTS}

you can also do:

env | sort

to see all variables set.

if that produces output, then your problem is not with Jenkins but with shell scripting. If it does not produce output, your problem is setting the env variables from parameters. I have not used Freestyle jobs in eons, not sure how to do it, but I am sure there is a way.

If it is a shell problem and your COMPONENTS holds whitespace separate list with no whitespaces inside it - this should work in most shells (no need to play games with subshells):

for COMPONENT in ${COMPONENTS}; do
  ...
done

HTH

Yes, variable is definitely set cause I’m using it early in the code. It doesn’t work in loop only… and for COMPONENT in ${COMPONENTS} I’ve already tried…

add set -x as first command in the script (or at least before the for loop) to see what shell is actually doing

make sure you are doing for COMPONENT in ${COMPONENTS} and not for COMPONENT in "${COMPONENTS}" - also you may need to ensure your IFS is set correctly (if you are messing with it)