在其他“替代”中包含默认替换

时间:2020-02-19 14:49:02

标签: yaml google-cloud-build

从这里... https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values

我正在尝试在cloudbuild.yaml文件中使用替代项,但是几乎所有替代项都取决于要部署到的项目的项目ID。

我有这样的yaml文件...

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['functions', 'deploy', 'functionName', ...other args... , '--service-account', '${_SERVICE_ACCOUNT}', '--source', '${_SOURCE_REPO}']
substitutions:
  _SOURCE_REPO: 'https://source.developers.google.com/projects/$PROJECT_ID/repos/my-repo-id/moveable-aliases/master/paths/functions/src'
  _SERVICE_ACCOUNT: 'blah@${PROJECT_ID}.iam.gserviceaccount.com'

无论我尝试哪种替换方式(我都尝试过两种格式$_FOO${_FOO}),最终得到的是blah@${PROJECT_ID}.iam.gserviceaccount.com,其中${PROJECT_ID}文本仍在而不是那里有实际的项目ID。

如果我将文本移到步骤中并仅使用它而不是替换,那么它将起作用。但理想情况下,我想使用此方法,因为这些值将被大量使用,以节省重复次数。

编辑

好吧,我尝试了几种不同的选择,包括@ralemos提到的一些建议,但似乎没有任何作用。

如果我使用这种格式...

steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    args: ['functions', 'deploy', 'functionName', ...other args... , '--service-account', '${SERVICE_ACCOUNT}', '--source', '${SOURCE_REPO}']
options:
  env:
    SOURCE_REPO: 'https://source.developers.google.com/projects/${PROJECT_ID}/repos/my-repo-id/moveable-aliases/master/paths/functions/src'
    SERVICE_ACCOUNT: 'blah@${PROJECT_ID}.iam.gserviceaccount.com

它抱怨env行无效。

如果我使用这种格式...

steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    args: ['functions', 'deploy', 'functionName', ...other args... , '--service-account', '${SERVICE_ACCOUNT}', '--source', '${SOURCE_REPO}']
options:
  env:
    'SOURCE_REPO=https://source.developers.google.com/projects/${PROJECT_ID}/repos/my-repo-id/moveable-aliases/master/paths/functions/src'
    'SERVICE_ACCOUNT=blah@${PROJECT_ID}.iam.gserviceaccount.com

它抱怨SERVICE_ACCOUNTSOURCE_REPO不是有效的内置替换。

如果我使用$$SOURCE_REPO作为语法,它只会将其替换为$SOURCE_REPO,而不是使用替换。

现在看来,我正在尝试的事情是不可能的。

1 个答案:

答案 0 :(得分:0)

您可以使用环境变量而不是替代变量,如对此community post的答案所见。

结果将是这样的:

steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    args: ['functions', 'deploy', 'functionName', ...other args... , '--service-account', '${SERVICE_ACCOUNT}', '--source', '${SOURCE_REPO}']
options:
  env:
    SOURCE_REPO: 'https://source.developers.google.com/projects/${PROJECT_ID}/repos/my-repo-id/moveable-aliases/master/paths/functions/src'
    SERVICE_ACCOUNT: 'blah@${PROJECT_ID}.iam.gserviceaccount.com

让我知道这是否对您有帮助。