"Only" or "rules" keywords weirdly remove jobs from the CI pipeline
Asked Answered
D

1

7

I am trying to use "rules" and "only" keywords to define my pipeline behaviors between merge requests, pushes into dev branch and pushes into master branch.

I noticed several weird behaviors in the Gitlab CI, let's see in my merge_requests pipelines.

  • With this gitlab-ci.yml file, without any rule, all the jobs are displayed and run.
image: "python:3.8"


stages:
  - test_without_only_policy
  - test_with_only_policy

test_without_only_policy:
  stage: test_without_only_policy
  when: manual
  script:
    - echo "Yay, I am in the pipeline"

test_with_only_policy:
  stage: test_with_only_policy
  script:
  - echo "I am always in the pipeline"

both jobs appear when there are no rules

Everything is working as expected, great πŸ‘

  • With this gitlab-ci.yml file, without an "only" policy in the 2nd job, the 1st job without rules disappears.
image: "python:3.8"

stages:
  - test_without_only_policy
  - test_with_only_policy

test_without_only_policy:
  stage: test_without_only_policy
  when: manual
  script:
    - echo "No, I am not in the pipeline anymore"

test_with_only_policy:
  stage: test_with_only_policy
  only:
  - merge_requests
  script:
  - echo "I am always in the pipeline"

the job without "only" policy disappears

Why did the 1st job without the rules or "only" policy disappear?

  • With this gitlab-ci.yml file, with a "rules" keyword in the 2nd job, the 1st job without rules disappears.
image: "python:3.8"

stages:
  - test_without_only_policy
  - test_with_only_policy

test_without_only_policy:
  stage: test_without_only_policy
  when: manual
  script:
    - echo "No, I am not in the pipeline anymore"

test_with_only_policy:
  stage: test_with_only_policy
  rules:
  - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    when: manual
  script:
  - echo "I am always in the pipeline"

the job without "rules" keyword disappears

Why did the 1st job without the rules or "only" policy disappear?

Thank you for your help, I don't understand why my job disappears when I add rules in other jobs.

Delilahdelimit answered 27/9, 2021 at 17:0 Comment(0)
D
9

According to the documentation for Merge Request Pipelines, if you have a pipeline where only some jobs use only/except/rules with merge_requests, only those jobs will be in the pipeline if it is a Merge Request pipeline. The other jobs will be left out.

Here's the example from the docs:

build:
  stage: build
  script: ./build
  only:
    - main

test:
  stage: test
  script: ./test
  only:
    - merge_requests

deploy:
  stage: deploy
  script: ./deploy
  only:
    - main

In this example, the only job that specifies it should run for Merge Request pipelines is the test job. For standard push pipelines, the build and deploy jobs will run, but when a new merge request is created, a change is pushed to a branch that is the source branch on an existing merge request, or you hit the Run Pipeline button on the Pipelines tab for a Merge Request, it will run a Merge Request pipeline.

Here's another example with a different scenario:

A:
  stage: some_stage
  only:
    - branches
    - tags
    - merge_requests
  script:
    - script.sh

B: 
  stage: some_other_stage
  only:
    - branches
    - tags
    - merge_requests
  script:
    - script.sh

C: 
  stage: third_stage
  only:
    - merge_requests
  script:
    - script.sh

In this example, jobs A and B run for all pipeline types push, tags, merge_requests, etc., but job C only runs for merge_request pipelines.

That's why your test_without_only_policy job will never be in a pipeline where test_with_only_policy runs. test_with_only_policy specifically runs for Merge Request events, but test_without_only_policy does not.

Danas answered 28/9, 2021 at 18:9 Comment(4)
Thank you for your answer. This is an unusual behavior I think but as you said, the documentation specifies: "When you use only: [merge_requests], only jobs with that keyword are run in the context of a merge request. No other jobs run." – Delilahdelimit
The docs for gitlab 15.0 and later no longer have this wording; I wonder if this behavior has changed? – Prophetic
I'm still seeing this behavior in 15.10.6. – Burly
in gitlab v17, keyword only is deprecated and changed to rules keyword. – Finley

© 2022 - 2024 β€” McMap. All rights reserved.