How to run job on a specific branch using rules in GitLab CI/CD

Viewed 13234

It seems rules replaces only/except functionality in the latests GitLab versions.

Before, specifying that a job had to be executed only for master branch, for example, was very straightforward.

How would that be done with rules?

I'm guessing GitLab provides some variable that specifies the current branch's name, but I cannot find that. The only examples I see are regarding merge requests.

In other words, if I have the following job, how to restrict it to run only in potato branch?

unit_tests:
  stage: test
  script: dotnet vstest test/*UnitTests/bin/Release/**/*UnitTests.dll --Blame
  rules:
    - exists:
      - test/*UnitTests/*UnitTests.csproj
2 Answers

I guess this would be it:

unit_tests:
  stage: test
  script: dotnet vstest test/*UnitTests/bin/Release/**/*UnitTests.dll --Blame
  rules:
    - if: $CI_COMMIT_BRANCH == "potato"
Related