1

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 ?

Pritish
  • 658
  • 3
  • 16
  • 38
  • 1
    Does this answer your question? [Google Cloud build conditional step](https://stackoverflow.com/questions/58235945/google-cloud-build-conditional-step) – guillaume blaquiere May 25 '20 at 19:24
  • It answers partially. I tried with your solution but was unable to solve the issue. Might be that, I wouldn't have followed your solution. – Pritish May 25 '20 at 20:53

3 Answers3

4

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
Tranvu Xuannhat
  • 524
  • 3
  • 6
  • 2
    Here `&&` isn't required since the command is executed in `sequence` but not `chained`. – Pritish May 26 '20 at 06:50
  • 1
    This is the [correct](https://stackoverflow.com/questions/61912467/adding-a-are-you-sure-dialog-in-gcp-cloudbuild-yaml/61920593?noredirect=1#comment109675799_61920593) code. Thanks for your time. :) – Pritish May 26 '20 at 10:21
  • how can I check if the cloud scheduler exists already? So that my cloud build won't by saying ERROR: (gcloud.scheduler.jobs.create.http) ALREADY_EXISTS: ? – M-sAnNan Aug 10 '20 at 09:53
0

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.

Olivier Lépine
  • 618
  • 5
  • 14
0

I wanted to delete the latest version of each service only if there were more than two previous versions. This was my solution.

args:
- "-c"
- |
  if [[ $(gcloud app versions list --format="value(version.id)" --service=MY-SERVICE | wc -l) -ge 2 ]];
  then
    gcloud app versions list --format="value(version.id)" --sort-by="~version.createTime" --service=admin | tail -n -1 | xargs gcloud app versions delete --service=MY-SERVICE --quiet;
  fi
Arturo Bernal
  • 31
  • 1
  • 1