Understanding GitLab Scheduled Pipelines and YAML Configuration
In GitLab,
- A scheduled pipeline allows automated execution of jobs at specific times using cron expressions. The .gitlab-ci.yml file defines the pipeline’s structure, including stages, jobs, and the scripts to run. Each job can specify rules or only: [schedules] to control when it runs, ensuring scheduled jobs don’t trigger on pushes or merge requests. When a schedule is created in the GitLab UI, it targets a branch and defines the timing and optional variables.
- At the scheduled time, GitLab triggers a pipeline on the selected branch with CI_PIPELINE_SOURCE="schedule". The runner picks up jobs whose rules match the scheduled context and executes the defined scripts.
- Job logs and statuses are captured in GitLab for monitoring and debugging. Variables defined in the schedule are passed into the pipeline, allowing dynamic configuration. Using stages ensures jobs execute in the intended order even in complex pipelines.
- Overall, this integration provides a reliable, automated mechanism for running repetitive tasks without manual intervention. By combining .gitlab-ci.yml rules and GitLab schedules, teams can fully control what runs and when, maintaining flexibility and efficiency in CI/CD workflows.
Here’s the basic and minimal .gitlab-ci.yml file structure you need to schedule your job every day at 11:11 AM from GitLab.
- This is the simplest version (no workflow, no extra variables):
stages:
- run
scheduled_job:
stage: run
script:
- echo "Scheduled job running at $(date)"
- echo "Put your script logic here"
rules:
# Run only when the pipeline is triggered by a schedule
- if: '$CI_PIPELINE_SOURCE == "schedule"'
when: always
# Prevent job from running on push/merge
- when: never
Steps to Schedule in GitLab:
- Go to Project → CI/CD → Schedules.
- Click New schedule.
- Enter:
- Interval pattern: 11 11 * * *
- Target branch: the branch where the above .gitlab-ci.yml exists (e.g., main)
- Timezone: your local timezone
- Click Save.
Now this job will run every day at 11:11 AM and execute the commands you placed in script:
Top comments (0)