Is this the correct way to write if..else statement in cloudbuild.yaml file?

戏子无情 提交于 2020-07-09 11:50:44

问题


I am trying to deploy a cloud function using cloudbuild.yaml. It works fine if I don't use any conditional statement. I am facing an error when I execute my cloudbuild.yaml file with if conditional statement. What is the correct way to write it. Below is my code:

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'

Where am I doing it wrong ?


回答1:


From the github page, https://github.com/GoogleCloudPlatform/cloud-sdk-docker, the entrypoint is not set to gcloud. So you cannot specify the arguments like that.

Good practice for specifying directory is to start with /workspace

Also the right way to write the step should be

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



回答2:


I'm not sure you can do this.

In my case, I use the branch selector in the Cloud build trigger to select which branch (or tag) I want to build from a pattern.



来源:https://stackoverflow.com/questions/62002236/is-this-the-correct-way-to-write-if-else-statement-in-cloudbuild-yaml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!