Release folders and a symlink, so a deploy can roll back
Build each deploy into its own folder, point a current symlink at the one that is live, and keep the previous folder around. A bad release then has a way back.
I do not deploy a site by copying new files over the live directory. I build each release into its own folder, then move a `current` symlink to point at that folder when the release is ready. I also keep a `previous` symlink pointed at the release that was live a moment ago.
The idea is old and I did not invent it. Capistrano has used a releases directory for years. I use a shell script because the server already has bash, Docker, and git, and I want the steps to be readable in one file.
What the layout buys you
A release folder is immutable once it is built. If the new app is broken, the old files are still on disk. Rolling back is pointing `current` back at the previous folder and restarting the process, not trying to remember which files you overwrote.
The other habit that matters is a lock. Two deploys at once will race the symlink and leave you with a tree that belongs to neither SHA. A simple `flock` on a lock file is enough. If the lock is held, the second deploy should exit. It should not queue itself quietly and surprise you later.
I also require a full 40-character commit SHA, not a branch name. A branch can move while the script is running. A SHA cannot. The script I use will not start without that SHA, and it will not deploy a commit that is not on the branch I allow, which in this case is `main`.
What happens on failure
A deploy has a lot of steps: fetch, build, migrate, switch, restart, clean up. If a step fails after the build but before the switch, the live symlink should still point at the old release. Cleanup should run even when the build fails, and a cleanup failure should not be reported as a successful deploy.
I am deliberate about backups in this path. An ordinary code deploy should not invent a new backup as a side effect of shipping a CSS change. Recovery evidence belongs to the backup process, and the deploy can check that a recent verified backup exists before it continues. The deploy's job is to ship a SHA, not to confuse the backup trail.
What I prune
You cannot keep every release folder forever. I keep the current one, the previous one, and enough history to be useful, then delete the rest. Pruning has to happen after the new release is live, and it must never delete the folder the symlink still points at. That sounds obvious until a path is not resolved all the way and a script deletes the live tree.
Why I still like this on a single VPS
Platforms with instant rollbacks hide this layout from you. On a VPS I want the same property where I can see it: two named releases, a lock, a SHA, and a way back that does not depend on rebuilding from a laptop at midnight. The script is longer than `rsync` into `/var/www`, and that is the point.