微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

“gcloud builds submit”不会因缺少必需的替换而触发错误

如何解决“gcloud builds submit”不会因缺少必需的替换而触发错误

我需要一些有关云构建 --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

解决方法

在您的 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: 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?