使用 Cloud Build 时,如何将密钥从 Google Secrets Manager (GSM) 传递到 Cloud Function?下面的cloudbuild.yaml包含三个步骤。此外,我正在使用volumes
在构建步骤之间创建永久存储。我可以通过 Cloud Build 确认 GSM 检索。但是,当我尝试使用 yaml 格式传递秘密时,--env-vars-file
遇到以下错误...
Already have image (with digest): gcr.io/cloud-builders/gcloud
ERROR: gcloud crashed (AttributeError): 'str' object has no attribute 'items'
cloudbuild.yaml:
steps:
- name: 'gcr.io/cloud-builders/gcloud'
volumes:
- name: 'secrets'
path: '/secrets'
entrypoint: "bash"
args:
- "-c"
- |
echo -n 'gsm_secret:' > /secrets/my-secret-file.txt
- name: 'gcr.io/cloud-builders/gcloud'
volumes:
- name: 'secrets'
path: '/secrets'
entrypoint: "bash"
args:
- "-c"
- |
gcloud components update
gcloud beta secrets versions access --secret=MySecret latest >> /secrets/my-secret-file.txt
cat /secrets/my-secret-file.txt
- name: 'gcr.io/cloud-builders/gcloud'
volumes:
- name: 'secrets'
path: '/secrets'
args: [
'functions', 'deploy', 'gsm-foobar',
'--project=[...]',
'--trigger-http',
'--runtime=go111',
'--region=us-central1',
'--memory=256MB',
'--timeout=540',
'--entry-point=GSM',
'--allow-unauthenticated',
'--source=https://source.developers.google.com/[...]',
'--service-account', '[...]@appspot.gserviceaccount.com',
'--env-vars-file', '/secrets/my-secret-file.txt'
]
更新:
不需要使用卷,因为/workspace
Cloud Build 中的步骤之间的永久存储也是如此。此外,gcloud components update
不再需要,因为截至今天,默认的 Cloud SDK 版本是 279.0.0
一个解法:
steps:
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: "bash"
args:
- "-c"
- |
echo "gsm_secret: $(gcloud beta secrets versions access --secret=MySecret latest)" > /workspace/my-secret-file.txt
cat /workspace/my-secret-file.txt
- name: 'gcr.io/cloud-builders/gcloud'
args: [
'functions', 'deploy', 'gsm-foobar',
[...]
'--entry-point=GSM',
'--allow-unauthenticated',
'--source=https://source.developers.google.com/[...]',
'--service-account', '[...]@appspot.gserviceaccount.com',
'--env-vars-file=/workspace/my-secret-file.txt'
]