Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gitlab-ci

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
like image 347
diegosasw Avatar asked Nov 28 '25 17:11

diegosasw


2 Answers

Here are the variable references:

https://docs.gitlab.com/ee/ci/variables/predefined_variables.html


Here is an example from gitlab-runner project source code itself

https://gitlab.com/gitlab-org/gitlab-runner/-/blob/main/.gitlab/ci/test.gitlab-ci.yml

job-name: 
  script:
    - echo "i am potato"
  rules:
    - if: '$CI_COMMIT_BRANCH == "potato"'
like image 136
Jayakrishnan Karolil Avatar answered Dec 01 '25 05:12

Jayakrishnan Karolil


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"
like image 43
psergiocf Avatar answered Dec 01 '25 06:12

psergiocf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!