How to run a gitlab job except a variable has some value?

Viewed 1035

I want to run a gitlab job in every case EXCEPT the variable RUN is defined and contains the value run3.

So again:

  • variable RUN is undefined: run the job
  • variable RUN is defined and does not contain the value run3: run the job
  • variable RUN is defined and does contain the value run3: DO NOT run the job

I tried the following rules:

  rules:
    - if: $RUN != null && $RUN == "run3"
      when: never

  rules:
    - if: $RUN != "" && $RUN == "run3"
      when: never

  rules:
    - if: $RUN == "run3"
      when: never 

Without the variable RUN being defined, no job was run. I expected the job to have been run.

How to fix it? Where is the problem in the logic?

3 Answers

After some time-consuming try-and-error trials I found the solution: As you cannot test if a variable exists, you have to create the rule the other way around. It means, you run the job if the value does not match:

The correct rule is then:

- if: $RUN != "run3"
  when: always
- when: never

The first part only evaluates to true if the variable exists AND if the variable does not evaluate to run3. In that case the job is run.

The second part then tells to NOT run the job in any other case.

Alex, maybe you can try:

if: $RUN != undefined && $RUN == "run3"
      when: never

You're close

  rules:
    - if: $RUN == "run3"
      when: never 

This is correct, but you also need a default - when:

To emulate the default behavior of no rules when the first rule does not match, set the default as on_success

  rules:
    - if: $RUN == "run3"
      when: never # don't run the job in this case
    - when: on_success  # run per usual in all other cases
Related