Issue
stage('adjust Dockerfile') {
steps {
script {
TAG = sh(returnStdout: true, script: 'echo 123456')
sh 'sed -i "s/name:TAG/name:\"${TAG}\"/g" Dockerfile'
}
}
}
Result is that variable Tag is not replaced in the sh command in the Jenkinsfile.
+ sed -i s/name:TAG/name:/g Dockerfile
Same result if I change to
TAG = sh(returnStdout: true, script: 'echo 123456')
sh 'sed -i "s/name:TAG/name:${TAG}/g" Dockerfile'
If I change the quotation marks like this
TAG = sh(returnStdout: true, script: 'echo 123456')
sh "sed -i 's/name:TAG/name:${TAG}/g' Dockerfile"
I got the variable TAG replaced with the correct value but got a issue with sed.
+ sed -i s/name:TAG/name:123456
/g Dockerfile
sed: -e expression #1, char 22: unterminated `s' command```
Solution
You should replace echo 123456
with printf 123456
as echo
adds a trailing newline at the end (which ruins the sed command later), and you must use double quotes around the sed command in order to interpolate variables inside it:
Use
TAG = sh(returnStdout: true, script: 'printf 123456')
sh 'sed -i "s/name:TAG/name:${TAG}/g" Dockerfile'
Answered By - Wiktor Stribiżew Answer Checked By - Marilyn (WPSolving Volunteer)