GCP Cloud构建将秘密传递给docker arg

时间:2020-06-09 14:59:00

标签: docker google-cloud-platform

我打算将我的npm令牌传递给gcp云构建, 这样我就可以在多阶段构建中使用它来安装私有npm软件包。

我有以下简短的Dockerfile:

FROM ubuntu:14.04 AS build
ARG NPM_TOKEN

RUN echo "NPM_TOKEN:: ${NPM_TOKEN}"

和以下简短的cloudbuild.yaml:

---
  steps:
  - name: gcr.io/cloud-builders/gcloud
    entrypoint: 'bash'
    args: [ '-c', 'gcloud secrets versions access latest --secret=my-npm-token > npm-token.txt' ]
  - name: gcr.io/cloud-builders/docker
    args:
    - build
    - "-t"
    - gcr.io/my-project/my-program
    - "."
    - "--build-arg NPM_TOKEN= < npm-token.txt"
    - "--no-cache"

我的cloudbuild.yaml基于documentation,但似乎无法将两个和两个放在一起,例如:“ --build-arg NPM_TOKEN =

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

您的目标是使秘密文件内容进入build参数。因此,您必须使用NPM_TOKEN="$(cat npm-token.txt)"NPM_TOKEN="$(< npm-token.txt)"来读取文件内容。

  name: gcr.io/cloud-builders/docker
  entrypoint: 'bash'
  args: [ '-c', 'docker build -t gcr.io/my-project/my-program . --build-arg NPM_TOKEN="$(cat npm-token.txt)" --no-cache' ]

注意:但是,gcr.io/cloud-builders/docker使用exec入口点形式。因此,您将入口点设置为bash。

还要注意,您将机密保存到构建workspace(/ workspace / ..)中。这还允许您将秘密作为文件复制到容器中。

FROM ubuntu:14.04 AS build
ARG NPM_TOKEN

COPY npm-token.txt .
RUN echo "NPM_TOKEN:: $(cat npm-token.txt)"

答案 1 :(得分:2)

我不会像您那样写第二步,而是这样:

  - name: gcr.io/cloud-builders/docker
    entrypoint: "bash"
    args:
    - "-c"
    - |
        build -t gcr.io/my-project/my-program . --build-arg NPM_TOKEN=$(cat npm-token.txt) --no-cache