Rules


Make jobs conditional

Rules define whether to execute a job

At least one successful rule for the job to be executed

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

Formerly only/except which are “not actively developed”

Official documentation of job control


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

See new .gitlab-ci.yml:

git checkout origin/160_gitlab_ci/130_rules -- '*'

Also see GitLab Pages


Make pipelines conditional

Workflow rules define whether to execute a whole pipeline

workflow:
  rules:
  - if: $VAR == "value"
  - if: $VAR2 = "value2"

job_name:
  #...

Conditions are also used in workflow rules


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

Pro tip: Mind the order

Rules are evaluated in-order

First match determines result

Adjust order from most specific…

…to most general


Pro tip: Rule templates

Pipelines often have many jobs

Rules will be repeated multiple times

Combine rules with templates to prevent repetition

.rule-only-web:
  rules:
  - if: $CI_PIPELINE_SOURCE == 'web'

job_name:
  extends:
  - .rule-only-web
  #...

Pro tip: Use CI_DEPLOY_FREEZE with rules

Disable pipeline:

workflow:
  rules:
  - if: '$CI_DEPLOY_FREEZE'
    when: manual
  - when: on_success

Template to disable job:

.freeze-deployment:
  rules:
  - if: '$CI_DEPLOY_FREEZE'
    when: manual
    allow_failure: true
  - when: on_success