Sobes.tech
Senior

What is rules in GitLab CI? What is workflow used for?

sobes.tech AI

Answer from AI

In GitLab CI, rules are a flexible mechanism for defining conditions under which a specific job is run or skipped. They allow you to set logic based on branches, variables, events, and other parameters.

For example, you can configure a job to run only on pushes to the main branch or when a certain environment variable is present.

workflow is a setting that manages the execution of the entire pipeline. Using workflow: rules, you can determine when to run or skip the entire pipeline based on conditions.

This is useful for resource optimization, for example, to avoid running pipelines on documentation branches or when changes do not affect the code.

Example of rules for a job:

job:
  script: echo "Run job"
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
      when: always
    - when: never

Example of workflow:

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
      when: always
    - when: never