From feefce11c262de28565b7d31e49b3a4bee983125 Mon Sep 17 00:00:00 2001 From: mihir Date: Thu, 19 Nov 2026 20:50:01 +0530 Subject: [PATCH 1/2] ci: deploy static frontend over SSH with rollback --- .drone.yml | 53 +++++++++++++++----- .gitattributes | 1 + README.md | 2 +- deploy/nginx-locations.conf | 45 +++++++++++++++++ docs/RELEASE-CHECKLIST.md | 2 +- docs/STATIC-DEPLOYMENT.md | 96 +++++++++++++++++++++++++++++++++++++ scripts/deploy-frontend.sh | 72 ++++++++++++++++++++++++++++ test/deployment.test.sh | 49 +++++++++++++++++++ 8 files changed, 305 insertions(+), 15 deletions(-) create mode 100644 .gitattributes create mode 100644 deploy/nginx-locations.conf create mode 100644 docs/STATIC-DEPLOYMENT.md create mode 100644 scripts/deploy-frontend.sh create mode 100644 test/deployment.test.sh diff --git a/.drone.yml b/.drone.yml index 00c7198..391a326 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,8 +1,33 @@ kind: pipeline type: docker -name: Build Image +name: Validate and deploy frontend steps: + - name: Check deployment access + image: appleboy/drone-ssh + settings: + host: server1.midastix.com + username: mihir + key: + from_secret: manicandles_deploy_key + port: 22 + script_stop: true + script: + - test -d /var/www/manicandles/releases && test -w /var/www/manicandles/releases + - test -r /etc/nginx/snippets/manicandles.conf + - command -v flock && command -v curl + when: + branch: + - main + event: + - push + + - name: Validate Nginx configuration + image: nginx:alpine + commands: + - printf 'events {}\nhttp { server { listen 8080; include /drone/src/deploy/nginx-locations.conf; } }\n' > /tmp/manicandles-nginx.conf + - nginx -t -c /tmp/manicandles-nginx.conf + - name: Quality gates image: node:24-alpine environment: @@ -12,16 +37,20 @@ steps: - npm ci - npm audit --audit-level=high - npm run check + - apk add --no-cache util-linux + - sh test/deployment.test.sh + - printf '%s\n' '${DRONE_BUILD_NUMBER}-${DRONE_COMMIT_SHA}' > dist/release.txt - - name: Build Docker Image - image: plugins/docker + - name: Upload frontend release + image: appleboy/drone-scp settings: - username: - from_secret: docker_username - password: - from_secret: docker_password - repo: git.midastix.com/ci-docker-repo-user/manicandles - registry: git.midastix.com + host: server1.midastix.com + username: mihir + key: + from_secret: manicandles_deploy_key + port: 22 + source: dist,scripts/deploy-frontend.sh + target: /var/www/manicandles/releases/${DRONE_BUILD_NUMBER}-${DRONE_COMMIT_SHA} when: branch: @@ -30,7 +59,7 @@ steps: - push - - name: Deploy image + - name: Activate and verify frontend image: appleboy/drone-ssh settings: host: server1.midastix.com @@ -41,9 +70,7 @@ steps: command_timeout: 5m script_stop: true script: - - whoami - - hostname - - echo "ManiCandles SSH authentication successful" + - 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 when: branch: diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfdb8b7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.sh text eol=lf diff --git a/README.md b/README.md index 16d5d87..0cafa2f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ docker build -t manicandles-frontend:uat . docker run --rm -p 8080:80 manicandles-frontend:uat ``` -The existing main-branch Drone pipeline deploys automatically on push. Work is on a feature branch; a local build is not deployment verification. Docker was unavailable locally for container verification. +After the one-time [server setup](docs/STATIC-DEPLOYMENT.md), main-branch Drone pushes upload the checked static build over SSH and activate it through the existing Nginx HTTPS site. Readiness failure restores the previous release. Drone requires the `manicandles_deploy_key` secret. Feature branches run validation without uploading or activating a release. Storefront list returns only id/name/slug; details return prices/attributes/groups. The API has no product-media fields, so catalog cards use a generic branded placeholder rather than assigning local photos to unknown records. Existing marketing imagery remains intact. diff --git a/deploy/nginx-locations.conf b/deploy/nginx-locations.conf new file mode 100644 index 0000000..fd5d65b --- /dev/null +++ b/deploy/nginx-locations.conf @@ -0,0 +1,45 @@ +# Include inside the existing HTTPS server block for manicandles.midastix.com. +# This supplies the root and locations. Keep the existing TLS directives. +root /var/www/manicandles/current; +index index.html; +add_header X-Content-Type-Options nosniff always; +add_header Referrer-Policy strict-origin-when-cross-origin always; +add_header X-Frame-Options DENY always; +add_header Cache-Control "no-cache" always; +add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https://www.transparenttextures.com; connect-src 'self'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'" always; + +location /api/ { + proxy_pass https://manicandles-backend.midastix.com; + proxy_ssl_server_name on; + proxy_ssl_verify on; + proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; + proxy_set_header Host manicandles-backend.midastix.com; + proxy_set_header Authorization $http_authorization; + proxy_connect_timeout 10s; + proxy_read_timeout 20s; + proxy_hide_header Access-Control-Allow-Origin; + proxy_hide_header Access-Control-Allow-Credentials; + + location ~ "^/api/v1/(?:supplier-documents|b2b/artworks)/[0-9a-fA-F-]{36}/versions$" { + client_max_body_size 3m; + proxy_pass https://manicandles-backend.midastix.com; + } +} + +location = /release.txt { + try_files $uri =404; +} + +location /assets/ { + try_files $uri @previous_assets; +} + +# Existing browser tabs may still request the preceding release's chunks. +location @previous_assets { + root /var/www/manicandles/previous; + try_files $uri =404; +} + +location / { + try_files $uri $uri/ /index.html; +} diff --git a/docs/RELEASE-CHECKLIST.md b/docs/RELEASE-CHECKLIST.md index 9238d7c..591f54a 100644 --- a/docs/RELEASE-CHECKLIST.md +++ b/docs/RELEASE-CHECKLIST.md @@ -34,7 +34,7 @@ Production release is blocked until the environment checks below pass. Local imp ## Rollback -Record the immutable deployed image digest and previous known-good digest before rollout. On the deployment host, use the existing deployment mechanism to restore the previous frontend image, then verify health, SPA deep links and /api connectivity. No backend schema changes are included; retain orders, stock ledgers and accounts. Do not reset or delete database records. Confirm the actual host procedure and measured rollback time in UAT before production. A feature-branch push does not deploy the main-branch pipeline. +Record the active and previous build/commit release IDs before rollout. The SSH deployment restores the previous release automatically when its HTTPS checks fail. Follow the [static deployment runbook](STATIC-DEPLOYMENT.md) for manual rollback, then verify health, SPA deep links and /api connectivity. Retain orders, stock ledgers and accounts. Confirm the actual host procedure and measured rollback time in UAT before production. A feature-branch push does not deploy the main-branch pipeline. ## Operational limits diff --git a/docs/STATIC-DEPLOYMENT.md b/docs/STATIC-DEPLOYMENT.md new file mode 100644 index 0000000..dff5625 --- /dev/null +++ b/docs/STATIC-DEPLOYMENT.md @@ -0,0 +1,96 @@ +# SSH deployment to server1 + +Drone validates the application and Nginx snippet, uploads `dist` and the release +script, and activates the release through SSH. The sole deployment secret is +`manicandles_deploy_key`. SSH uses `mihir@server1.midastix.com:22`. + +## One-time server setup + +Obtain this branch's files on server1 using your existing repository access, +or transfer the Nginx snippet from your workstation. From the repository directory, +run the following as `mihir`. + +```sh +sudo install -d -o mihir -g "$(id -gn mihir)" -m 0755 \ + /var/www/manicandles /var/www/manicandles/releases +sudo install -d -m 0755 /etc/nginx/snippets +sudo install -m 0644 deploy/nginx-locations.conf /etc/nginx/snippets/manicandles.conf +command -v flock +command -v curl +``` + +Identify the existing Nginx site file for `manicandles.midastix.com`. + +```sh +sudo nginx -T 2>&1 | grep -E '^# configuration file|server_name.*manicandles' +``` + +Back up that site file before editing it. Inside its existing HTTPS `server` +block for `manicandles.midastix.com`, replace the old `root`, `index`, and +`location` directives with this include. Preserve its `listen`, `server_name`, +certificate paths, TLS settings, and HTTP-to-HTTPS redirect. Resolve any duplicate +security header definitions against the snippet. + +```nginx +include /etc/nginx/snippets/manicandles.conf; +``` + +Validate and reload. If validation fails, restore the saved site file before +continuing. Reload preserves existing connections. The frontend will begin +serving when the first release supplies `/var/www/manicandles/current`. + +```sh +sudo nginx -t && sudo systemctl reload nginx +``` + +Save the complete matching private key in Drone's `manicandles_deploy_key` +secret. Its public key must be authorized for `mihir` on server1. Keep the +private key outside the repository and logs. + +## Release and acceptance + +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 +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, +`/workspace`, `/cart`, and `/api/v1/health/ready`. Failure restores the previous +current symlink, or removes the first activation if no release existed before it. +The last successful release is also retained at `previous`. Its assets can serve +requests from browser tabs opened before the update. Failed first deployments +leave the site awaiting a successful release. + +After the main build succeeds, test externally from a trusted terminal. + +```sh +curl --fail --show-error https://manicandles.midastix.com/release.txt +curl --fail --show-error https://manicandles.midastix.com/api/v1/health/ready +``` + +Then exercise authenticated UAT journeys. The frontend release does not provision +an organization or user accounts. + +## Manual rollback + +As `mihir` on server1, restore the preceding successful release under the same +lock used by CI. Verify the marker and API after switching. Coordinate this with +active pipelines because a later successful deployment can activate a new release. + +```sh +( + set -eu + cd /var/www/manicandles + flock -x 9 + test -s previous/index.html + ln -s "$(readlink previous)" .rollback-current + mv -Tf .rollback-current current +) 9>/var/www/manicandles/.deploy.lock +curl --fail --show-error https://manicandles.midastix.com/release.txt +curl --fail --show-error https://manicandles.midastix.com/api/v1/health/ready +``` + +Retain release directories until rollback has been verified. Disk retention is +manual in this initial deployment flow. diff --git a/scripts/deploy-frontend.sh b/scripts/deploy-frontend.sh new file mode 100644 index 0000000..c0d5020 --- /dev/null +++ b/scripts/deploy-frontend.sh @@ -0,0 +1,72 @@ +#!/bin/sh +set -eu + +root=${1:?Deployment directory required} +release_id=${2:?Build number and commit required} +host=${3:?Frontend hostname required} +case "$release_id" in *[!0-9a-f-]*|'') echo 'Invalid release ID' >&2; exit 1 ;; esac +build=${release_id%%-*} +case "$build" in *[!0-9]*|'') echo 'Invalid build number' >&2; exit 1 ;; esac +case "$host" in *[!a-zA-Z0-9.-]*|'') echo 'Invalid hostname' >&2; exit 1 ;; esac +cd "$root" +root=$(pwd -P) +exec 9> .deploy.lock +flock -x 9 +release="$root/releases/$release_id/dist" +test -s "$release/index.html" +test "$(cat "$release/release.txt")" = "$release_id" + +# A slow older pipeline must not replace a newer successful release. +last_build=0 +if [ -L current ]; then + last_release=$(cat current/release.txt) + last_build=${last_release%%-*} +fi +if [ "$build" -lt "$last_build" ]; then + echo "Skipping superseded build $build (active build $last_build)" + exit 0 +fi +if [ -e current ] && [ ! -L current ]; then + echo 'The current release must be a symlink' >&2 + exit 1 +fi +old=$(readlink current || true) +temporary="$root/.current-$release_id" +switched=false +restore() { + result=$? + trap - EXIT HUP INT TERM + if [ "$switched" = true ] && [ "$result" -ne 0 ]; then + if [ -n "$old" ]; then + ln -sfn "$old" "$temporary" + mv -Tf "$temporary" current + else + rm -f current + fi + echo 'Release verification failed. Restored the previous release.' >&2 + fi + rm -f "$temporary" + exit "$result" +} +trap restore EXIT +trap 'exit 1' HUP INT TERM + +chmod a+rx "$root/releases/$release_id" +chmod -R a+rX "$release" +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 \ + --resolve "$host:443:127.0.0.1" "https://$host/release.txt") +test "$served" = "$release_id" +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 new file mode 100644 index 0000000..7669993 --- /dev/null +++ b/test/deployment.test.sh @@ -0,0 +1,49 @@ +#!/bin/sh +set -eu +fixture=$(mktemp -d) +trap 'rm -rf "$fixture"' EXIT +mkdir -p "$fixture/bin" "$fixture/site/releases" +export DEPLOY_TEST_ROOT="$fixture/site" +export PATH="$fixture/bin:$PATH" +cat > "$fixture/bin/curl" <<'MOCK' +#!/bin/sh +set -eu +case "$*" in + *release.txt*) + if [ -f "$DEPLOY_TEST_ROOT/wrong-marker" ]; then + echo wrong-release + else + cat "$DEPLOY_TEST_ROOT/current/release.txt" + fi ;; + *health/ready*) test ! -f "$DEPLOY_TEST_ROOT/fail-health" ;; +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" +done +deploy() { sh scripts/deploy-frontend.sh "$fixture/site" "$1" test.example; } + +touch "$fixture/site/fail-health" +if deploy 1-aaa; then echo 'Expected first release failure' >&2; exit 1; fi +test ! -L "$fixture/site/current" +rm "$fixture/site/fail-health" +deploy 1-aaa +deploy 2-bbb +test "$(cat "$fixture/site/current/release.txt")" = 2-bbb +test "$(cat "$fixture/site/previous/release.txt")" = 1-aaa + +touch "$fixture/site/fail-health" +if deploy 3-ccc; then echo 'Expected rollback on readiness failure' >&2; exit 1; fi +test "$(cat "$fixture/site/current/release.txt")" = 2-bbb +test "$(cat "$fixture/site/previous/release.txt")" = 1-aaa +rm "$fixture/site/fail-health" +touch "$fixture/site/wrong-marker" +if deploy 3-ccc; then echo 'Expected rollback on wrong virtual host' >&2; exit 1; fi +test "$(cat "$fixture/site/current/release.txt")" = 2-bbb +rm "$fixture/site/wrong-marker" +deploy 1-aaa +test "$(cat "$fixture/site/current/release.txt")" = 2-bbb +printf 'Deployment activation, rollback and stale-build checks passed.\n' -- 2.40.1 From f8f97bb1f6b810c23b6cab07aadb0800e028e020 Mon Sep 17 00:00:00 2001 From: mihir Date: Thu, 19 Nov 2026 20:52:39 +0530 Subject: [PATCH 2/2] fix: stage uploads before activating immutable releases --- .drone.yml | 6 +++--- docs/STATIC-DEPLOYMENT.md | 6 ++++-- scripts/deploy-frontend.sh | 27 +++++++++++++++++++++------ test/deployment.test.sh | 11 ++++++++--- 4 files changed, 36 insertions(+), 14 deletions(-) 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 -- 2.40.1