我有一个Jenkinsfile
阶段的以下步骤,该阶段在kubernetes
jenkins
中启动,并在dind
容器内运行(因此docker
守护程序存在)
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'
}
}
假设在echo "Bulding with alerta base version: ${env.VERSION}"
控制台中看到了jenkins
,显然已执行:
21:55:57 Bulding with alerta base version: 8.0.2
但是,管道最终失败并出现以下错误:
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
我想念什么?
假设我要在构建过程中通过Jenkinsfile
,在--build-arg
中构建图像是否有更“优雅”的方式?
答案 0 :(得分:2)
出现错误消息是因为${env.VERSION}
不是有效的环境变量。应该使用双引号使Jenkins评估该变量并将评估值传递到sh
步骤:
sh "docker build --tag alerta-local --build-arg ALERTA_BASE_VERSION=${env.VERSION} alerta-custom"