The specification
The script takes one argument, the path of a release directory. It must refuse to run without that argument. It creates the parent site directory if needed, records the current target of the link called current inside that directory, and points current at the new release. A release is healthy when it contains a file named ok. If the file is missing, the script restores the previous target, prints a message that says it rolled back, and exits with status 1. If there was no previous target, it removes the link. On success, it prints a line that names the release and exits with status 0.
Design decisions
The script uses strict mode, because a missing directory or an unset argument must stop it at once. It reads the previous link target before changing anything, so the rollback has a value to restore. It uses ln -sfn, which replaces the link in one command, and it never deletes the release directory, which is the dangerous part of a deploy. The health check happens after the switch, so a broken release is detected in the place where users will see it.
A design alternative is to check the release before switching at all. That is simpler and avoids the rollback. The rollback version here is included because the pattern appears in real systems where the health check can only run after the switch, for example when a service must reload.
The reference solution
Read the solution against the specification, one rule at a time. It is intentionally short, and each line maps to a rule above.
#!/usr/bin/env bash
# Usage: deploy.sh RELEASE_DIR
# Points /workspace/site/current at RELEASE_DIR, checks it, and rolls back on failure.
set -euo pipefail
release=${1:?usage: deploy.sh RELEASE_DIR}
site=/workspace/site
mkdir -p "$site"
prev=$(readlink "$site/current" 2>/dev/null || true)
ln -sfn "$release" "$site/current"
if [[ -f "$release/ok" ]]; then
echo "deployed $release"
exit 0
fi
echo "health check failed for $release" >&2
if [[ -n $prev ]]; then
ln -sfn "$prev" "$site/current"
echo "rolled back to $prev" >&2
else
rm -f "$site/current"
fi
exit 1The line that reads the previous target uses || true, which is the explicit way to say that a missing link is expected on the first deploy. Without it, strict mode would stop the script before any release was made. The status of the final exit 1 is what a calling tool would use to decide that the deploy failed.
Testing the rollback
Test the script with a healthy release, then with a broken one. Create two directories, one with an ok file and one without it. Deploy the healthy one first, then the broken one. After the second run, the link should still point at the healthy release, and the script should have reported the rollback. The lab for this build creates these directories and checks the result. Run the tests in a scratch directory so that a failure does not leave a real site broken.
Extensions to try
Once the basic script works, add a lock with flock so that two deploys cannot run at once, and add a trap that removes a temporary log file. Each addition is a few lines and reuses one unit of this module. Test each change in the same way, with a healthy release and a broken one.