I use Jenkins for k6 tests CI/CD. In my pipeline I have the code:
def users = ["user1","user2","user3"]
sh """
export MY_USERS=${users.join(' ')}
./start.sh
"""
In my bash script I have:
SCRIPT_USERS=$MY_USERS
for user in $MY_USERS; do
eval "$PARAMS $K6 run $user.js -o xk6-influxdb \
--out web-dashboard=port=0\&report=$DIR/${user}.html" &
done
However, in logs I got SCRIPT_USERS=user1
How can I get my values in a bash variable? I need all the values. Tried different ways of passing the variables but no worked.
In the Groovy script, you are constructing a script dynamically. The equivalent after users.join(' ')
is executed is
export MY_USERS=user1 user2 user3
which exports the name MY_USERS
after it is assigned the value user1
, but also exports the undefined names user2
and user3
.
You need to quote ${users.join(' ')}
so that the entire result is part of the single assignment to MY_USERS
.
def users = ["user1","user2","user3"]
sh """
export MY_USERS="${users.join(' ')}"
./start.sh
"""
Your actual bash
script is more or less fine. SCRIPT_USERS
isn't necessary; you can just use MY_USERS
directly as you are anyway. eval
may not be necessary, and you should work to avoid it if you can. There is a general issue of relying on a space-separated string to correctly represent an array of arbitrary strings, but assuming your user names are "simple" (nothing except alphanumeric characters), you can get away with it.