Gitlab CI - is there a difference between a rule with when:always to a rule without?
Asked Answered
T

1

6

I have a pipeline in Gitlab-CI and one of the jobs has the rule:

rules:
  - if: "$CI_PIPELINE_SOURCE == "merge_request_event"

Will there be a difference if I will change it to:

  - if: "$CI_PIPELINE_SOURCE == "merge_request_event"
    when: always

Because, the first rule will execute when the condition is met and the alternative rule ( the one with the when: always ) will do the same thing.

So what is the "when: always" will change? if it will change anything at all.

Tapdance answered 27/3, 2023 at 6:49 Comment(0)
W
14

Yes, there is a difference. By default, the value of when: is on_success -- jobs only run if jobs in previous stages succeed. Using always will allow the job to run, even if jobs in the previous stage failed.

Per the docs these are the possible values for when:

on_success (default): Run the job only when all jobs in earlier stages succeed or have allow_failure: true.
manual: Run the job only when triggered manually.
always: Run the job regardless of the status of jobs in earlier stages. Can also be used in workflow:rules.
on_failure: Run the job only when at least one job in an earlier stage fails. A job with allow_failure: true is always considered successful.
delayed: Delay the execution of a job for a specified duration.
never: Don’t run the job. Can only be used in a rules section or workflow: rules.

But if you did the following, it would be effectively the same as the first example:

  - if: "$CI_PIPELINE_SOURCE == "merge_request_event"
    when: on_success

This also assumes when: is not used in the job body.

If you do have when: in the body AND in your rules, this changes a bit.

myjob:
  when: always
  rules:
    - if: ...
      when: on_success  # this takes precedence if this rule matches
    - if: ...  # uses the default "when:" if this rule matches

In which case, adding the when: to the rules, the when: specified in the rules will take precedence over the when: set in the job body when the rule matches.

In older versions of GitLab, mixing job:when: and job:rules:when: is not allowed and results in an error.

Whithersoever answered 27/3, 2023 at 7:14 Comment(2)
is there any option like - if: always or - if: true I couldn't find a flag that returns always true.Scenography
@ÖmerGÜZEL - if: $CI should always evaluate true.Whithersoever

© 2022 - 2024 — McMap. All rights reserved.