Deploy static sites with an atomic switch and a tested rollback

Deploy static sites with an atomic switch and a tested rollback

Deploy static sites with an atomic switch and a tested rollback

When updating a static site, syncing the build output directly into the live directory can cause requests to receive a mix of new pages and old scripts. If the sync fails midway, it can also leave an incomplete set of files. A deployment workflow on a dedicated server should prepare a complete release first, switch the serving entry point, and then verify the response. If validation fails, it should restore the previous entry point.

The following example uses Debian 12, Nginx and the local filesystem. It covers static files only, not database migrations. VPSAMD’s two 1TB NVMe drives are directly attached, with no hardware RAID. Release directories are not a substitute for off-site backups and do not protect against disk failure.

1. Separate release files from mutable data

Use /srv/www/site as the site’s base directory, with releases for release files, manifests for checksum manifests and current as the serving entry point. Release names must be unique identifiers generated by the build pipeline. This example uses r001; never overwrite an existing release.

Keep uploads, caches and application data outside release directories. Otherwise, restoring the old symlink may make new uploads inaccessible, and old code may misinterpret data stored in a newer format. Static build artifacts must not contain environment secrets, repository metadata or build logs either.

Atomic deployment guarantees that “the entry point is replaced in a single, indivisible operation.” It does not guarantee that “every request involved in a page visit uses the same release,” nor does it provide a data transaction for the entire server.

Create current for the first time before allowing production traffic, and validate the initial release. The scripts below require an existing, working symlink so that a failed first deployment does not leave you without a rollback target.

2. Verify the artifacts before writing the release directory

Before you begin, trusted build output must be available in /srv/build/site. That directory must contain only regular files and directories, with no symlinks or device files. Run the following preparation steps in Bash with administrator privileges. All deployment jobs must share the same lock:

set -euo pipefail
base=/srv/www/site
release=r001
source=/srv/build/site

exec 9>/run/lock/site-deploy.lock
flock -n 9
test -f "$source/index.html"
test -z "$(find "$source" ! -type f ! -type d -print -quit)"

install -d "$base/releases" "$base/manifests"
test ! -e "$base/releases/$release"
manifest="$base/manifests/$release.sha256"
test ! -e "$manifest"

(
  cd "$source"
  find . -type f -print0 |
    sort -z |
    xargs -0 -r sha256sum
) > "$manifest"

install -d "$base/releases/$release"
cp -a "$source/." "$base/releases/$release/"
(
  cd "$base/releases/$release"
  sha256sum -c "$manifest"
)

printf '%s\n' "$release" > "$base/releases/$release/release-id.txt"
chown -R root:root "$base/releases/$release"
find "$base/releases/$release" -type d -exec chmod 0755 {} +
find "$base/releases/$release" -type f -exec chmod 0644 {} +

The checksum manifest is generated before copying so that verification can detect content changes during the copy or transfer errors. The build directory must remain unchanged. The pipeline should also check the expected file list, because checksum verification does not report extra files.

release-id.txt is a probe marker generated by the deployment process and is not included in the original artifact manifest. If any step fails, leave the files in place for inspection rather than retrying with the same release name. Once you have confirmed that nothing is using them, remove the failed directory and its manifest, then use a new identifier.

3. Point the serving entry point at a complete release

The Nginx site’s root points to /srv/www/site/current. The following configuration provides a separate local validation endpoint. Its port is explicitly chosen for this article and is not a service default. Add it as a complete server block inside the existing http configuration.

server {
    listen 127.0.0.1:8080;
    root /srv/www/site/current;
    open_file_cache off;

    location = /release-id.txt {
        add_header Cache-Control "no-store";
        try_files $uri =404;
    }

    location / {
        try_files $uri $uri/ =404;
    }
}

Run nginx -t first. If it succeeds, reload the configuration through the system service and confirm that the old release marker is readable. The public-facing site must also use the same entry point; a local probe is no substitute for validating the public request path.

The script below creates a temporary symlink in the same parent directory, then replaces current using mv -T. Both are on the same filesystem, so the replacement uses rename semantics. Do not delete the old symlink before creating a new one: that would leave a period during which the entry point does not exist.

set -euo pipefail
base=/srv/www/site
release=r001

exec 9>/run/lock/site-deploy.lock
flock -n 9
test -L "$base/current"
previous=$(readlink "$base/current")
test -d "$base/current"
test -f "$base/releases/$release/index.html"
test "$(cat "$base/releases/$release/release-id.txt")" = "$release"
test "$(readlink -f "$base/current")" != "$base/releases/$release"

temporary="$base/.current-$$"
rollback="$base/.rollback-$$"
switched=0

finish() {
  status=$?
  trap - EXIT
  if [ "$switched" -eq 1 ]; then
    ln -s "$previous" "$rollback"
    mv -Tf "$rollback" "$base/current"
  fi
  rm -f "$temporary"
  exit "$status"
}

trap finish EXIT
trap 'exit 130' INT
trap 'exit 143' TERM

ln -s "releases/$release" "$temporary"
switched=1
mv -Tf "$temporary" "$base/current"
actual=$(curl -fsS --max-time 10 \
  http://127.0.0.1:8080/release-id.txt)
test "$actual" = "$release"
switched=0

After a successful run, there is no need to reload Nginx again: the filesystem entry point has changed, not the configuration. If the local request fails or the marker does not match, the exit handler restores the old symlink. A failed restore must be treated as a deployment incident, not marked as a successful job.

4. Check page and asset compatibility across releases

After the switch, a browser may still hold old HTML and then request assets from the new release directory. Keeping the old release directory alone is not enough: old asset URLs that go through current still resolve to the new directory.

Asset naming and retention policy

Use content-hashed filenames for scripts and stylesheets, and do not overwrite files with the same name. The new release directory must include the assets needed by old pages during the compatibility period, or those assets must be served from a separate, immutable asset directory. Do not delete old assets immediately after a successful deployment.

Validation should actually request the home page and the scripts and stylesheets it references, as well as check key pages. A matching release marker proves only that the entry point has switched, not that the build output works. If file caching or upstream caching is enabled, validate each cache’s behavior separately. The local marker cannot replace checks along the end-user request path.

5. Make rollback drills a deployment completion requirement

In a test environment, remove the probe marker from the new release and confirm that the switch script rejects the deployment while leaving the old entry point unchanged. Then make the local validation endpoint unreachable and confirm that a failure after the switch restores the old symlink. After a normal deployment, also check that the old release remains readable, file permissions are correct and requests for old assets succeed.

The exit handler cannot cover a power failure or forced termination. Before starting each deployment job, check where current actually points and look for leftover temporary symlinks. Record the pre-switch target in the deployment job’s persistent records. The deployment lock prevents concurrency only among jobs that follow the same locking protocol; manual operations must use it too.

Run release cleanup separately, using the same lock. Before deleting anything, exclude the current target, the designated rollback release and assets needed during the compatibility period. Do not simply sort directories by name and delete them. The final validation record should include the build identifier, checksum results, targets before and after the switch, and HTTP check results, providing reviewable evidence of the deployment.

Frequently asked questions

Does an atomic switch keep every request on the same release?

No. It switches the directory entry, while HTML and assets are fetched separately. Use content-hashed asset names and retain assets referenced by older pages during the compatibility period.

Does restoring the previous symlink roll back all data?

No. This procedure rolls back the static-file entry point only. Databases, uploads and other mutable state need separate backup and recovery procedures.

EPYC 7543P Dedicated Physical Server

From $442.9/mo

Configure a Dedicated Server VPSAMD monthly totals