π 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
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
- Lists all Cloud Scheduler jobs in the source project:
gcloud scheduler jobs list --project="$SOURCE_PROJECT" --location="$SOURCE_LOCATION"
- 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
- Exports the job data (
π 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
The script will list all available jobs.
For each one, it will ask:
wish copy job 'my-job' to project 'destination'? (y/n)
After copying, it will confirm:
β
Job 'my-job' created successfully.
And it will pause the original job:
βΈοΈ Pausing job 'my-job' in source project...
π 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)