My pipeline sh block:
sh "set +e; /terraform/terraform plan -var aws_access_key=${aws_access_key} -
var aws_secret_key=${aws_secret_key} -var aws_ami=${ami_id} -var
aws_instance_type=${instance_type} -var aws_elb_security_group=${elb_sg} -var
aws_ec2_security_group=${ec2_sg} -detailed-exitcode; echo \$? > status"
exitCode = readFile('status').trim()
echo "Terraform Plan Exit Code: ${exitCode}"
output :
+ set +e
+ /terraform/terraform plan -var aws_access_key=**** -var aws_secret_key=**** -var aws_ami=ami-xxxxxxx
+ -var aws_instance_type=t2.medium -var aws_elb_security_group=sg-xxxx
/terraform/selectdev/int/mp-frontend@tmp/durable-6c57c14c/script.sh: line 3: -var: command not found
+ -var aws_ec2_security_group=sg-axxx
/terraform/selectdev/int/mp-frontend@tmp/durable-6c57c14c/script.sh: line 4: -var: command not found
+ -detailed-exitcode
/terraform/selectdev/int/mp-frontend@tmp/durable-6c57c14c/script.sh: line 5:
-detailed-exitcode: command not found
+ echo 127
I'm not sure why new line is being added to the command and If I do single quotes like sh '', variables are blank. what am I doing wrong ?
I tried to do like below but it too adding new lines
def command = $/....../$
res = sh(returnStdout: true, script: command)
First, FYI: single quotes skip variable interpolation in groovy
If you want to have a multiple line script in a string, you need to escape endlines in a multi line variable.
You need three things:
"""
. This allows you to have multi-line strings with interpolation (triple single quoted strings '''
let you do the same thing without interpolation).\
. This lets you insert newlines to format a long command.\"
)For example as follows: (one argument per line for readability)
sh("""set +e; /terraform/terraform plan \
-var aws_access_key="${aws_access_key}" \
-var aws_secret_key="${aws_secret_key}" \
-var aws_ami="${ami_id}" \
-var aws_instance_type="${instance_type}" \
-var aws_elb_security_group="${elb_sg}" \
-var aws_ec2_security_group="${ec2_sg}" \
-detailed-exitcode; echo \$? > status""")
The variables I'm using are from other stages and have new line.. had to use .trim()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With