Pipeline with stop action always pending until stopped

I am learning GitLab CI/CD and trying to set up dynamic environments for new branches.
I want to have a new environment when branch is created, and to destroy it when branch is deleted.

My pipeline deploys the build and I get a new environment, but the stop job is manual(as it should be I assume from the docs) and whole pipeline is in Pending state until I stop the environment and stop job finishes.

I assume this is intended behavior, but then I don’t understand the purpose of environments and why pipeline is pending until environment is destroyed.

deploy_dev:
  stage: deploy
  script:
    - gitlab-terraform apply
  environment:
    name: $CI_PROJECT_NAME-$CI_COMMIT_BRANCH
    on_stop: stop_env_dev
  rules:
    - if: $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_BRANCH != "master"

stop_dev_env:
  stage: deploy
  script:
    - gitlab-terraform destroy -auto-approve
  environment:
    name: $CI_PROJECT_NAME-$CI_COMMIT_BRANCH
    action: stop
  rules:
    - if: $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_BRANCH != "master"
      when: manual
1 Like