0

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

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

这是说的:

cloudbuild.yaml

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

以下代码段使用替换来打印“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
4

1 回答 1

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: 
于 2021-01-18T19:10:14.193 回答