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

2024-05-11

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

variables:
    myarrray: ['abc', 'dcef' ]
....
script: |
    echo myarray[0]  myarray[1]

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

variables config should be a hash of key value pairs, value can be a hash

我已经尝试过下一个:

variables:
    arr[0]: 'abc'
    arr[1]: 'cde'
....
script: |
    echo $arr[0] $arr[1]

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

bash: line 128: export: `arr[0]': not a valid identifier

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


根据docs https://docs.gitlab.com/ee/ci/variables/#store-multiple-values-in-one-variable,这就是你应该做的:

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

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

job1:
  variables:
    FOLDERS: src test docs
  script:
    - |
      for FOLDER in $FOLDERS
        do
          echo "The path is root/${FOLDER}"
        done
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.gitlab-ci.yml yaml 内的数组变量 的相关文章

随机推荐