From e1b528694650dc018ae9bd9dedb790578acb6c2c Mon Sep 17 00:00:00 2001 From: Darian Date: Tue, 20 Oct 2026 01:17:13 +0330 Subject: [PATCH] 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. --- .gitea/workflows/deploy.yml | 38 +++++++++++++---------------------- docs/deployment.md | 21 +++++++++---------- scripts/assemble-prebuilt.sh | 39 ++++++++++++++++++++++++++++++++++++ scripts/ci-build-prebuilt.sh | 16 +++++++++++++++ scripts/deploy-production.sh | 15 +++++++++++--- 5 files changed, 90 insertions(+), 39 deletions(-) create mode 100644 scripts/assemble-prebuilt.sh create mode 100644 scripts/ci-build-prebuilt.sh diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index bfa10fb..04bd246 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -13,30 +13,20 @@ jobs: deploy: runs-on: local-host 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: - REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} - REPO_ACTOR: ${{ github.actor }} - REPO_NAME: ${{ github.repository }} - DEPLOY_SHA: ${{ github.sha }} + NEXT_PUBLIC_SITE_URL: https://edu.mstaut.ir + run: bash scripts/ci-build-prebuilt.sh + + - name: Roll out (runtime image, backup, migrate, smoke, swap) run: | set -euo pipefail - TARGET=/opt/prod/Aut.ManagementFaculty - mkdir -p "$TARGET" - 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" + rm -rf prebuilt/static + PREBUILT_DIR="$GITHUB_WORKSPACE/prebuilt" bash scripts/deploy-production.sh diff --git a/docs/deployment.md b/docs/deployment.md index 0052e89..f46382d 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -1,10 +1,16 @@ # Production deployment 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 (`node db/migrate.mjs` — idempotent via `schema_migrations`), checks a 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 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: ```sh diff --git a/scripts/assemble-prebuilt.sh b/scripts/assemble-prebuilt.sh new file mode 100644 index 0000000..cb0b455 --- /dev/null +++ b/scripts/assemble-prebuilt.sh @@ -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" diff --git a/scripts/ci-build-prebuilt.sh b/scripts/ci-build-prebuilt.sh new file mode 100644 index 0000000..d9bb0c2 --- /dev/null +++ b/scripts/ci-build-prebuilt.sh @@ -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" diff --git a/scripts/deploy-production.sh b/scripts/deploy-production.sh index c446f51..186e86a 100644 --- a/scripts/deploy-production.sh +++ b/scripts/deploy-production.sh @@ -18,9 +18,18 @@ docker inspect insaight-postgres --format '{{json .NetworkSettings.Networks}}' | docker volume create "$UPLOAD_VOLUME" >/dev/null # Keep the current app running throughout the build. -DOCKER_BUILDKIT=1 docker build \ - --build-arg NEXT_PUBLIC_SITE_URL=https://edu.mstaut.ir \ - -t "$IMAGE" . +# 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 \ + -t "$IMAGE" . +fi # Back up before applying the repository's transactional, versioned migrations. # Migrations are forward-only and idempotent (schema_migrations); dump is the safety net.