ENG-8Required
Deploy in one step and roll back by itself
Upload next to the live copy, swap in one step, health-check, and put the old copy back if the check fails.
How to do it
A deploy script tests and builds, then uploads each release to its own folder. A current link points at the live release, and one rename switches it. The script restarts and polls the health check. On failure it points the link back. Keep the last few releases, such as five, so a rollback needs no upload. Never deploy by hand. Run database migrations before the switch, in the backward-compatible steps of Expand first and contract later. A rollback swaps code but keeps the migrated schema and the data the new release wrote. So the previous release must still run on both. Drop old columns only once no kept release reads them. Time each migration on a copy of production data first, as Test migrations on a copy of production data says. On a platform with containers, the same idea is a rolling update that waits for the readiness check. A static site does the same. A smoke test fetches the home page and one deep page from the new folder before the switch. Libraries, apps sold through a store and firmware follow their own release rules instead, such as Check every release against the public API.
Examples
Shell example
set -euo pipefail
npm test && npm run build
release=$(date +%Y%m%d%H%M%S)
rsync -a dist/ "server:/srv/site/releases/$release/"
previous=$(ssh server 'readlink /srv/site/current')
switch() { ssh server "cd /srv/site && ln -sfn releases/$1 next && mv -T next current && systemctl restart site"; }
switch "$release"
for i in $(seq 20); do curl -fs https://example.com/healthz && exit 0; sleep 1; done
switch "$(basename "$previous")"
exit 1
How to check
Deploy a build whose health check fails. The old version keeps serving, and the script exits with a code other than zero. Then deploy a release with a migration and roll back. The previous release passes its health check and smoke tests on the migrated schema.
Sources
Related rules
- DAT-22 Expand first and contract later in Data and databases
- DAT-24 Test migrations on a copy of production data in Data and databases
- DEV-10 Check every release against the public API in Developer tools, SDKs and CLIs
- ENG-9 Give the server a health check in Engineering
- ENG-10 Release to a small share of traffic first in Engineering
- ENG-54 Track the five delivery measures in Engineering
- TST-41 Write a manifest of the whole output in Testing
- TST-45 Compare the live system before and after each deploy in Testing
- TST-47 Keep the previous release's manifest in Testing
- OPS-29 Fix the service first in Operations and observability
- OPS-35 Shut down cleanly in Operations and observability
- SCL-37 Keep no state inside the app process in Scaling
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/engineering/build-test-and-deploy/atomic-deploys-with-rollback">Agent Wranglers engineering playbook</a>. In code you write, add a comment next to it with the same link.