如何使用 Cloud Build 使用 Secret Manager 中的机密部署 Cloud Functions?

Emi*_*sen 4 google-cloud-platform google-cloud-functions google-cloud-build

我有一个云函数,我想使用 Cloud Build 将其部署在我的 CD 管道中。该函数需要存储在 Secret Manager 中的几个秘密,我想使用该--set-secrets标志将它们作为环境变量拉入。

当我使用 CLI 手动部署时,没有任何问题:

gcloud beta functions deploy myfunction \
  --source src \
  --trigger-topic mytopic \
  --region europe-west1 \
  --runtime python39 \
  --set-secrets 'env_1=secret_1:latest','env_2=secret_2:latest'
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用具有此配置的 Cloud Build 进行部署时:

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  args:
  - beta
  - functions
  - deploy
  - myfunction
  - --source=src
  - --trigger-topic=mytopic
  - --region=europe-west1
  - --runtime=python39
  - --set-secrets='env_1=secret_1:latest','env_2=secret_2:latest'
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,该--set-secrets参数must match the pattern 'SECRET:VERSION' or 'projects/{PROJECT}/secrets/{SECRET}:{VERSION}' or 'projects/{PROJECT}/secrets/{SECRET}/versions/{VERSION}' where VERSION is a number or the label 'latest'. 我不明白为什么会出现此错误,因为我认为我的论点符合所述模式。

我有什么遗漏的吗?

Mab*_* A. 9

首先,按照 Guillaume 的建议删除每对周围的引号。之后,它应该看起来像这样:

--set-secrets=env_1=secret_1:latest,env_2=secret_2:latest
Run Code Online (Sandbox Code Playgroud)

或者,我的建议是将所有参数封装为一个列表,如下例所示。我测试了下面的配置,它对我有效。

steps:
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  args: ['gcloud', 'beta','functions', 'deploy', 'myfunction', '--region=europe-west1', '--source=src', '--trigger-topic=mytopic', '--runtime=python39', '--set-secrets=env_1=secret_1:latest,env_2=secret_2:latest']
Run Code Online (Sandbox Code Playgroud)

注意:如果您有多个秘密,请勿在 --set-secrets 值中添加空格

要了解更多信息,请查看此文档