Skip to content

Deleting branch while job is running doesn't delete environment

Summary

I have a branch that triggered a job as well as created an environment for it. If I delete the branch while the job is still running, it leaves behind the environment and it also does not trigger the stop job for that environment. Is this expected behavior or is this a bug?

Would there be a way for me to be able to delete the environments / run cleanup jobs when the branches get deleted?

Steps to reproduce

  1. Create a branch that triggers a pipeline and also creates an environment. See sample below:
deploy-vm:
  stage: deploy
  image: ...
  environment:
    name: vr${CI_PROJECT_ID}${CI_COMMIT_REF_SLUG}
    on_stop: stop_vms
  script:
    - mycommands
  tags:
    - aws
    - generic

deploy-software:
  stage: load
  image: ...
  script:
    - mycommands
  environment:
    name: vr${CI_PROJECT_ID}${CI_COMMIT_REF_SLUG}
    on_stop: stop_vms
  tags:
    - aws
    - generic
  rules:
    - allow_failure: true

stop_vms:
  stage: deploy
  variables:
    GIT_STRATEGY: none
  image: ...
  script: 
    - mycommands
  environment:
    name: vr${CI_PROJECT_ID}${CI_COMMIT_REF_SLUG}
    action: stop
  when: manual
  tags:
    - aws
    - generic
  1. While the job is running, delete the branch
  2. If you go to Environments, you will see that the environment for that branch still exists
  3. The job will still be running, and even after it's done, the stop_vms job will never be called even when the branch is already deleted.

What is the current bug behavior?

After the deletion of a branch, the environment is still present if there was an ongoing job running. The cleanup job will also never run unless triggered manually.

What is the expected correct behavior?

I would expect the environments to get deleted, and the stop/cleanup job triggered as well (either after the current job finishes or after)

Relevant logs and/or screenshots