是否可能或有办法在 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/