Refactor deployment workflow to utilize GitHub Actions for improved efficiency. Updated deploy.yml to include actions for checkout and Node.js setup, streamlined build process for Next.js with prebuilt assets, and revised deployment script to eliminate unnecessary steps. Enhanced documentation for clarity on automated deployment process.

This commit is contained in:
smhk1376 committed 2026-10-20 01:17:13 +03:30
1 parent bd2cfa5a26
commit e1b5286946
5 files changed
+88 -37

No files matched your search

+14 -24
View File
@@ -13,30 +13,20 @@ jobs:
deploy: deploy:
runs-on: local-host runs-on: local-host
steps: steps:
- name: Checkout and deploy on the VPS - uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "22"
cache: npm
- name: Build Next.js standalone (host; npm cache reused between runs)
env: env:
REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} NEXT_PUBLIC_SITE_URL: https://edu.mstaut.ir
REPO_ACTOR: ${{ github.actor }} run: bash scripts/ci-build-prebuilt.sh
REPO_NAME: ${{ github.repository }}
DEPLOY_SHA: ${{ github.sha }} - name: Roll out (runtime image, backup, migrate, smoke, swap)
run: | run: |
set -euo pipefail set -euo pipefail
TARGET=/opt/prod/Aut.ManagementFaculty rm -rf prebuilt/static
mkdir -p "$TARGET" PREBUILT_DIR="$GITHUB_WORKSPACE/prebuilt" bash scripts/deploy-production.sh
if [ ! -d "$TARGET/.git" ]; then
git -C "$TARGET" init
git -C "$TARGET" remote add origin "http://127.0.0.1:8301/$REPO_NAME.git"
fi
# Clear stale locks from interrupted shallow fetches (common after crash/timeout).
# This checkout is deploy-only; concurrency group already serializes jobs.
rm -f \
"$TARGET/.git/shallow.lock" \
"$TARGET/.git/index.lock" \
"$TARGET/.git/HEAD.lock" \
"$TARGET/.git/config.lock"
# Supply the job token only for fetch; never store it in .git/config.
AUTH=$(printf '%s:%s' "$REPO_ACTOR" "$REPO_TOKEN" | base64 -w0)
git -C "$TARGET" -c "http.extraHeader=Authorization: Basic $AUTH" fetch --depth=1 origin "$DEPLOY_SHA"
unset AUTH REPO_TOKEN
git -C "$TARGET" checkout --detach --force FETCH_HEAD
bash "$TARGET/scripts/deploy-production.sh"
+9 -12
View File
@@ -1,10 +1,16 @@
# Production deployment # Production deployment
The production site is https://edu.mstaut.ir. Gitea runs on the same VPS. The production site is https://edu.mstaut.ir. Gitea runs on the same VPS.
Pushes to `master` run `.gitea/workflows/deploy.yml` on the existing
`local-host` runner. The checkout lives at `/opt/prod/Aut.ManagementFaculty`.
`scripts/deploy-production.sh` builds a Next.js standalone Docker image, **Normal releases are fully automatic:** a push to `master` (or
*workflow_dispatch* in Gitea) runs `.gitea/workflows/deploy.yml` on the
`local-host` runner — checkout, `npm ci` + `next build` on the runner host
(with npm cache), then `scripts/deploy-production.sh` with a prebuilt tree
(`Dockerfile.prebuilt`: no second `npm ci` / compile inside Docker). Backup,
migrations, smoke checks, and container swap need no manual steps.
`scripts/deploy-production.sh` builds the Docker image from `PREBUILT_DIR` in CI
(or from the full root `Dockerfile` only for emergency/local use without CI),
backs up the database to `/opt/backups/edu`, applies versioned migrations backs up the database to `/opt/backups/edu`, applies versioned migrations
(`node db/migrate.mjs` — idempotent via `schema_migrations`), checks a (`node db/migrate.mjs` — idempotent via `schema_migrations`), checks a
candidate container, and replaces `aut-management-faculty`. It retains the candidate container, and replaces `aut-management-faculty`. It retains the
@@ -30,15 +36,6 @@ port 443, with `edu.mstaut.ir` as the origin host name. Leave the ACME
challenge path accessible for certificate renewal and bypass caching for challenge path accessible for certificate renewal and bypass caching for
admin pages, API requests, and authenticated traffic. admin pages, API requests, and authenticated traffic.
To deploy manually on the VPS:
```sh
cd /opt/prod/Aut.ManagementFaculty
git fetch origin master
git checkout --detach origin/master
bash scripts/deploy-production.sh
```
To inspect a deployment: To inspect a deployment:
```sh ```sh
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
# Assemble a Next.js standalone tree ready for Dockerfile.prebuilt (runtime-only image).
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OUT="${1:-$ROOT/prebuilt}"
test -f "$ROOT/.next/standalone/server.js"
test -f "$ROOT/.next/BUILD_ID"
rm -rf "$OUT"
mkdir -p "$OUT"
cp -a "$ROOT/.next/standalone/." "$OUT/"
rm -rf "$OUT/static" "$OUT/.next/static"
mkdir -p "$OUT/.next"
cp -a "$ROOT/.next/static/." "$OUT/.next/static/"
if [ -d "$ROOT/public" ]; then
rm -rf "$OUT/public"
cp -a "$ROOT/public" "$OUT/public"
else
mkdir -p "$OUT/public"
fi
cp "$ROOT/Dockerfile.prebuilt" "$OUT/Dockerfile"
printf '%s\n' 'Dockerfile' '.dockerignore' > "$OUT/.dockerignore"
test -f "$OUT/server.js"
test -f "$OUT/.next/BUILD_ID"
test -d "$OUT/.next/static"
test -d "$OUT/node_modules"
if [ -e "$OUT/static" ]; then
echo "ERROR: root-level static/ must not exist" >&2
exit 1
fi
echo "Prebuilt tree ready at $OUT"
+16
View File
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
export DISABLE_REACT_COMPILER=1
export NEXT_TELEMETRY_DISABLED=1
export NEXT_PUBLIC_SITE_URL="${NEXT_PUBLIC_SITE_URL:-https://edu.mstaut.ir}"
export NODE_OPTIONS="${NODE_OPTIONS:---max-old-space-size=3072}"
npm ci --no-audit --no-fund
npm run build
test -f .next/standalone/server.js
test -f .next/BUILD_ID
bash scripts/assemble-prebuilt.sh "$(pwd)/prebuilt"
+10 -1
View File
@@ -18,9 +18,18 @@ docker inspect insaight-postgres --format '{{json .NetworkSettings.Networks}}' |
docker volume create "$UPLOAD_VOLUME" >/dev/null docker volume create "$UPLOAD_VOLUME" >/dev/null
# Keep the current app running throughout the build. # Keep the current app running throughout the build.
DOCKER_BUILDKIT=1 docker build \ # PREBUILT_DIR: CI-assembled standalone tree (Dockerfile.prebuilt). Skips npm ci + next build on VPS (~1–2 min).
PREBUILT_DIR="${PREBUILT_DIR:-}"
if [ -n "$PREBUILT_DIR" ]; then
test -f "$PREBUILT_DIR/server.js"
test -f "$PREBUILT_DIR/Dockerfile"
rm -rf "$PREBUILT_DIR/static"
DOCKER_BUILDKIT=1 docker build -t "$IMAGE" "$PREBUILT_DIR"
else
DOCKER_BUILDKIT=1 docker build \
--build-arg NEXT_PUBLIC_SITE_URL=https://edu.mstaut.ir \ --build-arg NEXT_PUBLIC_SITE_URL=https://edu.mstaut.ir \
-t "$IMAGE" . -t "$IMAGE" .
fi
# Back up before applying the repository's transactional, versioned migrations. # Back up before applying the repository's transactional, versioned migrations.
# Migrations are forward-only and idempotent (schema_migrations); dump is the safety net. # Migrations are forward-only and idempotent (schema_migrations); dump is the safety net.