简体   繁体   中英

Incorrectly escaped sed command in Jenkinsfile

I'm trying to run a sed command in a jenkinsfile which has a number of special characters in the search pattern and the replacement string is using two variables and a string.

The string I'm looking to replace in the file is: ${data.terraform_remote_state.bucket.s3_bucket_name2}

I need to replace all of it with a simple string, but grabbing the full string with brackets and all is proving difficult.

This is the latest thing I've tried, as well a few other attempts at either less escapes or trying to put the full string in into the sed command.

sh """
    S3_IAM_PATH1='\$\\{data\\.terraform_remote_state\\.bucket\\.s3_bucket_name1\\}'
    S3_IAM_PATH2='\$\\{data\\.terraform_remote_state\\.bucket\\.s3_bucket_name2\\}'
    sed -i s/\${S3_IAM_PATH1}/\${CLUSTER_NAME}-\${ID}-export/g $CLUSTER_TYPE/security/iam.tf
    sed -i s/\${S3_IAM_PATH2}/\${CLUSTER_NAME}-\${ID}-import/g $CLUSTER_TYPE/security/iam.tf
    cat $CLUSTER_TYPE/security/iam.tf
"""

The current output I'm getting is this:

+ S3_IAM_PATH1='$\{data\.terraform_remote_state\.bucket\.s3_bucket_name1\}'
+ S3_IAM_PATH2='$\{data\.terraform_remote_state\.bucket\.s3_bucket_name2\}'
+ sed -i 's/$\{data\.terraform_remote_state\.bucket\.s3_bucket_name1\}/thisCluster1-process211-export/g' DATA-CLUSTER/security/awsiam.tf
sed: -e expression #1, char 93: Invalid content of \{\}

Am willing to put out more code if needed but would really appreciate any help on this!

You should use triple single quote ''' which won't trigger groovy string interpolation on pattern ${} appeared in Jenkinsfile.

// previous stage
def CLUSTER_TYPE = 'xxx'

// if CLUSTER_TYPE is groovy variable in previous stage
// you can make it into environment variable, so that
// bash in next stage can access it from environment variables
env.CLUSTER_TYPE = CLUSTER_TYPE 


// next stage
sh '''
    S3_IAM_PATH1='${data.terraform_remote_state.bucket.s3_bucket_name1}'
    S3_IAM_PATH2='${data.terraform_remote_state.bucket.s3_bucket_name2}'
    sed -i "s/${S3_IAM_PATH1}/${CLUSTER_NAME}-${ID}-export/g" "$CLUSTER_TYPE/security/iam.tf"
    sed -i "s/${S3_IAM_PATH2}/${CLUSTER_NAME}-${ID}-import/g" "$CLUSTER_TYPE/security/iam.tf"
    cat "$CLUSTER_TYPE/security/iam.tf"
'''

I eventually got it running through the initial issue @yong supplied and a few minor tweaks in case anyone needs something similar in the future. The issue was the CLUSTER_TYPE variable not being set as Parameter in jenkinsfile but rather as something created in a previous stage. To access it, I needed to use double quotes. So some of the solution needed double quotes and the rest needed single:

sh '''
    S3_IAM_PATH1='${data.terraform_remote_state.bucket.s3_bucket_name1}'
    S3_IAM_PATH2='${data.terraform_remote_state.bucket.s3_bucket_name2}'
    sed -i "s/${S3_IAM_PATH1}/${CLUSTER_NAME}-${ID}-export/g" ''' + 
 "$CLUSTER_TYPE/security/iam.tf; " +
    '''sed -i "s/${S3_IAM_PATH2}/${CLUSTER_NAME}-${ID}-import/g" ''' 
 + """$CLUSTER_TYPE/security/iam.tf; 
    cat "$CLUSTER_TYPE/security/iam.tf" """

Like I said, not pretty but it works :) Thanks for the help guys!

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