【问题标题】:Is this the correct way to write if..else statement in cloudbuild.yaml file?这是在 cloudbuild.yaml 文件中编写 if..else 语句的正确方法吗?
【发布时间】:2020-05-25 12:16:36
【问题描述】:

我正在尝试使用 cloudbuild.yaml 部署云功能。如果我不使用任何条件语句,它工作正常。当我使用 if conditional 语句执行 cloudbuild.yaml 文件时遇到错误。什么是正确的写法。以下是我的代码:

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  id: deploy
  args: 
   - '-c'
   - 'if [ $BRANCH_NAME != "xoxoxoxox" ] 
     then 
        [
          'functions', 'deploy', 'groups', 
          '--region=us-central1',
          '--source=.',
          '--trigger-http', 
          '--runtime=nodejs8', 
          '--entry-point=App', 
          '--allow-unauthenticated',
          '--service-account=xoxoxoxox@appspot.gserviceaccount.com'
        ]
     fi'
  dir: 'API/groups'

我哪里做错了?

【问题讨论】:

  • 这能回答你的问题吗? Google Cloud build conditional step
  • 部分回答。我尝试了您的解决方案,但无法解决问题。可能是这样,我不会遵循你的解决方案。

标签: bash google-cloud-platform google-cloud-functions yaml google-cloud-build


【解决方案1】:

从 github 页面https://github.com/GoogleCloudPlatform/cloud-sdk-docker,入口点未设置为gcloud。所以你不能这样指定参数。

指定目录的好习惯是以/workspace开头

另外正确的步骤写法应该是

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  id: deploy
  dir: '/workspace/API/groups'
  entrypoint: bash
  args: 
   - '-c'
   - |
      if [ $BRANCH_NAME != "xoxoxoxox" ] 
      then 
        gcloud functions deploy groups
        --region=us-central1
        --source=. 
        --trigger-http 
        --runtime=nodejs8 
        --entry-point=App
        --allow-unauthenticated
        --service-account=xoxoxoxox@appspot.gserviceaccount.com
      fi

【讨论】:

  • 这里不需要&&,因为该命令在sequence 中执行,而不是chained
  • 这是correct 代码。谢谢你的时间。 :)
  • 如何检查云调度程序是否已经存在?这样我的云构建就不会说 ERROR: (gcloud.scheduler.jobs.create.http) ALREADY_EXISTS: ?
【解决方案2】:

我不确定你能做到这一点。

在我的例子中,我使用云构建触发器中的分支选择器来选择我想从一个模式构建哪个分支(或标签)。

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    相关资源
    最近更新 更多