On_stop Job Fails w/ Race Condition of Branch Deletion

I’m running into an issue where I set up a review app along the lines of https://gitlab.tableausoftware.com/help/ci/review_apps/index.md that has an on_stop action to clean up the app (terraform destroy via make) after the feature branch is merged and deleted as part of that merge request. What ends up happening is that the branch gets deleted before the stop_review job can sync the code and so it errors out. Am I missing a cache key somewhere or is there a better way to automatically handle cleaning up these temporary environments?

Excerpt of my gitlab-ci.yml file:

deploy_review:
  stage: deploy_review
  script:
  - make deploy
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://$CI_ENVIRONMENT_SLUG.review.example.com
    on_stop: stop_review
  only:
    - branches
  except:
    - master

stop_review:
  stage: deploy_review
  when: manual
  script:
    - make destroy_all
  environment:
    name: review/$CI_COMMIT_REF_NAME
    action: stop
  except:
    - master

Error output:

Cloning repository…
Cloning into ‘/builds/project/repo’…
Checking out 123243f887 as branch_name…
fatal: reference is not a tree: 128ef887495e83486135c167eed5f20d3132454ae

1 Like

I’ve also been running into this issue a lot with my ‘stop_review’ jobs. Have you, by chance, come across a solution or workaround since posting this?