1

We have the following in our cloudbuild.yaml file and it is creating our cloud scheduler job as expected.

    steps:
    # deploy cloud scheduler job
    - name: "gcr.io/cloud-builders/gcloud"
      args: ["scheduler", "jobs", "create", "pubsub", "my_job_name", "--schedule=0 0 * * 1-5", "--topic=my_topic", "--message-body=My Message", "--description=My Description", "--time-zone=America/MyTimeZone"]

However, once cloud build is triggered again we get the following error:

    ERROR: (gcloud.scheduler.jobs.create.pubsub) ALREADY_EXISTS: ....

We would like to keep this in our cloudbuild rather than manually creating in console. How would we, if possible, restructure the build steps and/or maybe add a cloud function in python37 that would check if the job exists, if not create. If job exists and no changes, continue. If changes are present then update.

Any feedback, suggestions or examples provided will be appreciated!

Thanks

Brian N
  • 23
  • 5

2 Answers2

2

You can build conditionals with the gcloud Cloud Builder and bash within your cloudbuild.yaml. See this creative answer for inspiration.

I.E. you use a check with gcloud beta scheduler jobs list and/or gcloud beta scheduler jobs describe as a conditional before you create the schedule.

YaguraStation
  • 558
  • 2
  • 12
1

I am using a pipe to ignore this error. If the scheduler exists already, it just prints out the error and keeps the build moving forward.

- name: 'gcr.io/cloud-builders/gcloud'
  id: scheduler
  waitFor: ['sensor']
  entrypoint: bash
  args:
   - '-c'
   - |
      gcloud scheduler jobs create http NAME --schedule="* * * * *" --uri="uri" ||  echo "Scheduler email-sensor already exist";

Not the best solution but it works.

M-sAnNan
  • 704
  • 8
  • 15