Sobes.tech
Middle

How to create recurring tasks in GitLab CI?

sobes.tech AI

Answer from AI

There are several approaches for creating recurring jobs in GitLab CI:

  1. Pipeline Schedules: This is the most common and intended method. You configure a schedule in the GitLab interface, specifying the branch/tag, variables (if needed), and the execution frequency (cron syntax).
  2. API Triggers: You can use the GitLab API to trigger pipelines from an external system or script. This is useful if the schedule is more complex or depends on external events.
  3. CI/CD variables for conditional execution: Inside the .gitlab-ci.yml file, you can use predefined or custom variables to determine job conditions. Although this does not create a schedule per se, you can trigger a pipeline on a schedule (via Pipeline Schedules) and decide within it which jobs to run based on variables.

Example of using Pipeline Schedules:

In the GitLab interface, go to the repository -> Build -> Pipeline schedules. Create a new schedule, specifying:

  • Description
  • Branch/tag
  • Interval (cron syntax, e.g., 0 0 * * * for daily midnight runs)
  • Variables (optional)

Example of API trigger (using curl):

# Replace Private-Token with your personal access token with API rights
# Replace <your_project_id> with your project ID
# Replace <your_ref_name> with the branch or tag name

curl --request POST \
     --header 'Content-Type: application/json' \
     --header 'Private-Token: <your_private_token>' \
     --data '{
       "ref": "<your_ref_name>",
       "variables": {
         "MY_VARIABLE": "some_value"
       }
     }' \
     'https://gitlab.com/api/v4/projects/<your_project_id>/trigger/pipeline'

Example of using a variable in .gitlab-ci.yml for conditional execution:

stages:
  - build
  - deploy

scheduled_job:
  stage: build
  script:
    - echo "This job runs only when triggered by a schedule"
  only:
    variables:
      - $CI_PIPELINE_SOURCE == "schedule"

other_job:
  stage: deploy
  script:
    - echo "This job runs for all pipeline sources"
  # No `only: variables:` condition

The method choice depends on specific requirements. For simple recurring tasks, Pipeline Schedules are the optimal choice. For more complex scenarios or integration with external systems, API triggers are suitable. Conditional execution with variables is useful for controlling flow within an already running pipeline.