首页 > 解决方案 > 这是在 cloudbuild.yaml 文件中编写 if..else 语句的正确方法吗?

问题描述

我正在尝试使用 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'

我在哪里做错了?

标签: bashgoogle-cloud-platformgoogle-cloud-functionsyamlgoogle-cloud-build

解决方案


从 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

推荐阅读