Execute jobs on a schedule

This page describes how to execute Cloud Run jobs on a schedule using Cloud Scheduler.

Before you begin

Create a Cloud Run job.

Configure a Cloud Run job to execute on a schedule

To execute a Cloud Run job on a schedule:

Console

  1. Go to Cloud Run jobs

  2. Click the job you want to execute on a schedule.

  3. Click the Triggers tab.

  4. Click Add Scheduler Trigger.

  5. If you haven't yet enabled the Cloud Scheduler API for your project, you are prompted to do so in the far right panel: click Enable API.

  6. Fill out the Cloud Scheduler job form

    image

  7. Under Define a schedule:

    1. Give your Cloud Scheduler job a name.

    2. Select a region for your Cloud Scheduler job. It does not need to match the region used for the Cloud Run job.

    3. Specify the frequency for your job execution, using the unix-cron format, for example, 0 12 * * *

    4. Select your timezone.

  8. Click Continue.

  9. In the Service Account dropdown menu, select a service account that has the permission to invoke the current Cloud Run service.

  10. Click Create to create the Cloud Scheduler job that will execute the Cloud Run job at the specified frequency.

Command line

  1. Make sure you create a Cloud Run job first.

  2. Run the command:

    gcloud scheduler jobs create http SCHEDULER_JOB_NAME \
      --location SCHEDULER_REGION \
      --schedule="SCHEDULE" \
      --uri="https://CLOUD_RUN_REGION-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/PROJECT-ID/jobs/JOB-NAME:run" \
      --http-method POST \
      --oauth-service-account-email PROJECT-NUMBER-compute@developer.gserviceaccount.com

    Replace

    • SCHEDULER_JOB_NAME with the name you want to give your scheduler job.
    • SCHEDULER_REGION with a region supported by Cloud Scheduler, for example, europe-west2.
    • CLOUD_RUN_REGION with the region for the your Cloud Run job. For preview, use europe-west9.
    • SCHEDULE with the desired frequency, for example 0 12 * * *
    • PROJECT-ID with your project ID.
    • PROJECT-NUMBER with your project number.
    • JOB-NAME with your Cloud Run job.

Terraform

To create a Cloud Scheduler job that executes a Cloud Run job:

resource "google_cloud_scheduler_job" "job" {
  provider         = google-beta
  name             = "schedule-job"
  description      = "test http job"
  schedule         = "*/8 * * * *"
  attempt_deadline = "320s"
  region           = "us-central1"
  project          = data.google_project.project.project_id

  retry_config {
    retry_count = 3
  }

  http_target {
    http_method = "POST"
    uri         = "https://${google_cloud_run_v2_job.default.location}-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${data.google_project.project.number}/jobs/${google_cloud_run_v2_job.default.name}:run"

    oauth_token {
      service_account_email = google_service_account.cloud_run_invoker_sa.email
    }
  }

  depends_on = [resource.google_project_service.cloudscheduler_api, resource.google_cloud_run_v2_job.default, resource.google_cloud_run_v2_job_iam_binding.binding]
}

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

Cloud Scheduler will execute the Cloud Run job at the specified frequency.

What's next

After you use this feature, you can do the following: