【问题标题】:How to deploy Cloud Functions with secrets from Secret Manager using Cloud Build?如何使用 Cloud Build 使用来自 Secret Manager 的密钥部署 Cloud Functions?
【发布时间】:2021-08-29 22:12:27
【问题描述】:

我有一个云函数,我想使用 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'

但是,当我尝试使用具有此配置的 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'

我收到--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' 的错误。我不明白为什么会出现此错误,因为我认为我的论点符合上述模式。

我有什么遗漏吗?

【问题讨论】:

  • 您可以尝试删除 set-secret args 中的简单引号吗?
  • 谢谢,这确实是解决方案!

标签: google-cloud-platform google-cloud-functions google-cloud-build


【解决方案1】:

首先,按照 Guillaume 的建议删除每对的引号。之后,它应该如下所示:

--set-secrets=env_1=secret_1:latest,env_2=secret_2:latest

或者,我的建议是将您的所有参数作为一个列表包含在下面的示例中。我测试了下面的配置,它对我有用。

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']

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

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

【讨论】:

  • 谢谢!您关于删除引号的第一个建议起到了作用:)
【解决方案2】:

这里是一些文档:https://cloud.google.com/build/docs/securing-builds/use-secrets

您需要在 cloudbuild.yaml 中使用 secretEnv 密钥以及 availableSecrets 声明

【讨论】:

  • 错误答案 ;)
  • 感谢学员的投入!我知道可以通过首先将秘密带入 Cloud Build,然后将其插入到环境变量中来将秘密带入 Cloud Function。但是,我希望 Cloud Function 使用 --set-secrets 参数直接引用机密。
猜你喜欢
  • 2021-06-03
  • 1970-01-01
  • 2020-12-02
  • 2021-10-25
  • 2023-02-26
  • 1970-01-01
  • 2021-09-30
  • 2020-11-03
  • 2018-05-18
相关资源
最近更新 更多