$_ CLI In Depth
Learning progress 0%
Build · Robust Scripts, Traps & Debugging

Build: A Safe 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.

~30 min Advanced 2 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: 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:

BASH11 lines
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

Step 2: 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.

Files

  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

walkthrough.sh

BASH43 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: Switch to a healthy release
# (the lab prepares this first)
# mkdir -p /workspace/releases/r1 /workspace/releases/r2
# touch /workspace/releases/r1/ok
cat > /workspace/deploy.sh <<&#039;EOF&#039;
#!/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

# Step 2: Restore the previous release on failure
cat > /workspace/deploy.sh <<&#039;EOF&#039;
#!/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
EOF

Lessons this build uses