Rules


Make jobs conditional

Rules define whether to execute a job

Pipeline requires one successful rule for the job to be added

job_name:
  rules:
  - if: $VAR == "value"
  - if: $VAR2 = "value2"
  # ...

Conditions are also used in workflow rules

Official documentation of job control

Formerly only/except but “not actively developed”


Hands-On: Rules

Run the deploy job only for the main branch

  1. Create folder public in repository
  2. Add files from public/ to new folder public
  3. Update .gitlab-ci.yml
  4. Check pipeline
  5. Go to Settings > Pages
  6. Open URL for pages
  7. Create branch
  8. Check pipeline

Also see GitLab Pages


Hands-On: Workflow rules

Disable execution for some trigger types

workflow:
  rules:
  - if: $CI_PIPELINE_SOURCE == 'push'
  - if: $CI_PIPELINE_SOURCE == 'web'
  - if: $CI_PIPELINE_SOURCE == 'schedule'
  - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  - if: $CI_PIPELINE_SOURCE == 'pipeline'
  - if: $CI_PIPELINE_SOURCE == 'api'
    when: never
  - if: $CI_PIPELINE_SOURCE == 'trigger'
    when: never