Charles
Charles

Reputation: 11

gcloud scheduler create jobs when already exist

I create my gcloud scheduler in command line with

gcloud scheduler jobs create

but when I already deploy my gitlabCI, i got already exist error.

is it possible to overwrite if already exist directly in my gitlabCI ?

Upvotes: 0

Views: 1327

Answers (2)

Banty
Banty

Reputation: 961

You can try the following steps in your .gitlab-ci file:

  1. list all scheduler jobs and use a filter to narrow down to your scheduler job. Use something unique to your scheduler job, e.g. description?
  2. Delete the existing job.
  3. Re-create your scheduler job
    # list scheduler jobs
    - >
      gcloud scheduler jobs list
      --location="LOCATION"
      --filter "description='The description for your existing scheduler job'"
      --project "YOUR_PROJECT_ID"
      --format json
      > list.json
    
    # delete the existing scheduler job
    - >
      for i in $(jq -r .[].name list.json); do
        gcloud scheduler jobs delete $i --quiet --project $YOUR_PROJECT_ID || echo "Failed to delete $i"
      done

    # now re-create your scheduler job
    - >
      gcloud scheduler jobs create your-scheduler-job

Upvotes: 0

nakhodkin
nakhodkin

Reputation: 1465

Suppose you create a Cloud Schedule job with the following attribute values

gcloud scheduler jobs create JOB --location=LOCATION
JOB LOCATION
my-job us-west1
gcloud scheduler jobs create my-job --location=us-west1

In order to verify if the job already exists you may use the gcloud schedule jobs describe JOB command using gcloud CLI .e.g https://cloud.google.com/sdk/gcloud/reference/scheduler/jobs/describe

gcloud scheduler jobs describe my-job --location=us-west1

If it indeed already exists, there is no direct way of "overwriting" the existing one, what you can do is to

  • either delete the previous job and re-create it from scratch e.g.
gcloud scheduler jobs delete my-job
gcloud scheduler jobs create my-job
  • or you can modify the existing job, for instance when you deploy a new version of a service to AppEngine, you can simply reflect this on your existing Cloud Scheduler job without the need to re-creating it entirely.
gcloud scheduler jobs update app-engine my-job --version=VERSION

For more information, please refer to the official documentation for Cloud SDK on Cloud Scheduler https://cloud.google.com/sdk/gcloud/reference/scheduler

Upvotes: 0

Related Questions