How can i stop CircleCi from automatically deleting branches that are deleted from github?

239 Views Asked by At

I have a workflow on CircleCi that builds and deploys a branch to AWS when a pull request is open from that branch.

I have a second workflow that tears down the deployed environment when the pull request is merged.

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 delete the branch.

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}}"
   
1

There are 1 best solutions below

1
yaningo On

I think the actual issue here is this:

\"branch\":\"${{env.CIRCLECI_BRANCH}}\"

This syntax to reference a CircleCI environment variable is incorrect.

Try this instead:

\"branch\": \"${CIRCLE_BRANCH}\"

  Also, this statement:

When a branch is deleted from Github, it is also deleted from CircleCI

is not completely true.

Although the related branch might be deleted from the "Filter Branches" list, any build that ran on the now-deleted branch will still be present and visible in CircleCI.

You can access them either by:

  • Constructing a URL for the branch in question
https://app.circleci.com/pipelines/github/your_org_name/your_project_name?branch=your_deleted_branch_name
  • Scrolling through the "Pipelines" page to find a build that ran on that branch, then by clicking the branch name you'll see all builds that previously ran on the branch.