Issue
I have the following step of a Jenkinsfile
stage that is initiated in a kubernetes
jenkins
and run from within a dind
container (therefore the docker
daemon is present)
steps {
echo 'building staging image'
script {
env.VERSION = readFile 'alerta/ci/ALERTA_BASE_VERSION'
}
echo "Bulding with alerta base version: ${env.VERSION}"
sh 'docker build --tag alerta-local --build-arg ALERTA_BASE_VERSION=${env.VERSION} alerta-custom'
}
}
The echo "Bulding with alerta base version: ${env.VERSION}"
is apparently executed, given that I see in the jenkins
console:
21:55:57 Bulding with alerta base version: 8.0.2
However the pipeline ultimately fails with the following error:
21:55:59 /home/jenkins/agent/workspace/ebhook-pipeline_alerta_dev_infra@tmp/durable-606e41f8/script.sh: 1: /home/jenkins/agent/workspace/ebhook-pipeline_alerta_dev_infra@tmp/durable-606e41f8/script.sh: Bad substitution
What am I missing?
Is there a more "elegant" way of building images in a Jenkinsfile
given that I want to pass --build-arg
during the built?
Solution
The error message is raised because ${env.VERSION}
is not a valid environment variable. Double quotes should be used to make Jenkins evaluate that variable and pass the evaluated value to the sh
step:
sh "docker build --tag alerta-local --build-arg ALERTA_BASE_VERSION=${env.VERSION} alerta-custom"
Answered By - kangasta
Answer Checked By - Katrina (JavaFixing Volunteer)