diff --git a/.drone.yml b/.drone.yml index 391a326..1abc6fb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -13,7 +13,7 @@ steps: port: 22 script_stop: true 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 - command -v flock && command -v curl when: @@ -50,7 +50,7 @@ steps: from_secret: manicandles_deploy_key port: 22 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: branch: @@ -70,7 +70,7 @@ steps: command_timeout: 5m script_stop: true 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: branch: diff --git a/docs/STATIC-DEPLOYMENT.md b/docs/STATIC-DEPLOYMENT.md index dff5625..0c5df34 100644 --- a/docs/STATIC-DEPLOYMENT.md +++ b/docs/STATIC-DEPLOYMENT.md @@ -12,7 +12,7 @@ run the following as `mihir`. ```sh 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 -m 0644 deploy/nginx-locations.conf /etc/nginx/snippets/manicandles.conf 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 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. The `current` symlink changes atomically. HTTPS checks use the real hostname resolved to loopback and verify the certificate, release marker, homepage, diff --git a/scripts/deploy-frontend.sh b/scripts/deploy-frontend.sh index c0d5020..7df1e33 100644 --- a/scripts/deploy-frontend.sh +++ b/scripts/deploy-frontend.sh @@ -13,6 +13,13 @@ root=$(pwd -P) exec 9> .deploy.lock flock -x 9 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 "$(cat "$release/release.txt")" = "$release_id" @@ -31,7 +38,9 @@ if [ -e current ] && [ ! -L current ]; then exit 1 fi old=$(readlink current || true) +old_previous=$(readlink previous || true) temporary="$root/.current-$release_id" +previous_temporary="$root/.previous-$release_id" switched=false restore() { result=$? @@ -43,9 +52,15 @@ restore() { else rm -f current 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 fi - rm -f "$temporary" + rm -f "$temporary" "$previous_temporary" exit "$result" } trap restore EXIT @@ -53,9 +68,13 @@ trap 'exit 1' HUP INT TERM chmod a+rx "$root/releases/$release_id" 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" mv -Tf "$temporary" current -switched=true # Verify this server through the existing HTTPS virtual host and certificate. 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 \ --resolve "$host:443:127.0.0.1" "https://$host$path" 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" diff --git a/test/deployment.test.sh b/test/deployment.test.sh index 7669993..67daf79 100644 --- a/test/deployment.test.sh +++ b/test/deployment.test.sh @@ -20,9 +20,9 @@ esac MOCK chmod +x "$fixture/bin/curl" for release in 1-aaa 2-bbb 3-ccc; do - mkdir -p "$fixture/site/releases/$release/dist" - printf '%s\n' "$release" > "$fixture/site/releases/$release/dist/release.txt" - printf 'Test\n' > "$fixture/site/releases/$release/dist/index.html" + mkdir -p "$fixture/site/incoming/$release/dist" + printf '%s\n' "$release" > "$fixture/site/incoming/$release/dist/release.txt" + printf 'Test\n' > "$fixture/site/incoming/$release/dist/index.html" done deploy() { sh scripts/deploy-frontend.sh "$fixture/site" "$1" test.example; } @@ -34,6 +34,11 @@ deploy 1-aaa deploy 2-bbb test "$(cat "$fixture/site/current/release.txt")" = 2-bbb 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")" = 'Test' touch "$fixture/site/fail-health" if deploy 3-ccc; then echo 'Expected rollback on readiness failure' >&2; exit 1; fi