Issue
This is my Jenkinsfile:
pipeline {
...
parameters {
string(name: 'BACKEND_VERSION', defaultValue: 'latest')
string(name: 'FRONTEND_VERSION', defaultValue: 'latest')
}
environment {
POSTGRES_PASSWORD = credentials('postgres-password')
}
...
stages {
stage('Production Deployment') {
steps {
...
sh('./terraform apply -var postgres_password=$POSTGRES_PASSWORD -var backend_version=${params.BACKEND_VERSION} -var frontend_version=${params.FRONTEND_VERSION} --auto-approve')
...
}
}
}
}
This line:
sh('./terraform apply -var postgres_password=$POSTGRES_PASSWORD -var backend_version=${params.BACKEND_VERSION} -var frontend_version=${params.FRONTEND_VERSION} --auto-approve')
produces this error:
/var/lib/jenkins/workspace/.../script.sh: line 1: syntax error: bad substitution
Using double quotes (sh("./terraform apply ...")
) produces this error:
/var/lib/jenkins/workspace/.../script.sh: line 1: syntax error: unterminated quoted string
What's the correct way of string interpolation when we have both environment variables and credentials.
Solution
I'm not sure if it's the best way or not. But I created two new environment variables from parameters:
environment {
POSTGRES_PASSWORD = credentials('postgres-password')
BACKEND_VERSION = "${params.BACKEND_VERSION}"
FRONTEND_VERSION = "${params.FRONTEND_VERSION}"
}
and then use them like this:
sh('./terraform apply -var postgres_password=$POSTGRES_PASSWORD -var backend_version=$BACKEND_VERSION -var frontend_version=$FRONTEND_VERSION --auto-approve')
Answered By - HsnVahedi
Answer Checked By - Gilberto Lyons (JavaFixing Admin)