Google Cloud Build Docker build-arg from file

时间:2019-03-10 04:50:02

标签: docker go dockerfile google-cloud-build

使用Google Cloud Build时出现问题。我无法通过cloudbuild.yaml将密钥传递给Docker

Google buildfile.yaml:

- name: 'gcr.io/cloud-builders/gcloud'
  args:
  - kms
  - decrypt
  - --ciphertext-file=A.enc
  - --plaintext-file=/root/.ssh/id_rsa
  - --location=global
  - --keyring=keyringxxx
  - --key=keyxxx
  volumes:
  - name: 'ssh'
    path: /root/.ssh
- name: 'gcr.io/cloud-builders/docker'
  args: [
    'build', '.',
    '-t', 'gcr.io/$PROJECT_ID/xxx:latest',
    '--build-arg', 'READ_KEY=`cat /root/.ssh/id_rsa`'
  ]
  volumes:
  - name: 'ssh'

Dockerfile:

FROM golang:1.11 AS builder

ARG READ_KEY
RUN mkdir -p ~/.ssh &&  \
    echo "$READ_KEY" > ~/.ssh/id_rsa && \
    chmod 0600 ~/.ssh/id_rsa && \
    ssh-keyscan github.com >> /root/.ssh/known_hosts && \
    git config --global url.ssh://git@github.com/XXXX.insteadOf https://github.com/XXXX

......

以上代码失败。 cat不起作用。

3 个答案:

答案 0 :(得分:1)

.ssh目录需要具有正确的权限

RUN mkdir -m 700 -p ~/.ssh &&  

答案 1 :(得分:0)

GCloud Docker Builder使用的是ENTRYPOINT的Exec形式。您来自cloudbuild.yaml的参数不会传递到外壳程序,因此您的cat将不会执行。

为什么不指示KMS将id_rsa直接写到/workspace并完全废除ssh卷?

- name: 'gcr.io/cloud-builders/gcloud'
  args:
  - kms
  - decrypt
  - --ciphertext-file=A.enc
  - --plaintext-file=/workspace/id_rsa
  - --location=global
  - --keyring=keyringxxx
  - --key=keyxxx
- name: 'gcr.io/cloud-builders/docker'
  args: [
    'build', '.',
    '-t', 'gcr.io/$PROJECT_ID/xxx:latest'
  ]

Dockerfile变成:

FROM golang:1.11 AS builder

RUN mkdir -p ~/.ssh
COPY id_rsa ~/.ssh/
RUN ssh-keyscan github.com >> ~/.ssh/known_hosts && \
    chmod -R 0600 ~/.ssh/ && \
    git config --global url.ssh://git@github.com:.insteadOf https://github.com

请不要忘记将.gitconfig挂载到其他构建步骤中。我只是将其作为CI构建脚本的一部分,而不需要额外的volume

答案 2 :(得分:0)

对于步骤有问题的人:

COPY id_rsa ~/.ssh/

如果 ~/.ssh/id_rsa 在 docker build 中不持久,对我有用的解决方法是先将文件复制到 App 文件中的目录,然后再复制到最终目录:

ADD id_rsa /app/id_rsa
RUN cp /app/id_rsa ~/.ssh/id_rsa

我不知道为什么这行得通,但很好,你去吧。