Jenkinsfile不从dind容器中构建图像

时间:2020-09-03 19:11:30

标签: docker jenkins jenkins-pipeline

我有一个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中构建图像是否有更“优雅”的方式?

1 个答案:

答案 0 :(得分:2)

出现错误消息是因为${env.VERSION}不是有效的环境变量。应该使用双引号使Jenkins评估该变量并将评估值传递到sh步骤:

sh "docker build --tag alerta-local --build-arg ALERTA_BASE_VERSION=${env.VERSION} alerta-custom"
相关问题