TL;DR
Learn how to effectively use GitLab Pipeline Schedules to automate your CI/CD workflows. This guide covers schedule creation, configuration, and best practices.
GitLab Pipeline Schedules: Automating CI/CD Workflows
Executive Summary
Pipeline schedules run a project's pipeline on a cron interval against a chosen branch, with variables you define per schedule. They are the right tool for work that must happen whether or not code changed: nightly builds, dependency and security scans, database maintenance, report generation, and deployments that follow a calendar rather than a merge. This guide explains how schedules actually behave, including the detail most teams miss, that a schedule runs the pipeline defined on the target branch and only runs jobs whose rules accept a scheduled pipeline source. You will learn how to scope jobs with rules and schedule-specific variables, run several schedules against one pipeline definition without collisions, and monitor scheduled work so a silently failing nightly job is discovered by an alert rather than by a customer.
Why This Matters
Scheduled work is where automation quietly rots. A nightly build that has failed for three weeks is worse than no nightly build, because the team believes the safety net exists. A scheduled deployment that fires against the wrong branch, or with a variable left over from testing, acts at machine speed with no human watching. Schedules also concentrate load: if every project in a group runs its heavy job at midnight, the runner fleet experiences a self-inflicted denial of service at the same time every night. Doing schedules well means treating each one as a small production system, with an owner, a purpose, a defined branch and variable set, failure notification, and a reason to exist that someone can still state six months later.
Core Concepts
What a Schedule Actually Runs
A schedule belongs to a project, targets one branch or tag, and carries a cron expression, a timezone, an active flag, and optional variables. When it fires, GitLab creates a pipeline for that ref with the pipeline source set to schedule. Two consequences follow. First, the pipeline definition used is the one committed on the target ref at that moment, so changing the schedule's behaviour usually means changing code on that branch. Second, jobs decide individually whether to join a scheduled pipeline, through their rules or legacy only clauses. A schedule does not run your merge pipeline unchanged; it runs the subset of jobs that opt in to the schedule source.
# .gitlab-ci.yml
nightly_build:
script:
- echo "Running nightly build"
- ./build.sh
only:
- schedulesNew configuration should prefer rules over only, because rules compose with variables and with manual or delayed execution.
Cron Expressions and Timezones
Schedules use standard five-field cron syntax, interpreted in the timezone configured on the schedule.
# Common cron patterns 0 0 * * * # Daily at midnight 0 */4 * * * # Every 4 hours 0 0 * * 0 # Weekly on Sunday 0 0 1 * * # Monthly on the 1st
Set the timezone explicitly and write it down in the schedule description. Most schedule confusion is timezone confusion: a job expected at the start of the business day in one region runs in the middle of the night in another, or a maintenance window is missed after a daylight-saving change. Stagger heavy schedules across the hour and across the fleet instead of aligning them all on the hour.
Scoping Jobs with Rules and Variables
The standard pattern is one pipeline definition serving several schedules, with each schedule setting a distinguishing variable and jobs matching on both the pipeline source and that variable.
deploy_production:
script:
- deploy_to_production.sh
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
when: manual
- when: never# .gitlab-ci.yml with variables
nightly_build:
variables:
ENVIRONMENT: "staging"
BUILD_TYPE: "nightly"
script:
- echo "Building for $ENVIRONMENT"
- ./build.sh --type $BUILD_TYPE
only:
- schedulesSchedule variables override pipeline and job variables for that run, which makes them powerful and dangerous in equal measure: a variable left set from an experiment silently changes every future run. Keep schedule variables few, documented in the schedule description, and reviewed when the schedule is created and when it is handed over.
Multiple Schedules, One Repository
Different schedules commonly target different purposes against the same branch: a nightly full build, a weekly deep scan, a monthly cleanup. Separate them by variable and by job design rather than by duplicating pipeline files.
# .gitlab-ci.yml
stages:
- build
- test
- deploy
nightly_staging:
stage: deploy
script:
- deploy_to_staging.sh
rules:
- if: $CI_PIPELINE_SOURCE == "schedule" && $SCHEDULE_TYPE == "staging"
when: on_success
weekly_production:
stage: deploy
script:
- deploy_to_production.sh
rules:
- if: $CI_PIPELINE_SOURCE == "schedule" && $SCHEDULE_TYPE == "production"
when: manualNote the production pattern: the schedule creates the pipeline, but the deploy job itself stays manual. Scheduling the opportunity to deploy is usually wiser than scheduling the deployment.
Designing the Scheduled Jobs Themselves
Scheduled jobs should be idempotent, because they will eventually run twice, after a retry, a manual trigger, or a schedule edit. They should also be economical: skip the clone when the job does not need source, run heavy work off-peak on appropriately tagged runners, and produce a durable artifact or report so the run leaves evidence.
# Resource-intensive job during off-peak hours
data_processing:
script:
- process_large_dataset.sh
only:
- schedules
tags:
- high-cpu
variables:
GIT_STRATEGY: none # Skip git clone if not neededFor a multi-stage scheduled pipeline, keep the stages honest: preparation, execution, verification, and notification, with the deploy or publish step gated on the verification step rather than merely following it.
Failure Handling and Notification
A scheduled pipeline has no author waiting for the result, so failure handling must be designed. Use after_script or a final notification job to report status to a channel a human actually reads, and make the notification specific: schedule name, branch, and a link to the pipeline.
nightly_build:
script:
- set -e # Exit on any error
- ./build.sh
after_script:
- |
if [ $CI_JOB_STATUS == "failed" ]; then
notify_team.sh "Nightly build failed"
fi
only:
- schedules# Slack notification for scheduled jobs
.notify_slack: ¬ify_slack
- |
curl -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"Pipeline $CI_PIPELINE_ID: $CI_JOB_STATUS\"}" \
$SLACK_WEBHOOK_URL
scheduled_deployment:
script:
- deploy_application.sh
after_script:
- *notify_slack
only:
- schedulesDistinguish failure of the work from failure of the schedule: if the pipeline was never created, no job-level notification can fire, which is why the schedule list itself needs a periodic human or automated review for last-run status.
Ownership, Takeover, and Lifecycle
Schedules are owned by a user, and pipelines they create run with that owner's permissions. When the owner leaves or loses access, schedules can stop working. Plan for takeover: document each schedule's purpose and owner in its description, transfer ownership deliberately during handovers, and delete schedules whose purpose has ended. A quarterly pass over the schedule list, removing the dead and confirming the live, is cheap insurance against both silent failures and forgotten automation with production credentials.
Troubleshooting
When a schedule does not run, check in order: the schedule is active, its cron and timezone say what you think they say, the target branch still exists and its pipeline definition is valid, the schedule owner still has access, and at least one job's rules accept the schedule source. When a schedule runs the wrong jobs, the cause is almost always rules: a job with no source restriction joins every pipeline, including scheduled ones, and does work nobody intended at 2 AM. Debug by echoing the source and schedule variables in a harmless job before changing production behaviour.
# Add debug information
scheduled_job:
script:
- echo "Schedule Type: $SCHEDULE_TYPE"
- echo "Pipeline Source: $CI_PIPELINE_SOURCE"
- echo "Current Time: $(date)"
- your_actual_script.sh
only:
- schedulesDecision Framework
| Situation | Recommendation |
|---|---|
| Full test suite too slow for every merge | Run a fast subset per merge and the full suite on a nightly schedule |
| Security and dependency scanning | Schedule scans on the default branch, with findings triaged on a fixed cadence |
| Deployment follows a business calendar | Schedule the pipeline, keep the production deploy job manual or approved |
| Backup, cleanup, or report generation tied to this repository | A scheduled pipeline is appropriate, with artifacts or an external record as evidence |
| Recurring task unrelated to the repository's code | Use a platform scheduler or the operating system's tooling instead of a pipeline schedule |
| Many projects scheduling heavy jobs | Stagger cron times deliberately across the fleet instead of aligning on the hour |
| Schedule has not been reviewed since creation | Audit it: confirm owner, branch, variables, and that its output is still consumed |
Implementation Checklist
Trade-offs
Schedules trade immediacy for regularity: a nightly scan finds a vulnerability hours after it lands, in exchange for a merge pipeline that stays fast. Concentrating heavy work off-peak protects daytime capacity and creates a nightly peak of its own. Schedule variables keep one pipeline definition serving many purposes and concentrate risk in configuration that is easy to forget and hard to review in a merge request. There is also a visibility cost: scheduled pipelines run when nobody is looking, so every control, notification, ownership, alerting, has to compensate for the absent human.
When Not to Use
Do not use a pipeline schedule as a general-purpose cron for work unrelated to the project; it inherits repository permissions and history it does not need, and a scheduler, a cloud job service, or the target system's own tooling will fit better. Do not schedule deployments that require judgement about current system state; schedule the check and let a human decide. Do not create a schedule to poll for external changes that the external system can push to you through a webhook or a trigger. And do not keep a schedule whose output nobody reads: an unconsumed report is not monitoring, it is litter with credentials.
Conclusion
A good schedule is a small, owned, observable system: explicit about when it runs, what it runs, and who hears about failure. Scope jobs with rules, keep variables documented, stagger the load, and audit the list regularly. Teams that do this get the real benefit of schedules, which is not automation in the abstract but confidence that the unglamorous recurring work actually happened.
References
Why This Matters
Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.
Trade-offs to Consider
Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.
When NOT to Use This
Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.
Decision Framework
Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.