.gitlab-ci.yml yaml 内的数组变量

Vas*_*sya 14 gitlab gitlab-ci gitlab-ci.yml

我想在 gitlab ci/cd yml 文件的变量中使用数组,如下所示:

variables:
    myarrray: ['abc', 'dcef' ]
....
script: |
    echo myarray[0]  myarray[1]
Run Code Online (Sandbox Code Playgroud)

但 Lint 告诉我该文件不正确:

variables config should be a hash of key value pairs, value can be a hash
Run Code Online (Sandbox Code Playgroud)

我已经尝试过下一个:

variables:
    arr[0]: 'abc'
    arr[1]: 'cde'
....
script: |
    echo $arr[0] $arr[1]
Run Code Online (Sandbox Code Playgroud)

但构建失败并打印出 bash 错误:

bash: line 128: export: `arr[0]': not a valid identifier
Run Code Online (Sandbox Code Playgroud)

有没有办法在 .gitlab-ci.yml 文件中使用数组变量?

Ber*_*rte 24

根据文档,这是你应该做的:

无法创建作为值数组的 CI/CD 变量,但您可以使用 shell 脚本技术来实现类似的行为。

例如,您可以在变量中存储用空格分隔的多个变量,然后使用脚本循环遍历这些值:

job1:
  variables:
    FOLDERS: src test docs
  script:
    - |
      for FOLDER in $FOLDERS
        do
          echo "The path is root/${FOLDER}"
        done
Run Code Online (Sandbox Code Playgroud)