如何使用 Cloud Build 使用 Secret Manager 的机密部署 Cloud Functions?
How to deploy Cloud Functions with secrets from Secret Manager using Cloud Build?
我有一个 Cloud Function,我想使用 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'
的错误。我不明白为什么会出现此错误,因为我认为我的论点符合上述模式。
有什么我遗漏的吗?
这是一些文档:https://cloud.google.com/build/docs/securing-builds/use-secrets
您需要在 cloudbuild.yaml
中使用 secretEnv 密钥和 availableSecrets 声明
首先,按照 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']
Note: Do not put spaces in --set-secrets value if you have multiple secrets
要了解更多信息,请查看此 documentation。
我有一个 Cloud Function,我想使用 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'
的错误。我不明白为什么会出现此错误,因为我认为我的论点符合上述模式。
有什么我遗漏的吗?
这是一些文档:https://cloud.google.com/build/docs/securing-builds/use-secrets
您需要在 cloudbuild.yaml
中使用 secretEnv 密钥和 availableSecrets 声明首先,按照 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']
Note: Do not put spaces in --set-secrets value if you have multiple secrets
要了解更多信息,请查看此 documentation。