![](/img/trans.png)
[英]How to append the new tag to the list of existing tag in the yaml file using groovy/pipeline script
[英]Pipeline syntax: append to an existing file without using sh ""
我正在嘗試在部署期間操作terraform.tfvars文件。
代碼:
dir("test123/${params.serviceName}/terraform"){
sh """
#!/bin/bash -e
cat <<EOF > ./terraform.tfvars
remote_data = [{
vpc_state = "${params.targetEnv}/vpc/terraform.state"
ecs_state = "${params.targetEnv}/ecs/terraform.state"
bucket = "${ENV_BUCKET}"
region = "${REGION}"
}]
上面的代碼行很難看。 是否有替代方法,例如在 groovy 中使用readFile
函數或除了調用sh
函數之外的任何其他方法謝謝
我認為根據您的版本,可能有其他 terraform 功能的解決方案,我正在努力了解為什么普通變量無法解決這個問題。 您是否嘗試過通過環境變量加載它們? 您只需在 tfvars 中定義變量,然后通過 TF_VARS_ENV_BUCKET 提供它們, 就像這里。 您可以直接將作業參數寫入 envs 以確保它們存在。
找到了更優雅的方式:
def remoteData(deploymentDir){
def data = """
remote_data = [{
vpc_state = "${TARGET_ENV}/vpc/terraform.state"
ecs_state = "${TARGET_ENV}/ecs/terraform.state"
bucket = "${ENV_BUCKET}"
region = "${ENV_BUCKET_REGION}"
}]
consul_address="${CONSUL_ADDRESS}"
vault_address="${VAULT_ADDRESS}"
service_name="${SERVICE_NAME}"
image_tag="${IMAGE_TAG}"
region="${REGION}"
deploy_role="${ARN}"
dashboard_folder="${SERVICE_NAME}-${TARGET_ENV}"
"""
writeFile file: "${WORKSPACE}/${deploymentDir}/terraform.tfvars", text: "${data}"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.