📚 Encyclopedia

Servers & Hosting · updated 2026-10-13

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)

  1. 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, and pubspec.yaml version: X.Y.Z+build (the +build becomes the Android versionCode; a frozen value makes Play reject the AAB as a duplicate). A flock guard stops two deploys at once.
  2. Build web, APK, AAB.
  3. Upload the web build to releases/vX.Y.Z/.
  4. Deploy the API before the swap, so a failed API deploy cannot leave new web plus old API.
  5. Atomic symlink swap last: ln -sfn releases/vX.Y.Z app.tmp && mv -T app.tmp app. ln -sfn on its own replaces the link in two steps; mv -T is a single rename(2).
  6. Git tag, then git push origin HEAD:main (a changelog commit made by the script otherwise diverges origin).
  7. Prune old releases, keeping N (5). Prune every versioned directory: releases/apk was 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

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)

Gotchas

See also

Linked from

Sources: forge memory (self-building-direction, MEMORY.md, completed-work), tldr memory (release-v1.31.0-audit, feedback_never_manual_rsync, feedback_deploy_command, completed-work, server-web-performance), posts #1445, #1702