简体   繁体   中英

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

I'm trying to manipulate a terraform.tfvars file during deploy.

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

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. 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 . You could just write the job params directly to envs to make sure they're present.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM