2

I am trying to implement the following functionality in Gitlab CI. I would like to execute a job whenever BOTH conditions are true:

  1. A merge request is made to the master branch
  2. A git commit tag is present.

Again, I would like to run the job only if BOTH conditions are met.

In the past, this could be accomplished by using the only and except keywords. As these are deprecated for some time now, I would like to implement the functionality using the rules and if keywords/statements.

My first attempt looks like this:

job1: script: - echo "Do some stuff ..." rules: - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" && $CI_COMMIT_TAG != ""' 

However, that doesn't seem to work for reasons I can't fathom. Can anyone help me with this?

In this context, I also wondered if maybe the rounding of the two conditions is causing the problem and if it might be more appropriate to specify the conditions in multiple if statements like so:

job1: script: - echo "Do some stuff ..." rules: - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"' - if: '$CI_COMMIT_TAG != ""' 

A corresponding Google search did not bring me closer to a solution. I could not find anything on the topic of multiple if statements. Maybe someone else knows more?

Any help in this matter would be greatly appreciated.

2
  • 3
    docs.gitlab.com/ee/ci/yaml/#rulesif <= If an if statement is true, add the job to the pipeline So you definitely don't want to separate your condition else the job will be added it either is true. Your first try looks ok but your are not describing "doesn't seem to work" with enough details. Which debugging steps did you take ? Commented Jul 30, 2021 at 16:10
  • After debugging a bit it seems to me that the $CI_COMMIT_TAG variable doesn't contain anything when creating a merge request to the master branch (using merge pipelines). It's simply empty, thats why any job with that condition is never going to run. I have no idea how to get around this though. I seems fairly common to me that one wants to create a new release whenever I merge the develop into the master with a new version number as git tag? Why is this so hard? Commented Aug 2, 2021 at 11:27

2 Answers 2

2

Those two variables will never both contain a value since they're defined as separate types of Pipelines. From the Predefined Variables page, the CI_COMMIT_TAG variable will only exist in Tag pipelines, and CI_MERGE_REQUEST_TARGET_BRANCH_NAME, along with all the other CI_MERGE_REQUST_* variables, only exist in Merge Request pipelines.

However there's another reason they'll never be set other than the way Gitlab chose to implement pipelines.

In Gitlab merge requests, there are only three states (as seen in the document linked above under the CI_MERGE_REQUEST_EVENT_TYPE): detached (not yet merged), merged_result (has been merged), or merge_train (basically the same as detached, but indicates the Merge Request is part of a Merge Train). Each of these can only ever exist as part of a branch.

When detached, there are two branches waiting to be merged. For the merge result, the source branch was just merged into the target branch, but the pipeline is still for a branch. For a merge_train event, it's treated the same as detached.

I'd take a look at some of the other variables available and see if you can limit your jobs the way you want with another combination of variables.

Sign up to request clarification or add additional context in comments.

Comments

0

The following example uses if to define that the job runs in only two specific cases:

job: script: echo "Hello, Rules!" rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" when: manual allow_failure: true - if: $CI_PIPELINE_SOURCE == "schedule" 

Alternatively, you can define a set of rules to exclude jobs in a few cases, but run them in all other cases:

job: script: echo "Hello, Rules!" rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" when: never - if: $CI_PIPELINE_SOURCE == "schedule" when: never - when: on_success 

Source: GitLab Documentation

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.