【问题标题】:Including default substitutions inside other `substitutions`在其他 `substitutions` 中包含默认替换
【发布时间】:2020-02-19 14:49:02
【问题描述】:

从这里...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} 文本仍然存在,而不是实际项目身份证。

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

编辑

嗯,我尝试了几个不同的选项,包括@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 而不是使用替换。

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

【问题讨论】:

    标签: yaml google-cloud-build


    【解决方案1】:

    您可以使用环境变量而不是替换,正如您在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
    

    【讨论】:

    • project-name 是默认替换。我想从传递给我的值中得到它。否则我每次使用它都必须进去编辑它。
    • 如果在 env 变量中使用默认替换会怎样?这样你就不需要替换标签了。
    • 我就是这么做的。那也不行。我认为其他问题中的 $$ 语法不正确。
    • 确实,$$ 只会在变量本身的值前面添加一个 $。在这种情况下,试试我编辑的答案,它应该可以工作
    • 谢谢,我试试看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多