24

I want to create some nested conditions: i need this pipeline to work when it is a merge or merge request and with certain name start "feature". So, is there an AND condition in the 'only' option for jobs?

Claudio Torres
  • 355
  • 1
  • 2
  • 6

2 Answers2

32

No there is not. You must use rules.

test:
  stage: test
  script:
    - echo "test"
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TITLE =~ /^feature/'
danielnelz
  • 3,794
  • 4
  • 25
  • 35
  • While you can have multiple `if` statements. Combined with `when`, right? – Melroy van den Berg Dec 15 '21 at 22:25
  • Is multiline possible so the && chain stays readable? – andymel Apr 28 '22 at 13:14
  • 1
    @andymel as far as I know that's unfortunately not possible. Multiline commands can only be used in `script`, `before_script` and `after_script` – danielnelz Apr 28 '22 at 13:49
  • 1
    @andymel - whiel you can't have them "chained" with the - > or - | ([multiline](https://docs.gitlab.com/ee/ci/yaml/script.html#split-long-commands)) you can use brackets such as `( condition1 && condition2 && condition3 )` and put each condition in new line. Keep in mind that each line has to be indented correctly, which you can verify using the [pipeline editor](https://docs.gitlab.com/ee/ci/pipeline_editor/). See such example [here](https://hastebin.app/62711ec2d3810c001cf24b68). – aljaxus May 03 '22 at 12:22
  • 1
    Updated relevant doc link: https://docs.gitlab.com/ee/ci/jobs/job_control.html#group-variable-expressions-together-with-parentheses – timblaktu Mar 08 '23 at 15:07
-1

There's another operator that the AND operator. You can replace the AND in your the gitlab-ci.yml file with the && operator. That's how we make it !

Haykad684
  • 1
  • 1