如果我在 jenkins 管道中使用 docker build-arg 会出错

Getting error if I use docker build-arg in jenkins pipeline

我需要在 docker 中使用主机 ssh 密钥,为此我构建了 docker like

docker build -t example --build-arg ssh_prv_key="$(cat ~/.ssh/id_rsa)"  -f dockerfile-dev .

如果我们使用直接 docker 命令,它工作正常,但如果我在 jenkins 管道脚本中使用,就会出现以下错误

Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 92: expecting '}', found 'ssh_prv_key' @ line 92, column 116.
   ev:${GIT_COMMIT} "--build-arg ssh_prv_ke 

我在 jenkins 管道中使用了以下步骤

 sh "docker build -t ${service_name}-dev:${GIT_COMMIT} --build-arg ssh_prv_key="$(cat ~/.ssh/id_rsa)" -f dockerfile-dev ." 

和docker 文件使用如下

ARG ssh_prv_key

# Authorize SSH Host
# Add the keys and set permissions
RUN mkdir -p /root/.ssh
RUN echo "$ssh_prv_key" > /root/.ssh/id_rsa && \
    chmod 600 /root/.ssh/id_rsa

我解决了一个类似的问题如下:

Jenkins 管道

sh "cp ~/.ssh/id_rsa id_rsa"
sh "docker build -t ${service_name}-dev:${GIT_COMMIT} -f dockerfile-dev ." 
sh "rm id_rsa"

Dockerfile


# Some instructions...

ADD id_rsa id_rsa

# Now use the "id_rsa" file inside the image...