DEV Community

Cover image for Moving Google Cloud Scheduler between projects: A Safe Strategy
Weslley Ribeiro
Weslley Ribeiro

Posted on

Moving Google Cloud Scheduler between projects: A Safe Strategy

πŸ”„ Use Case

You have scheduled jobs using Cloud Scheduler in a project called Project A (e.g., develop), but now you want to migrate them to Project B, where you're centralizing the infrastructure resources of your application.

This approach follows Google's recommended best practices:

  • Centralization: makes maintenance and control easier.
  • Cost organization: improves billing tracking.
  • Automation: avoids manually recreating jobs one by one.

βš™οΈ Prerequisites

Clone the repo at repository it contains the script you’ll need to fill in with your environment variables. Follow the included README.

Edit script variables

At the beginning of the script, modify the values below:

SOURCE_PROJECT="source-project-id"
DESTINATION_PROJECT="destination-project-id"
SOURCE_LOCATION="source-region"         # e.g., us-central1
DESTINATION_LOCATION="destination-region"   # e.g., us-central1
Enter fullscreen mode Exit fullscreen mode

Install required tools

  • gcloud CLI (authenticated in both projects with appropriate permissions)
  • jq (for JSON manipulation)

Required permissions

The account running the script must have permissions to:

  • List, describe, and pause jobs in the source project
  • Create and pause jobs in the destination project

πŸš€ What the script does

  1. Lists all Cloud Scheduler jobs in the source project:
gcloud scheduler jobs list --project="$SOURCE_PROJECT" --location="$SOURCE_LOCATION"
Enter fullscreen mode Exit fullscreen mode
  1. For each job found:
  • Asks if you want to copy it (y/n)
  • If the job already exists in the destination, it notifies and skips
  • Otherwise:
    • Exports the job data (gcloud scheduler jobs describe)
    • Saves it to a file named schedulers.json (for backup/audit)
    • Creates a new job in the destination with the same data (schedule, time zone, HTTP method, headers, body, retries, etc.)
    • Pauses the job in the source project (to avoid duplicate execution)
    • (Optional) Pauses the job in the destination if it was already paused in the source

πŸ“ Script Structure

  • Fully interactive (confirmation for each job)
  • Saves original job JSON data in the schedulers.json file (for later review or re-execution)
  • Supports:
    • httpTarget (including base64 body)
    • Custom headers
    • Retry settings (attempts, backoff, etc.)
    • Paused jobs remain paused in the destination

πŸ’‘ Important Tips

  • Carefully review each job before applying.
  • Use schedulers.json as a backup.
  • After migration, update any systems or variables that depend on job location (e.g., logging, monitoring, or IAM bindings).

πŸ“Œ Practical Example

bash migrate_scheduler_jobs.sh
Enter fullscreen mode Exit fullscreen mode

The script will list all available jobs.

For each one, it will ask:

wish copy job 'my-job' to project 'destination'? (y/n)
Enter fullscreen mode Exit fullscreen mode

After copying, it will confirm:

βœ… Job 'my-job' created successfully.
Enter fullscreen mode Exit fullscreen mode

And it will pause the original job:

⏸️ Pausing job 'my-job' in source project...
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ schedulers.json File

This file serves as an execution history, containing all exported jobs from the source project.

You can later use it to compare jobs between environments or even recreate them manually if needed.


βœ… Conclusion

This script is a practical and safe solution to migrate Cloud Scheduler jobs between different projects in Google Cloud, maintaining configurations intact and reducing the risk of human error.

If you want to evolve this process into a CI/CD scenario, you can automate script calls using environment variables defined in your pipeline.

If you need help adapting or improving it, I can assist!

Top comments (0)