简体   繁体   English

管道语法:附加到现有文件而不使用 sh ""

[英]Pipeline syntax: append to an existing file without using sh ""

I'm trying to manipulate a terraform.tfvars file during deploy.我正在尝试在部署期间操作terraform.tfvars文件。

Code:代码:

         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}"
  }]

The above lines of code are ugly.上面的代码行很难看。 Is there an alternative way, such as using readFile function in groovy or anything else beside calling sh function Thanks是否有替代方法,例如在 groovy 中使用readFile函数或除了调用sh函数之外的任何其他方法谢谢

I think there might be solutions with other terraform features depending on your version, I'm struggling to see why this can't be solved by normal variables.我认为根据您的版本,可能有其他 terraform 功能的解决方案,我正在努力了解为什么普通变量无法解决这个问题。 Have you tried loading them via environment variables instead?您是否尝试过通过环境变量加载它们? You would just define the variables in the tfvars, and then they'd be supplied via TF_VARS_ENV_BUCKET like here .您只需在 tfvars 中定义变量,然后通过 TF_VARS_ENV_BUCKET 提供它们, 就像这里 You could just write the job params directly to envs to make sure they're present.您可以直接将作业参数写入 envs 以确保它们存在。

Found a more elegant way:找到了更优雅的方式:

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.

相关问题 如何使用 groovy/pipeline 脚本将新标签 append 添加到 yaml 文件中现有标签的列表中 - How to append the new tag to the list of existing tag in the yaml file using groovy/pipeline script Jenkins 管道中的 sh 命令在有和没有@NonCps 的情况下都失败 - sh command in Jenkins pipeline fails with and without @NonCps &#39;sh:sh:找不到命令&#39;在多分支管道中使用withCredentials块时 - 'sh: sh: command not found' When using withCredentials block in multibranch pipeline 在流水线 SH 脚本中使用 Jenkins 环境变量 - Using Jenkins Environment Variable in Pipeline SH script 是否可以将Jenkins管道中的sh DSL命令的所有输出发送到文件? - Is it possible to send all output of the sh DSL command in the Jenkins pipeline to a file? Jenkins Pipeline 无法在 Windows 从站中执行 SH 命令文件 - Jenkins Pipeline cannot execute SH command file in a Windows slave 从 Jenkins 管道执行 build.sh 文件 - Executing a build.sh file from a Jenkins pipeline 在詹金斯管道中使用嵌套命令替换sh步骤 - Using nested command substitution in jenkins pipeline sh step Jenkins管道在使用sh代码时得到NotSerializableException:WorkflowJob - Jenkins pipeline get NotSerializableException: WorkflowJob when using sh code 使用 Multibranch Pipeline Jenkins 作业,是否可以在不重新运行现有分支构建的情况下运行分支索引 - Using Multibranch Pipeline Jenkins job, is it possible to run branch indexing without re-running existing branch builds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM