ci: deploy static frontend over SSH with rollback
continuous-integration/drone/push Build is passing Details

This commit is contained in:
mihir 2026-11-19 20:50:01 +05:30
parent aa9c9a59b3
commit feefce11c2
8 changed files with 305 additions and 15 deletions

View File

@ -1,8 +1,33 @@
kind: pipeline kind: pipeline
type: docker type: docker
name: Build Image name: Validate and deploy frontend
steps: 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 - name: Quality gates
image: node:24-alpine image: node:24-alpine
environment: environment:
@ -12,16 +37,20 @@ steps:
- npm ci - npm ci
- npm audit --audit-level=high - npm audit --audit-level=high
- npm run check - 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 - name: Upload frontend release
image: plugins/docker image: appleboy/drone-scp
settings: settings:
username: host: server1.midastix.com
from_secret: docker_username username: mihir
password: key:
from_secret: docker_password from_secret: manicandles_deploy_key
repo: git.midastix.com/ci-docker-repo-user/manicandles port: 22
registry: git.midastix.com source: dist,scripts/deploy-frontend.sh
target: /var/www/manicandles/releases/${DRONE_BUILD_NUMBER}-${DRONE_COMMIT_SHA}
when: when:
branch: branch:
@ -30,7 +59,7 @@ steps:
- push - push
- name: Deploy image - name: Activate and verify frontend
image: appleboy/drone-ssh image: appleboy/drone-ssh
settings: settings:
host: server1.midastix.com host: server1.midastix.com
@ -41,9 +70,7 @@ steps:
command_timeout: 5m command_timeout: 5m
script_stop: true script_stop: true
script: script:
- whoami - 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
- hostname
- echo "ManiCandles SSH authentication successful"
when: when:
branch: branch:

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
*.sh text eol=lf

View File

@ -25,7 +25,7 @@ docker build -t manicandles-frontend:uat .
docker run --rm -p 8080:80 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. 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.

View File

@ -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;
}

View File

@ -34,7 +34,7 @@ Production release is blocked until the environment checks below pass. Local imp
## Rollback ## 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 ## Operational limits

96
docs/STATIC-DEPLOYMENT.md Normal file
View File

@ -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.

View File

@ -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"

49
test/deployment.test.sh Normal file
View File

@ -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 '<html>Test</html>\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'