Reputation: 1307
I am trying to implement the following functionality in Gitlab CI. I would like to execute a job whenever BOTH conditions are true:
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.
Upvotes: 1
Views: 5003
Reputation: 787
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
Upvotes: 0
Reputation: 7649
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.
Upvotes: 2