How to schedule jobs in a pipeline

Here is my .gitlab-ci.yml

default:
    image: python:3.11
job1:
    berfore_script:
        - python -m venv venv
        - source venv/bin/activate
        - pip install -r requirements.txt
    script:
        - which python3
    rules:
        - exists: [".gitlab/schedules/job1.yml"]

job2:
    berfore_script:
        - python -m venv venv
        - source venv/bin/activate
        - pip install -r requirements.txt
    script:
        - python3 -c "print("Hello")"
    rules:
        - exists: [".gitlab/schedules/job2.yml"]

Here is my job1.yml

cron: "0/5 * * * *" # Runs the job every 5 mins
start_in: 0 mins

Here is my job2.yml

cron: "0/10 * * * *" # Runs the job every 10 mins
start_in: 0 mins

On commit the jobs are running, however the successive jobs are not triggering. what needs to be done?

I am not sure what you are trying to achieve. If you want to schedule some jobs, you can use the CI/CD → Schedules in the left menu for that.

The CI/CD Schedule would schedule the whole pipeline ; If i want individual jobs on cron how do i achieve this?

You can use rules to limit when each job is run. Usually, I have jobs that run only during schedule in a different file and use include. Just to make it easier to read CI config.

(this is not a complete config)

daily:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $SCHEDULE_TYPE == "daily"'
  script:
    - echo daily

weekly:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $SCHEDULE_TYPE == "weekly"'
  script:
    - echo weekly

then you setup 2 schedules in GitLab UI and define a variable SCHEDULE_TYPE with correct value in each.

If you don’t want to run some other jobs as part of the schedule you have to add rules for them as well:

rules:
  - if: $CI_PIPELINE_SOURCE == "schedule"
    when: never
2 Likes