yaml - if 子句中的 gitlab yaml anchor 引用

标签 yaml gitlab-pipelines yaml-anchors

是否可能或有办法在 BASH if 子句中使用 yaml anchor 引用。 如果是这样,怎么办? 这就是我到目前为止正在尝试的。

create-cluster:
  needs: 
    - terra-bootstrap
  script:
    - export TF_VAR_state_bucket_prefix="${TF_VAR_vsad}/${TF_VAR_cluster_name}"
    - pushd terra-cluster
    - *init_with_gcs_state
    - |
      if [[ "${CLUSTER_EXISTS}" == 'false' ]]; then
       terraform apply -auto-approve
       *does_cluster_exist
      fi
    - popd
  stage: create-cluster
  tags:
    - gke

最佳答案

不可以,YAML 规范不允许您执行此操作。 YAML anchor 不能在字符串(或任何其他标量)内使用。

GitLab CI YAML 的更好方法是将可重用的 bash 步骤定义为函数,然后根据需要在作业脚本逻辑中使用这些步骤。

例如,您可以定义一个函数does_cluster_exist并使用anchors/reference/etc引用它。

.helper_functions:
  script: |
     function cluster_exists() {
         cluster="$1"
         
         # ... complete the function logic
     }

     function another_function() {
         return 0
     }

another_job:
  before_script:
    # ensure functions are defined
    - !reference [.helper_functions, script]
  script:
    # ...
    # use the functions in an `if` or wherever...
    - |
      if cluster_exists "$cluster"; then
         another_function
      else
         echo "fatal, cluster does not exist" > /dev/stderr
         exit 1
      fi

关于yaml - if 子句中的 gitlab yaml anchor 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71254402/

相关文章:

algorithm - 'n' 人使用 Go 的桥梁和 torch 问题

yaml - CloudFormation 提到架构版本 2.2 需要文档类型

php - yaml中的空字段

azure - 我可以通过 CLI 验证 azure-pipelines.yml 文件吗?

python - 覆盖率.misc.NoSource : No source for code: '/usr/src/server/rep-1>'

Gitlab ci 问题,使用触发器将工件传递到下游管道并需要关键字

gitlab - 有没有办法在gitlab中为一个组添加付款?