Google Cloud Build 和 App Engine 环境变量

Google Cloud Build and App Engine enviroment variables

我的 App Engine 上有一个秘密令牌 app.yaml

env_variables:
  TOKEN: super-secret-token

而且显然这个令牌不在 git。使用 Google Cloud Build,如何在构建时或之前设置此参数 TOKEN

在将您的应用程序部署到 App Engine 之前,您可以使用 Secret Manager within Cloud Build 获取实际密钥并替换 app.yaml 中的 super-secret-token 占位符值。那看起来像这样:

steps:
- name: gcr.io/cloud-builders/gcloud
  entrypoint: 'bash'
  args: [ '-c', "gcloud secrets versions access latest --secret=secret-name --format='get(payload.data)' | tr '_-' '/+' | base64 -d > decrypted-data.txt" ]
- name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: /bin/sh
  args:
  - '-c'
  - |
     sed "s/super-secret-token/g" $(cat decrypted-data.txt)
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  entrypoint: 'bash'
  args: ['-c', 'gcloud config set app/cloud_build_timeout 1600 && gcloud app deploy']
timeout: '1600s'

话虽如此,您的秘密令牌仍可在 App Engine 的环境变量中未加密地使用,这在安全方面不是最佳选择。相反,您可能希望直接从 App Engine 代码中查询 Secret Manager。您会找到执行此操作的代码示例 here.