Environment on_stop jobs can't be conditional

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

  • Close this issue

Summary

If the on_stop task for an environment is made conditional using the rules: clauses, even if all those rules are when: manual or when: never it is treated differently to one that only has a when: manual clause.

Steps to reproduce

Failing sample .gitlab-ci.yml file:

stages:
  - deploy

deploy_docs:
  stage: deploy
  image: python:latest
  rules:
    - if: '$CI_COMMIT_REF_PROTECTED != "true" && $CI_COMMIT_BRANCH'
      changes:
        - docs/*
    - when: never
  script:
    - pip install awscli
    - aws s3 cp --acl public-read --recursive _site/ s3://${S3_DOCS_BUCKET}/${CI_COMMIT_REF_SLUG}/
  environment:
    name: docs/${CI_COMMIT_REF_SLUG}
    url: "http://$DOCS_S3_URL_PREFIX/$CI_COMMIT_REF_SLUG/"
    on_stop: undeploy_docs

undeploy_docs:
  stage: deploy
  image: python:latest
  rules:
    - if: '$CI_COMMIT_REF_PROTECTED != "true" && $CI_COMMIT_BRANCH'
      changes:
        - docs/*
      when: manual
    - when: never
  variables:
    GIT_STRATEGY: none
  script:
    - pip install awscli
    - aws s3 rm --recursive s3://${S3_DOCS_BUCKET}/${CI_COMMIT_REF_SLUG}
  environment:
    name: docs/${CI_COMMIT_REF_SLUG}
    action: stop

Workflow:

  1. User makes changes in the docs/ directory
  2. User pushes the changes and makes a merge request
  3. Pipeline completes, deploying the docs to a test site
  4. The merge request is approved and merged

Example Project

Any project using the CI pipeline above should exhibit this behaviour. This was found in a private project on gitlab.com.

What is the current bug behavior?

The pipeline is blocked, waiting for the undeploy_docs job to be manually started, preventing merging if completed pipelines are required for merge.

What is the expected correct behavior?

The expected behaviour is that the undeploy_docs job would be treated as part of the environment process and therefore not required for the pipeline to be complete.

Relevant logs and/or screenshots

These are from a project with a much larger pipeline including some other relevant steps. The sample pipeline above is a reduced version of that project's pipeline.

Broken pipeline:

image

Expected pipeline:

image

Output of checks

This bug happens on GitLab.com

User Impact

If users configure their pipeline by creating a on_stop task for an environment and it's conditional using the rules: clauses, even if all those rules are when: manual or when: never, it is treated differently to one that only has a when: manual clause. This will cause unintended downstream consequences when the pipeline is running with other jobs.

Edited Jun 17, 2025 by 🤖 GitLab Bot 🤖
Assignee Loading
Time tracking Loading