0

I am aware of the fact that one can run a GitLab pipeline only when a certain condition is satisfied, eg. only when the branch is master or only when a Git Tag is created.

For example:

stages:
  - greet

greet_job:
  stage: greet
  only:
    - master
  script:
    - echo "Hello!"

This runs the greet_job job only when the branch is called master.

I would like to combine two conditions with a logical and, ie. I would like to run a pipeline, say, only when the branch is called master and a new Git Tag has been created. Is that possible?

ADDED

Here I found a possible solution:

  - greet

greet_job:
  stage: greet
  only:
    refs:
      - master
      - tags
    variables:
      - $CI_COMMIT_BRANCH == "master"
  script:
    - echo "Hello!"
EM90
  • 552
  • 1
  • 8
  • 22

1 Answers1

1

You can build fairly complex conditions with rules, which you use should anyway as work on only/except is discontinued.

You can combine two conditions in rules with the && operator, so e.g. run the job only on merge-requests and if $CUSTOM_VARIABLE is true.

 rules:
    - if: '$CUSTOM_VARIABLE == "true" && $CI_PIPELINE_SOURCE == "merge_request_event"'

But checking if the branch is master and a tag was created is not trivial, as there are no predefined varariables for the branch from which a tag was created.

So if you only create tags from master, checking if the pipeline is a tag pipeline wpuld be sufficient.

stages:
  - greet

greet_job:
  stage: greet
  script:
    - echo "Hello!"
  rules:
    - if: '$CI_COMMIT_TAG'
danielnelz
  • 3,794
  • 4
  • 25
  • 35
  • The practice is actually slightly different: tags are created on a branch called `develop`, which is then merged into `master`. – EM90 Jun 10 '21 at 10:34
  • Would it be possible to automatically merge `develop` into `master` (and consequently run a pipeline) each time that a tag is created on `develop` itself? – EM90 Jun 10 '21 at 10:35
  • 1
    It would be possible but tricky. You can take a look at this answer how to merge a branch in a pipeline: https://stackoverflow.com/questions/42113402/how-can-i-make-gitlab-runner-merge-code-into-a-branch-on-a-successful-build – danielnelz Jun 10 '21 at 11:08