Reputation: 6883
I want to use arrays in variables of my gitlab ci/cd yml file, something like that:
variables:
myarrray: ['abc', 'dcef' ]
....
script: |
echo myarray[0] myarray[1]
But Lint tells me that file is incorrect:
variables config should be a hash of key value pairs, value can be a hash
I've tried the next:
variables:
arr[0]: 'abc'
arr[1]: 'cde'
....
script: |
echo $arr[0] $arr[1]
But build failed and prints out bash error:
bash: line 128: export: `arr[0]': not a valid identifier
Is there any way to use array variable in .gitlab-ci.yml file?
Upvotes: 22
Views: 40754
Reputation: 151
To expand Bernardo Duarte answer:
Variables can also be set this way:
job1:
variables:
FOLDERS:
src
test
docs
another_folder
script:
- |
for FOLDER in $FOLDERS
do
echo "The path is root/${FOLDER}"
done
This however dowes not work as expected ATM:
test:
variables:
stuff:
this
that
another\ one
"also another one"
script:
- |
for s in $stuff; do
echo test: $s
done
result:
...
Executing "step_script" stage of the job script
$ for s in $stuff; do # collapsed multi-line command
test: this
test: that
test: another\
test: one
test: "also
test: another
test: one"
Cleaning up project directory and file based variables
Job succeeded
Upvotes: 0
Reputation: 372
Another approach you could follow is two use a matrix of jobs that will create a job per array entry.
deploystacks:
stage: deploy
parallel:
matrix:
- PROVIDER: aws
STACK: [monitoring, app1]
- PROVIDER: gcp
STACK: [data]
tags:
- ${PROVIDER}-${STACK}
Here is the Gitlab docs regarding matrix https://docs.gitlab.com/ee/ci/jobs/job_control.html#run-a-one-dimensional-matrix-of-parallel-jobs
Upvotes: 2
Reputation: 6883
After some investigations I found some surrogate solution. Perhaps It may be useful for somebody:
variables:
# Name of using set
targetType: 'test'
# Variables set test
X_test: 'TestValue'
# Variables set dev
X_dev: 'DevValue'
# Name of variable from effective set
X_curName: 'X_$targetType'
.....
script: |
echo Variable X_ is ${!X_curName} # prints TestValue
Upvotes: 2
Reputation: 4264
According to the docs, this is what you should be doing:
It is not possible to create a CI/CD variable that is an array of values, but you can use shell scripting techniques for similar behavior.
For example, you can store multiple variables separated by a space in a variable, then loop through the values with a script:
job1:
variables:
FOLDERS: src test docs
script:
- |
for FOLDER in $FOLDERS
do
echo "The path is root/${FOLDER}"
done
Upvotes: 29