Feedback

Chat Icon

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Driving Job Execution, Timing, and Dependencies with Conditions and Rules
58%

Using Conditions Based on Files

Sometimes you want to run a job only when a file exists or has been modified. In this case, you can use the exists and changes keywords. Let's see some examples.

Example:

You want to run a job only when the file ok exists:

build_job:
  stage: build
  script:
    - echo "running build_job"
  rules:
    - exists:
      - ok

Example:

You want to run a job only when the file requirements.txt has been modified:

build_job:
  stage: build
  script:
    - echo "running build_job"
  rules:
    - changes:
      - requirements.txt

The modifications are detected differently depending on the pipeline type:

  • For branch pipelines, changes are detected compared to the latest commit on the target branch.
  • For merge request pipelines, changes are detected compared to the source branch of the merge request.
  • For new branches, changes are evaluated to true.

Example:

You want to run a job only when the file requirements.txt has been modified and ok exists:

build_job:
  stage: build
  script:
    - echo "running build_job"
  rules:
    - changes:
      - requirements.txt
    - exists:
      - ok

Example:

You want to run a job only when the file requirements.txt or Dockerfile has been modified:

build_job:
  stage: build
  script:
    - echo "running build_job"

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Enroll now to unlock all content and receive all future updates for free.