Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array variable inside .gitlab-ci.yml yaml

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?

like image 578
Vasya Avatar asked Sep 12 '25 17:09

Vasya


2 Answers

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
like image 190
Bernardo Duarte Avatar answered Sep 15 '25 13:09

Bernardo Duarte


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
like image 26
Vasya Avatar answered Sep 15 '25 13:09

Vasya