Replace old syntax only - trigger with rules

Hello,

In my gitlab-ci yamls I use:

only:
  - triggers

I know that only is slowly becoming obsolete, although it can still be used, but I’d like to use rules for the new projects in any case.

So how could I replace this so using rules?

I’ve had a look at the documentation (Keyword reference for the `.gitlab-ci.yml` file | GitLab )and they can’t be used together in the same job. If you configure one job to use both keywords, the GitLab returns a key may not be used with rules error., but I’m not sure how I can do it.

rules:
  - if: triggers

maybe? :slight_smile:

Or maybe this?

rules:
  - exists: '$CI_PIPELINE_TRIGGERED'

Hi @lethargos

I think you want something like:

rules:
    - if: "$CI_PIPELINE_SOURCE == 'trigger'"
      when: on_success
    - when: never

The CI_PIPELINE_SOURCE variable is documented here.

2 Likes

Thanks for the reply!
Can I ask what the difference between the two when directives is? I’m not sure I follow.

The first one only applies if the if statement above returns true.

The third line, - when: never is the default for this job. So if "$CI_PIPELINE_SOURCE == 'trigger'" returns false then when: never is applied.

Note the - in front of the second when which starts the next row in the YAML matrix.

1 Like