Jakub Bouček
Jakub Bouček

Reputation: 1405

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? 🙏

Upvotes: 8

Views: 9419

Answers (2)

Guilhem Bonnefille
Guilhem Bonnefille

Reputation: 11

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

If you want to run only on merge request, you have to invert the logic:

    - if: '$CI_PIPELINE_SOURCE != "merge_request_event"'
      when: never

Upvotes: 0

Tolis Gerodimos
Tolis Gerodimos

Reputation: 4408

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

Upvotes: 8

Related Questions