【问题标题】:"gcloud builds submit" is not triggering error for missing required substitutions“gcloud builds submit”不会因为缺少所需的替换而触发错误
【发布时间】:2021-04-23 01:58:27
【问题描述】:

我需要一些有关云构建的帮助--substitutions。

这是文档:https://cloud.google.com/cloud-build/docs/build-config#substitutions

这是说的:

cloudbuild.yaml

substitutions:
    _SUB_VALUE: world
options:
    substitution_option: 'ALLOW_LOOSE'

下面的 sn-p 使用替换来打印“hello world”。 ALLOW_LOOSE 替换选项已设置,这意味着如果缺少替换变量或缺少替换,构建将不会返回错误。

我的情况:我没有使用ALLOW_LOOSE 选项。我需要我的替代品。我不希望应用任何默认值。如果我忘记传递任何我需要的替换,我需要它立即失败。

这是我的cloudbuild.yaml 文件:

cloudbuild.yaml

substitutions: 
  _SERVER_ENV: required
  _TAG_NAME: required
  _MIN_INSTANCES: required

我将它们的默认值初始化为required,特别是因为如果我忘记将它们中的任何一个传递给gcloud builds submit 调用,我预计构建调用会失败。

如果我调用gcloud builds submit 并且不传递任何定义的替换,我预计它会失败。但它并没有失败,并且构建正常完成,没有该值。

文档中有这样的观察:

注意:如果您的构建由触发器调用,则默认设置 ALLOW_LOOSE 选项。在这种情况下,如果缺少替换变量或缺少替换变量,您的构建将不会返回错误替代。您不能为触发器调用的构建覆盖 ALLOW_LOOSE 选项。

但如果我手动调用gcloud builds submit,这意味着我的构建没有被任何触发器调用,对吧?因此不应启用ALLOW_LOOSE 选项。

这是我完整的cloudbuild.yaml:

cloudbuild.yaml

steps:
  - name: "gcr.io/cloud-builders/docker"
    args:
      - "build"
      - "--build-arg" 
      - "SERVER_ENV=$_SERVER_ENV"
      - "--tag"
      - "gcr.io/$PROJECT_ID/server:$_TAG_NAME"
      - "."
    timeout: 180s

  - name: "gcr.io/cloud-builders/docker"
    args:
      - "push"
      - "gcr.io/$PROJECT_ID/server:$_TAG_NAME"
    timeout: 180s

  - name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
    entrypoint: gcloud
    args:
      - "beta"
      - "run"
      - "deploy"
      - "server"
      - "--image=gcr.io/$PROJECT_ID/server:$_TAG_NAME"
      - "--platform=managed"
      - "--region=us-central1"
      - "--min-instances=$_MIN_INSTANCES"
      - "--max-instances=3"
      - "--allow-unauthenticated"
    timeout: 180s

images: 
  - "gcr.io/$PROJECT_ID/server:$_TAG_NAME"
substitutions: 
  _SERVER_ENV: required
  _TAG_NAME: required
  _MIN_INSTANCES: required

【问题讨论】:

    标签: google-cloud-build cloudbuild.yaml


    【解决方案1】:

    在您的cloudbuild.yaml 文件中,当您定义substituions 变量时,您会自动设置其默认值

    substitutions: 
      # Value = "required"
      _SERVER_ENV: required 
      # Value = ""
      _TAG_NAME: 
    

    尝试使用substitutions数组中未定义的变量,如:

    steps:
      - name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
        entrypoint: bash 
        args:
          - -c
          - | 
             # print "required"
             echo $_SERVER_ENV 
             # print nothing
             echo $_TAG_NAME
             # Error, except if you allow loose. In this case, print nothing
             echo $_MIN_INSTANCES
    substitutions: 
      _SERVER_ENV: required
      _TAG_NAME: 
    

    【讨论】:

    • 感谢您的回答。这是我一开始的想法。但是我尝试从subtitutions 属性中删除_SERVER_ENV(它不是一个数组),同样的情况也会发生。我会尝试添加substitution_option: "MUST_MATCH",看看效果如何。刚刚发现here。
    • 我开始很确定这是一个错误。那也没有用。我完全删除了substitutions 属性。并且没有通过任何预期的substitutions。构建失败,但不是因为gcloud builds submit 调用中缺少substitutions,而是因为它们的缺失值破坏了依赖它们被填充的步骤。
    • 什么!!我刚试过,你是对的,没有错误!!!几周前我遇到了错误......同意,这应该是一个错误。
    • 首相不走运。我打开了这个问题:issuetracker.google.com/issues/178013730
    猜你喜欢
    • 2019-09-14
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2021-09-04
    相关资源
    最近更新 更多