I recently came across this problem and was able to come up with a solution that should cater to all possibilities of array inputs.
What I mean by that is GitLab CI input arrays are a bit odd. They're a little like JSON arrays, except they allow values that aren't wrapped in quotes as well as those unquoted values having spaces. For example, the following is totally valid:
my_array_values:
- value_1
- "value 2"
- value 3
- "value 4"
This will cause $[[ inputs.my_array_values ]]
to hold the following value:
'[value_1, "value 2", value 3, "value 4"]'
Hence, we can't use jq
since it isn't valid JSON. You could enforce the use of ""
wrapped values within your team / organisation and then use jq
. However, if you want a more robust solution you could use this abomination I cooked up:
script:
-|
readarray -t tags < <(echo $[[ inputs.tags ]] |
sed '
s/[][]//g; # remove brackets
s/,\s\+/,/g; # remove spaces after commas
s/,/\n/g; # remove commas
s/\"//g; # remove double quotes
s/ /\\ /g; # escape spaces
' |
xargs printf "%s\n")
)
for value in ${my_array_values[@]}; do
echo "$value"
done
If anyone that's good at bash has a better way of doing this please let me know