Interview Prep/Shell Scripting/Scenarios: deployments, user management and fleet operations

Implement a blue/green switch on a single nginx host with a script.

AdvancedScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → Two app instances (blue on 8081, green on 8082); nginx points at one through an include file.

What I'd do →

active=$(grep -oE '808[12]' /etc/nginx/conf.d/upstream-active.conf)
target=$([[ $active == 8081 ]] && echo 8082 || echo 8081)
deploy_to_port "$target"
wait_for_http "http://127.0.0.1:$target/readyz" 60 || die "new color unhealthy; live traffic untouched"

tmp=$(mktemp /etc/nginx/conf.d/.upstream.XXXXXX)
printf 'upstream app { server 127.0.0.1:%s; }\n' "$target" > "$tmp"
chmod 0644 "$tmp"
mv -f -- "$tmp" /etc/nginx/conf.d/upstream-active.conf
nginx -t && systemctl reload nginx || { echo "nginx config invalid" >&2; exit 1; }
echo "switched $active -> $target; rollback = rerun with previous color"
  1. The idle color is deployed and tested before any traffic moves.
  2. nginx reload swaps workers gracefully; existing connections finish on old workers.

Why → Rollback is just pointing back at the old color, which is still running.

Interview tip → Mention database migrations: they must be backward compatible so both colors can run against the same schema.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations