Job not executed when branch is deleted

Viewed 51

I am following the GitLab flow using Environment and deployments. I have a job, deploy_review that deploys a review app. After the branch is merged or deleted, it should trigger the stop_review job, as per the documentation.

This is my CI configuration:

stages:
- build
- deploy
- test
...

deploy_review:
  stage: deploy
  script:
  ...
  environment:
     name: review/$CI_COMMIT_REF_SLUG
     url: https://$CI_ENVIRONMENT_SLUG.example.com
     on_stop: stop_review
  when: manual

stop_review:
   stage: deploy
   script:
   ...
   environment:
     name: review/$CI_COMMIT_REF_SLUG
     action: stop
   when: manual

test_sauce:
  stage: test
  trigger:
     include: .gitlab-ci-testing.yml
  variables:
     EMULATE_BRANCH: $EMULATE_BRANCH

I am wondering if there's a problem if the job 'test' is preventing the stop_review: from being triggered if the branch is deleted.

Edit: I add variable GIT_STRATEGY, but still experiencing the same issue

stop_review:
   stage: deploy
   variables:
     GIT_STRATEGY: none
   script:
   ...
   environment:
     name: review/$CI_COMMIT_REF_SLUG
     action: stop
   when: manual
1 Answers

Per the documentation, for this to work, you must add the GIT_STRATEGY: none variable to the stop_review: job. Otherwise, the job will fail when trying to checkout the repository (because the branch no longer exists!)

stop_review:
   stage: deploy
   variables:
     GIT_STRATEGY: none
   # ...

That also means this won't work if your stop_review: job depends on files in the repository. In which case, you may opt to provide them via artifacts: or clone any necessary resources from another repo.

This also only applies when your pipeline is NOT associated with a merge request. If there is an associated MR, you need to close or merge the MR.

There are multiple ways to clean up dynamic environments:

  • If you use merge request pipelines, GitLab stops an environment when a merge request is merged or closed.
  • If you do NOT use merge request pipelines, GitLab stops an environment when the associated feature branch is deleted.
  • If you set an expiry period to an environment, GitLab stops an environment when it’s expired.
Related