$_ CLI In Depth
Learning progress 0%
build robustness traps debugging

Build: A Safe Deploy Script with Rollback

3 min read Module 15: Robust Scripts, Traps & Debugging Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:06

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.

BASH21 lines
#!/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 1

The 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.

Terminal lab · 2 steps · real Linux in your browser

Build: a deploy script with rollback

Write the deploy script in two stages, first switching and checking a release, then restoring the previous release when the check fails.

01
Switch to a healthy release
Create deploy.sh in /workspace. It takes one argument, the release directory. It uses strict mode, creates /workspace/site, points /workspace/site/current at the release with ln -sfn, and checks that the release has an ok file. It prints deployed followed by the release when the file exists, and otherwise prints a health check failure to standard error and exits 1: ``bash cat > deploy.sh <<'EOF' #!/usr/bin/env bash set -euo pipefail release=${1:?usage: deploy.sh RELEASE_DIR} site=/workspace/site mkdir -p "$site" ln -sfn "$release" "$site/current" if [[ -f "$release/ok" ]]; then echo "deployed $release"; exit 0; fi echo "health check failed for $release" >&2 exit 1 EOF ``
Show hint for step 1
The check uses the test form with -f on the ok file inside the release. ln -sfn replaces the link without deleting the release directory.
02
Restore the previous release on failure
Change deploy.sh so that it records the previous target of current before it switches, and restores that target when the health check fails. Print rolled back to followed by the previous target to standard error. If there was no previous target, remove the link instead. Keep the exit status 1 for a failed check. Then deploy r1 successfully and deploy r2, which has no ok file. The link must still point at r1 afterward.
Show hint for step 2
Read the old target with readlink into a variable before ln -sfn runs. A missing link on the first deploy is expected, so add || true to that read.
Check yourself Question 1 of 4
Concept Check What ln -sfn does recall

What does ln -sfn target link do when link already exists as a symbolic link?

Recap & Key Takeaways

The deploy script uses strict mode, records the previous link target before switching, and restores it when the health check fails.
ln -sfn switches a symbolic link to a new release without deleting the directory that holds the old one.
|| true on the read of the previous target is an explicit, documented exception to strict mode.
Test a rollback with a healthy and a broken release in a scratch directory before relying on it.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue