I have a workflow on CircleCi that builds and deploys a branch to AWS when a pull request is open from that branch. The pipeline is triggered by a curl request from a Github action.
I have a second workflow that tears down the deployed environment when the pull request is merged. Also by making a curl request from Github action.
NOW: When a branch is deleted from Github, it is also deleted from CircleCi. And most of the times, when the developers in my team merge their pull request, they immediately delete the branch before the request is made to CircleCi to teardown the environment.
When this happens, CircleCi tries to teardown the previously deployed environment. The problem now is that since the branch was deleted from Github and thus automatically deleted from CircleCi, the request to start the teardown workflow fails with "branch not found"
error.
The question now is, how can I make circleCi retain the branch that was deleted on Github so that this request will run? I can make the teardown job then delete the branch from CircleCi after the teardown is done.
The request to CircleCi to start the teardown workflow looks like this…
curl -X POST 'https://circleci.com/api/v2/project/github/<organization>/<project_name>/pipeline' \
-H 'Circle-Token: ${{env.CIRCLECI_TOKEN}}' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d "{\"branch\":\"${{env.CIRCLECI_BRANCH}}\",\"parameters\":{\"pull_request_closed\":true}}"