This page looks best with JavaScript enabled

How to control test triggers in GitLab for your projects

 ·  ☕ 2 min read  ·  🐧 sysadmin

Here is a video tutorial

Introduction

There are multiple ways to stop GitLab from automatically starting tests upon any commit in a project. Using conditional variables in the .gitlab-ci.yml file to define when tests should be run is one of the easiest methods. Here are some illustrations of how to do this:

1. Using only and except

You can configure jobs in the .gitlab-ci.yml file to run only on specific branches, tags, or commit messages.

Example:

1
2
3
4
5
6
7
8
test:
  script:
    - echo "Running tests"
  only:
    - master
    - /^release-.*$/
  except:
    - /^hotfix-.*$/

In this example, tests will run only in the master branch and in all branches starting with release-, but not in branches starting with hotfix-.

2. Using rules

Rules allow for more complex conditional logic in the .gitlab-ci.yml file.

Example:

1
2
3
4
5
6
test:
  script:
    - echo "Running tests"
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
    - if: '$CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/'

In this example, tests will run only in the master branch or when the commit is tagged with a version pattern (vX.X.X).

3. Using an Environment Variable

You can also use an environment variable to decide whether to run tests.

Example:

1
2
3
4
5
test:
  script:
    - echo "Running tests"
  rules:
    - if: '$RUN_TESTS == "true"'

In this case, you need to set the RUN_TESTS environment variable to true for the tests to run. You can set this variable in the project settings in GitLab or in the commit itself by adding the appropriate variable.

4. Disabling Tests Globally

If you want to disable all tests globally for all commits, you can simply comment out or remove the relevant jobs from the .gitlab-ci.yml file.

Example:

1
2
3
# test:
#   script:
#     - echo "Running tests"

In this way, no tests will run until you uncomment this section.

5. Using [ci skip]

You can also add [ci skip] or [skip ci] to the commit message to skip running CI/CD for that specific commit.

Example commit message:

Commit message [ci skip]
6. Disabling Specific Runners

If you want to disable specific Runners, you can use tags to decide which Runners should be used to run certain jobs.

Example in the .gitlab-ci.yml file:

1
2
3
4
5
test:
  script:
    - echo "Running tests"
  tags:
    - my-special-runner

Then, in the Runner settings in GitLab, make sure the Runner has the appropriate tags assigned.

Choose the solution that best fits your scenario and needs.

Support the author with

sysadmin
WRITTEN BY
sysadmin
QA & Linux Specialist