繁体   English   中英

如何使用 Google Secrets Manager 在 Google Cloud Build 中创建 docker ARG?

[英]How do I use Google Secrets Manager to create a docker ARG in Google Cloud Build?

我正在 GCB 上进行构建,我需要在其中安装私有依赖项,因此我正在使用 Google Secrets Manager。 我的 cloudbuild.yaml 看起来像这样:

steps:
- name: gcr.io/cloud-builders/gcloud
  entrypoint: 'bash'
  args: [ '-c', "gcloud secrets versions access latest --secret=PERSONAL_ACCESS_TOKEN_GITHUB --format='get(payload.data)' | tr '_-' '/+' | base64 -d > decrypted-pat.txt" ]
- name: 'gcr.io/cloud-builders/docker'
  args:
    - build
    - '--build-arg'
    - PERSONAL_ACCESS_TOKEN_GITHUB=$(cat decrypted-pat.txt)
    - '-t'
    - 'gcr.io/$PROJECT_ID/$REPO_NAME:$TAG_NAME'
    - .
images: [ gcr.io/$PROJECT_ID/$REPO_NAME:$TAG_NAME ]

但是, $(cat decrypted-pat.txt)没有得到评估。 Inserting: RUN echo https://${PERSONAL_ACCESS_TOKEN_GITHUB}@github.com into my dockerfile simply echoes the literal: of course, https://$(cat decrypted-pat.txt)@github.com is not the command I'我正在寻找(是的,如果我得到实际成功回显的东西,我将旋转令牌)。

gcb / secrets 文档中有一条注释

要在环境变量中使用密钥,您需要在变量名称前加上下划线“_”并使用 '(' 转义该值。例如:_VARIABLE_NAME=$(cat password.txt) && echo -n )_VARIABLE_NAME。

但这对我在构建参数中使用没有多大意义。

如何将这个秘密的实际值作为构建参数放入容器中?

自 2021 年 2 月 10 日起,您可以使用availableSecrets字段直接从 Cloud Build 访问 Secret Manager 机密:

steps:
- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'bash'
  args: ['-c', 'docker login --username=$$USERNAME --password=$$PASSWORD']
  secretEnv: ['USERNAME', 'PASSWORD']
availableSecrets:
  secretManager:
  - versionName: projects/PROJECT_ID/secrets/DOCKER_PASSWORD_SECRET_NAME/versions/DOCKER_PASSWORD_SECRET_VERSION
    env: 'PASSWORD'
  - versionName: projects/PROJECT_ID/secrets/DOCKER_USERNAME_SECRET_NAME/versions/DOCKER_USERNAME_SECRET_VERSION
    env: 'USERNAME'

文档

我发现我可以绕过 docker 构建步骤中的默认入口点,然后使用 bash 命令直接调用 docker。

steps:
- name: gcr.io/cloud-builders/gcloud
  entrypoint: 'bash'
  args: [ '-c', "gcloud secrets versions access latest --secret=PERSONAL_ACCESS_TOKEN_GITHUB --format='get(payload.data)' | tr '_-' '/+' | base64 -d > decrypted-pat.txt" ]

- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'bash'
  args:
    - "-c"
    - |
      # For getting the secret and pass it to a command/script
      docker build --build-arg PERSONAL_ACCESS_TOKEN_GITHUB=$(cat decrypted-pat.txt) -t gcr.io/$PROJECT_ID/$REPO_NAME:$TAG_NAME .

(受此帖子启发修复)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM