fix: stage uploads before activating immutable releases
continuous-integration/drone/push Build is passing Details

This commit is contained in:
mihir 2026-11-19 20:52:39 +05:30
parent feefce11c2
commit f8f97bb1f6
4 changed files with 36 additions and 14 deletions

View File

@ -13,7 +13,7 @@ steps:
port: 22 port: 22
script_stop: true script_stop: true
script: script:
- test -d /var/www/manicandles/releases && test -w /var/www/manicandles/releases - test -w /var/www/manicandles/releases && test -w /var/www/manicandles/incoming
- test -r /etc/nginx/snippets/manicandles.conf - test -r /etc/nginx/snippets/manicandles.conf
- command -v flock && command -v curl - command -v flock && command -v curl
when: when:
@ -50,7 +50,7 @@ steps:
from_secret: manicandles_deploy_key from_secret: manicandles_deploy_key
port: 22 port: 22
source: dist,scripts/deploy-frontend.sh source: dist,scripts/deploy-frontend.sh
target: /var/www/manicandles/releases/${DRONE_BUILD_NUMBER}-${DRONE_COMMIT_SHA} target: /var/www/manicandles/incoming/${DRONE_BUILD_NUMBER}-${DRONE_COMMIT_SHA}
when: when:
branch: branch:
@ -70,7 +70,7 @@ steps:
command_timeout: 5m command_timeout: 5m
script_stop: true script_stop: true
script: script:
- sh /var/www/manicandles/releases/${DRONE_BUILD_NUMBER}-${DRONE_COMMIT_SHA}/scripts/deploy-frontend.sh /var/www/manicandles '${DRONE_BUILD_NUMBER}-${DRONE_COMMIT_SHA}' manicandles.midastix.com - sh /var/www/manicandles/incoming/${DRONE_BUILD_NUMBER}-${DRONE_COMMIT_SHA}/scripts/deploy-frontend.sh /var/www/manicandles '${DRONE_BUILD_NUMBER}-${DRONE_COMMIT_SHA}' manicandles.midastix.com
when: when:
branch: branch:

View File

@ -12,7 +12,7 @@ run the following as `mihir`.
```sh ```sh
sudo install -d -o mihir -g "$(id -gn mihir)" -m 0755 \ sudo install -d -o mihir -g "$(id -gn mihir)" -m 0755 \
/var/www/manicandles /var/www/manicandles/releases /var/www/manicandles /var/www/manicandles/releases /var/www/manicandles/incoming
sudo install -d -m 0755 /etc/nginx/snippets sudo install -d -m 0755 /etc/nginx/snippets
sudo install -m 0644 deploy/nginx-locations.conf /etc/nginx/snippets/manicandles.conf sudo install -m 0644 deploy/nginx-locations.conf /etc/nginx/snippets/manicandles.conf
command -v flock command -v flock
@ -53,7 +53,9 @@ After server setup, merge the change into main. Main-branch push builds perform
an SSH access check before the quality gates. Feature branches validate the an SSH access check before the quality gates. Feature branches validate the
configuration, test rollback, and build the frontend without publishing a release. configuration, test rollback, and build the frontend without publishing a release.
Release directories have the form `releases/BUILD-COMMIT/dist`. A file lock Uploads first enter `incoming/BUILD-COMMIT`. A completed build moves to
`releases/BUILD-COMMIT/dist` under the deployment lock. Retried uploads cannot
overwrite a published release. A file lock
serializes activation. An older build cannot replace a newer active build. serializes activation. An older build cannot replace a newer active build.
The `current` symlink changes atomically. HTTPS checks use the real hostname The `current` symlink changes atomically. HTTPS checks use the real hostname
resolved to loopback and verify the certificate, release marker, homepage, resolved to loopback and verify the certificate, release marker, homepage,

View File

@ -13,6 +13,13 @@ root=$(pwd -P)
exec 9> .deploy.lock exec 9> .deploy.lock
flock -x 9 flock -x 9
release="$root/releases/$release_id/dist" release="$root/releases/$release_id/dist"
if [ ! -d "$release" ]; then
candidate="$root/incoming/$release_id/dist"
test -s "$candidate/index.html"
test "$(cat "$candidate/release.txt")" = "$release_id"
mkdir -p "$root/releases/$release_id"
mv "$candidate" "$release"
fi
test -s "$release/index.html" test -s "$release/index.html"
test "$(cat "$release/release.txt")" = "$release_id" test "$(cat "$release/release.txt")" = "$release_id"
@ -31,7 +38,9 @@ if [ -e current ] && [ ! -L current ]; then
exit 1 exit 1
fi fi
old=$(readlink current || true) old=$(readlink current || true)
old_previous=$(readlink previous || true)
temporary="$root/.current-$release_id" temporary="$root/.current-$release_id"
previous_temporary="$root/.previous-$release_id"
switched=false switched=false
restore() { restore() {
result=$? result=$?
@ -43,9 +52,15 @@ restore() {
else else
rm -f current rm -f current
fi fi
if [ -n "$old_previous" ]; then
ln -sfn "$old_previous" "$previous_temporary"
mv -Tf "$previous_temporary" previous
else
rm -f previous
fi
echo 'Release verification failed. Restored the previous release.' >&2 echo 'Release verification failed. Restored the previous release.' >&2
fi fi
rm -f "$temporary" rm -f "$temporary" "$previous_temporary"
exit "$result" exit "$result"
} }
trap restore EXIT trap restore EXIT
@ -53,9 +68,13 @@ trap 'exit 1' HUP INT TERM
chmod a+rx "$root/releases/$release_id" chmod a+rx "$root/releases/$release_id"
chmod -R a+rX "$release" chmod -R a+rX "$release"
switched=true
if [ -n "$old" ] && [ "$old" != "$release" ]; then
ln -sfn "$old" "$previous_temporary"
mv -Tf "$previous_temporary" previous
fi
ln -sfn "$release" "$temporary" ln -sfn "$release" "$temporary"
mv -Tf "$temporary" current mv -Tf "$temporary" current
switched=true
# Verify this server through the existing HTTPS virtual host and certificate. # Verify this server through the existing HTTPS virtual host and certificate.
served=$(curl --fail --silent --show-error --max-time 20 \ served=$(curl --fail --silent --show-error --max-time 20 \
@ -65,8 +84,4 @@ for path in / /workspace /cart /api/v1/health/ready; do
curl --fail --silent --show-error --output /dev/null --max-time 20 \ curl --fail --silent --show-error --output /dev/null --max-time 20 \
--resolve "$host:443:127.0.0.1" "https://$host$path" --resolve "$host:443:127.0.0.1" "https://$host$path"
done done
if [ -n "$old" ] && [ "$old" != "$release" ]; then
ln -sfn "$old" "$root/.previous-$release_id"
mv -Tf "$root/.previous-$release_id" previous
fi
echo "Activated $release_id on $host" echo "Activated $release_id on $host"

View File

@ -20,9 +20,9 @@ esac
MOCK MOCK
chmod +x "$fixture/bin/curl" chmod +x "$fixture/bin/curl"
for release in 1-aaa 2-bbb 3-ccc; do for release in 1-aaa 2-bbb 3-ccc; do
mkdir -p "$fixture/site/releases/$release/dist" mkdir -p "$fixture/site/incoming/$release/dist"
printf '%s\n' "$release" > "$fixture/site/releases/$release/dist/release.txt" printf '%s\n' "$release" > "$fixture/site/incoming/$release/dist/release.txt"
printf '<html>Test</html>\n' > "$fixture/site/releases/$release/dist/index.html" printf '<html>Test</html>\n' > "$fixture/site/incoming/$release/dist/index.html"
done done
deploy() { sh scripts/deploy-frontend.sh "$fixture/site" "$1" test.example; } deploy() { sh scripts/deploy-frontend.sh "$fixture/site" "$1" test.example; }
@ -34,6 +34,11 @@ deploy 1-aaa
deploy 2-bbb deploy 2-bbb
test "$(cat "$fixture/site/current/release.txt")" = 2-bbb test "$(cat "$fixture/site/current/release.txt")" = 2-bbb
test "$(cat "$fixture/site/previous/release.txt")" = 1-aaa test "$(cat "$fixture/site/previous/release.txt")" = 1-aaa
# A retried upload must not change files in an already published release.
mkdir -p "$fixture/site/incoming/2-bbb/dist"
echo 'incomplete retry' > "$fixture/site/incoming/2-bbb/dist/index.html"
deploy 2-bbb
test "$(cat "$fixture/site/current/index.html")" = '<html>Test</html>'
touch "$fixture/site/fail-health" touch "$fixture/site/fail-health"
if deploy 3-ccc; then echo 'Expected rollback on readiness failure' >&2; exit 1; fi if deploy 3-ccc; then echo 'Expected rollback on readiness failure' >&2; exit 1; fi