Gitlab CI AND operator in rules

Viewed 1443

In Gitlab CI rules, Gitlab check if one of the rules is present then he choose to add the job or not. I am looking to check all the conditions and if they're all OK he can add the job, otherwise he must no add it to the pipeline:

  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"'
    - changes:
      - filder1/*.xml

In this example i try to tell gitlab to add the job only if some xml files had changed and there is a merge request to be merged the main branch.

1 Answers

You should just combine both conditions into one mapping. I.e., remove extra dash before changes:

  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"'
      changes:
        - filder1/*.xml

But please also take into account that default action is on_success, so you should add another mapping with never to prevent the job from adding:

  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"'
      changes:
        - filder1/*.xml
      when: on_success
    - when: never
Related