Search code examples
gitlab-cipipeline

How to combine GitLab CI job for rule:if with matrix and allow_failure?


I need configure my GitLab CI job like this:

  1. Only job with $CI_PIPELINE_SOURCE == "merge_request_event" is added to Pipeline,
  2. Job is runned multipletimes by matrix for each version defined by matrix PHP_VERSION: [ '7.4', '8.0', '8.1' ].
  3. The '8.1' must but runned with allow_failure: true.

I tried to write rules intuitive as I except rules works, but I'm getting a different result.

I first tried this:

parallel:
matrix:
  - PHP_VERSION: [ '7.4', '8.0', '8.1' ]
rules:
  - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    when: on_success
  - if: '$PHP_VERSION == "8.1"'
    allow_failure: true

It result only to MR event for PHP 8.1 us added to Pipeline.

My next iteration is still wrong:

  parallel:
    matrix:
      - PHP_VERSION: [ '7.4', '8.0', '8.1' ]
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: on_success
    - if: '$PHP_VERSION == "8.1"'
      when: on_success
      allow_failure: true
    - when: on_success

This looks better, but it runs job for every other event (not only merge_request_event).

How I can to right combine rules to get result as I declared above? 🙏


Solution

  • You could try

      parallel:
        matrix:
          - PHP_VERSION: [ '7.4', '8.0', '8.1' ]
      rules:
        - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
          when: on_success
        - if: '$PHP_VERSION == "8.1"'
          when: on_success
          allow_failure: true
        - if: '$PHP_VERSION'
          when: on_success
          allow_failure: false
    

    Based on https://docs.gitlab.com/ee/ci/jobs/job_control.html#run-a-matrix-of-parallel-trigger-jobs

    Example of execution

    enter image description here