How to automatically revert a failed deploy (and when not to)

A deploy fails after a merge. Every minute production runs the wrong version, or no version, costs you. Reverting quickly is usually the right first move; fixing the real cause can wait until morning.

What "revert" means

In git, a revert adds a new commit that undoes an earlier one. It does not rewrite history, so it is safe on a shared branch. On GitHub you open a pull request that reverts the merge commit, merge it, and your normal deploy runs again with the last good code.

The manual steps

Two ways to roll back

Many platforms can roll back to the previous release without touching git. That is fast, but main still contains the bad change, so the next deploy will fail again. A git revert keeps your repository and production in step, which is why teams that deploy from main often prefer it.

Automating it

You can script the steps above with GitHub Actions: trigger on a failed deploy workflow, create the revert branch and pull request, and merge it. The hard parts are the edge cases: a failed deploy that is only a flaky test, a revert that itself fails to deploy, two merges close together, and making sure nobody gets surprised.

When automatic revert is a bad idea

A good tool warns you before you schedule such a change, so you can choose to watch it yourself.

How Merge Scheduler does it

After it merges a pull request, Merge Scheduler follows the deploy runs that the merge commit triggered in GitHub Actions. If one fails, it opens a revert pull request, merges it, and watches the redeploy. If the redeploy also fails, it stops and marks the release as needing attention instead of looping. Either way you get one email naming the failed workflow, job and step, with the error lines from the log.

Related: How to schedule a GitHub pull request merge (4 ways) · Merge Scheduler vs merge-schedule-action and Mergify schedules