Feedback

Chat Icon

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Conditional Jobs and Pipelines
62%

Logical Operators and Order of Conditions

When you have multiple conditions in the rules keyword, the order of the conditions is important. We will demonstrate this with an example. Run the following commands to create it and push the code to the main branch with the no-ci commit message. Observe the output of the pipeline.

cat <$HOME/todo/app/.gitlab-ci.yml && \
cd $HOME/todo/app && \
git add . && \
git commit -m "no-ci" && \
git push origin main
build_job:
  stage: build
  script:
    - echo "running build_job"
  rules:
    - if: '\$CI_COMMIT_MESSAGE =~ /no-ci/'
      when: never      
    - if: '\$CI_COMMIT_BRANCH == "main"'
    - if: '\$CI_COMMIT_BRANCH == "dev"'
    - if: '\$CI_COMMIT_BRANCH == "staging"'
    - if: '\$CI_COMMIT_BRANCH == "production"'
EOF

In this case, the build_job job will run only when one of the following conditions is met:

1) The commit message does not contain the word no-ci. This condition is checked first and it will be met because the commit message contains the word no-ci.
2) The branch name is main, dev, staging, or production. GitLab will ignore these conditions because the first condition is already met.

ℹ️ If the branch name is main and the commit message contains the word no-ci, the job will never run.

If we take the same example and only change the order of the conditions, the job will run because the when: never condition will be ignored:

cat <$HOME/todo/app/.gitlab-ci.yml && \
cd $HOME/todo/app && \
git add . && \
git commit -m "no-ci" && \
git push origin main
build_job:
  stage: build
  script:
    - echo "running build_job"
  rules:
    - if: '\$CI_COMMIT_BRANCH == "main"'
    - if: '\$CI_COMMIT_BRANCH == "dev"'
    - if: '\$CI_COMMIT_BRANCH == "staging"'
    - if: '\$CI_COMMIT_BRANCH == "production"'
    - if: '\$CI_COMMIT_MESSAGE =~ /no-ci/'
      when: never
EOF

ℹ️ The order of the conditions is important. The job will run if at least one of the conditions is met while respecting the order of the conditions.

Another Example:

In this example we want to launch a pipeline only when the branch name is experiment so we will use the following configuration:

cd $HOME/todo/app && \
git checkout -b no-experiment || git checkout no-experiment && \
cat <$HOME/todo/app/.gitlab-ci.yml && \
git add . && \
git commit -m "testing" && \
git push origin no-experiment
build_job:
  stage: build
  script:
    - echo "running build_job"
  rules:
    - if: '\$CI_COMMIT_MESSAGE =~ /testing/'
    - if: '\$CI_COMMIT_BRANCH == "experiment"'
EOF

If you go to the GitLab UI and check the pipeline, you will see that the job will run even if the branch name is no-experiment. This is because the first condition is met.

If we switch the order of the conditions, the job will also run even if the branch name is no-experiment. This is because the second condition is met.

Cloud Native CI/CD with GitLab

From Commit to Production Ready

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