Release-directory deploys with an atomic symlink swap
deployrsyncsymlinkrollbackreleaseversioning
A deploy script that builds into a versioned release directory and then flips one symlink gives
you near-zero-downtime releases, instant web rollback, and the property that a failed deploy
leaves production untouched. This is how the TLDR web app and API ship (./deploy.sh in
~/dev/tldr). The hard lessons are about ordering, exclusions, and what "rollback" really covers.
Layout
/var/www/html/tldr/
releases/v1.58.3/ # full web build incl. version.json
releases/v1.58.4/
releases/apk/ # versioned APKs (prune these too!)
app -> releases/v1.58.4 # Apache DocumentRoot points at the symlink
api/ # rsynced in place, not versioned
Pipeline order (what deploy.sh does)
- Guards first. Refuse to run unless the version is bumped in every place that carries it.
For a Flutter app that is three places:
lib/version.dart, the changelog entry, andpubspec.yamlversion: X.Y.Z+build(the+buildbecomes the Android versionCode; a frozen value makes Play reject the AAB as a duplicate). Aflockguard stops two deploys at once. - Build web, APK, AAB.
- Upload the web build to
releases/vX.Y.Z/. - Deploy the API before the swap, so a failed API deploy cannot leave new web plus old API.
- Atomic symlink swap last:
ln -sfn releases/vX.Y.Z app.tmp && mv -T app.tmp app.ln -sfnon its own replaces the link in two steps;mv -Tis a single rename(2). - Git tag, then
git push origin HEAD:main(a changelog commit made by the script otherwise diverges origin). - Prune old releases, keeping N (5). Prune every versioned directory:
releases/apkwas forgotten and grew to 94 files / 5.6 GB on a 25 GB disk before it was noticed (2026-10-02).
Because the swap is the last server-mutating step, every failure before it leaves the live site on
the previous version. Verify by reading the live symlink (readlink /var/www/html/tldr/app) and
https://tldrnerd.com/version.json, then run Writing a production smoke test that tells you why it failed.
Server-only files and rsync --delete
The API is deployed with rsync --delete from the repo (deploy-api.sh). Files that exist only
on the server must be in the exclude list or they are deleted: DB config, JWT secret config, AI
config, S3 config, vendor/, uploads/, data/, and operational flag files such as an
.uploads_disabled kill switch (which was being silently removed on every deploy, re-enabling
uploads mid-incident, until it was excluded).
Never hand-write an rsync to production. On 2026-04-08 a manual
rsync -avz --delete to the API dir deleted vendor/ and every server-only config file and took
the API down. Recovery: composer install, recreate configs, restore uploads from backup. If a
deploy script does not exist for something, write one first.
When recreating a lost config file, copy the format from a working config on the server rather
than from an .example file: the example used MYSQL_HOST/MYSQL_DATABASE while the code reads
SERVER_NAME/DATABASE, which produced "Undefined constant" fatals.
Only run composer install on the server when the composer.lock checksum differs between local
and server.
Rollback is asymmetric
deploy.sh rollback <ver>swaps the web symlink back. It should first check the target dir hasversion.json(a complete build).- The API is not versioned on disk; rolling it back means redeploying the previous git tag
(
git worktree add /tmp/rb <tag>then run that checkout'sdeploy-api.sh). - Never blind-rollback past a one-way migration. Example: v1.31.0 rehashes passwords to bcrypt on login; older API code cannot verify bcrypt, so rolling back the API locks out everyone who logged in since. See Migrating legacy password hashes to bcrypt on login.
- An admin "rollback" endpoint that swaps the symlink must check the role from the JWT. One of these once trusted a username in the request body. See API authorization from the verified identity (and the holes it closes).
Shipping without a changelog entry
If a release must not appear in the in-app history, bump only the version files and answer the
script's "changelog doesn't mention this version, deploy anyway?" prompt with a piped y. A
"silent" mode that inserts a generic "Minor improvements" entry is still an entry.
Lessons from Forge (a self-deploying Flutter app)
- Pruning will eventually delete a release you meant to keep. A prune like
ls -d v*/ | sort -V | head -n -5removes any old directory, including a "genesis" baseline you planned to reset to. Exclude protected releases by name or keep them outsidereleases/. - Version numbers must only go up. "Resetting" to an older release by restoring its old version
number breaks the in-app update checker (it compares semver against
version.json) and Android's versionCode. Treat a reset as a new major version built from the old code. - A web-only deploy mode that skips the slow APK build should carry the previous APK forward into
the new release directory, or the stable
/app.apkURL 404s after the swap. - Gate the swap on verification of the exact tree: Forge's builder refuses to deploy (HTTP 428) unless analyze, tests and a web build succeeded for the current HEAD plus dirty-file fingerprint.
- Validate release directory names: a terminal escape sequence once produced a junk release dir named
v\033[0on the server. deploy.shwritingversion.jsoninto each release gives the app an update checker for free: fetch it on launch, compare semver, show a banner (Android opens the APK URL, web reloads).
Gotchas
- A transient
scp: write remote ... Failureon a 70 MB APK upload was a network blip, not disk; checkdf -h /anddf -i /, then retry. - Long deploys (web + APK + AAB, over 10 minutes) launched from an AI session must be fully detached or they die silently mid-build. See Running long jobs so they survive the AI session that started them.
- "Deploy" means the script. Manual
scpof a build skips versioning, tags and pruning.
See also
- TLDR platform architecture (tldrnerd.com)
- Deploy scripts with rsync (patterns and traps)
- nginx in front of Apache (TLS proxy on :8008)
- SELinux file contexts and booleans on web servers (files that pass through
/tmpneedrestorecon)
Linked from
- A self-building app pipeline (Forge builds Forge)
- Deploy scripts with rsync (patterns and traps)
- In-app updates for sideloaded Android apps
- Memory thrash and capacity on a 1 GB VPS
- Migrating legacy password hashes to bcrypt on login
- Running long jobs so they survive the AI session that started them
- Running several AI coding sessions on one repo (worktrees, locks, fresh memory)
- TLDR platform architecture (tldrnerd.com)
- Writing a production smoke test that tells you why it failed