From 8233dd8a2451620f66046fe2c52646a2464ce1ad Mon Sep 17 00:00:00 2001 From: Master Date: Tue, 6 Oct 2026 11:45:44 +0330 Subject: [PATCH] Deploy edu production through same-host Gitea runner --- .gitea/workflows/deploy.yml | 35 +++++++++++++++ .github/workflows/deploy.yml | 6 +-- Dockerfile | 2 + docker-compose.yml | 4 +- docs/deployment.md | 45 +++++++++++++++++++ scripts/deploy-production.sh | 87 ++++++++++++++++++++++++++++++++++++ 6 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 .gitea/workflows/deploy.yml create mode 100644 docs/deployment.md create mode 100644 scripts/deploy-production.sh diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..54ff557 --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,35 @@ +name: Deploy edu.mstaut.ir + +on: + push: + branches: [master] + workflow_dispatch: + +concurrency: + group: edu-production + cancel-in-progress: false + +jobs: + deploy: + runs-on: local-host + steps: + - name: Checkout and deploy on the VPS + env: + REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO_ACTOR: ${{ github.actor }} + REPO_NAME: ${{ github.repository }} + DEPLOY_SHA: ${{ github.sha }} + 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 + # 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" diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 2f90382..6cda32e 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -11,7 +11,7 @@ env: IMAGE_NAME: aut-management-faculty:latest CONTAINER_NAME: aut-management-faculty NETWORK_NAME: shared_web_network - PUBLIC_HOST: aut.insaight.net + PUBLIC_HOST: edu.mstaut.ir DOCKER_BUILDKIT: "0" jobs: @@ -30,7 +30,7 @@ jobs: env: DISABLE_REACT_COMPILER: "1" NEXT_TELEMETRY_DISABLED: "1" - NEXT_PUBLIC_SITE_URL: https://aut.insaight.net + NEXT_PUBLIC_SITE_URL: https://edu.mstaut.ir run: | set -euo pipefail npm ci --no-audit --no-fund @@ -139,7 +139,7 @@ jobs: env: # Same-host Docker: use Postgres container DNS name on shared_web_network. # Public IP (185.36.145.12) works from outside the server, not from sibling containers. - DATABASE_URL: postgresql://managment_web:hD3H9seTqX1GJMu1mDJBGDYr@insaight-postgres:5432/ManagmentWebSite_Db + DATABASE_URL: ${{ secrets.DATABASE_URL }} run: | set -euo pipefail docker rm -f "$CONTAINER_NAME" 2>/dev/null || true diff --git a/Dockerfile b/Dockerfile index db88d0d..f0d2b77 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,8 @@ RUN npm ci # ---- Builder ---- FROM node:22-alpine AS builder WORKDIR /app +ARG NEXT_PUBLIC_SITE_URL=https://edu.mstaut.ir +ENV NEXT_PUBLIC_SITE_URL=$NEXT_PUBLIC_SITE_URL ENV NEXT_TELEMETRY_DISABLED=1 # Skip React Compiler in image builds (big CPU/RAM cost on self-hosted runners) ENV DISABLE_REACT_COMPILER=1 diff --git a/docker-compose.yml b/docker-compose.yml index 62d173f..320f9ef 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,7 +12,7 @@ services: - PORT=3000 - HOSTNAME=0.0.0.0 - NEXT_PUBLIC_SITE_URL=${NEXT_PUBLIC_SITE_URL:-https://edu.mstaut.ir} - - DATABASE_URL=${DATABASE_URL:-postgresql://managment_web:password@insaight-postgres:5432/ManagmentWebSite_Db} + - DATABASE_URL=${DATABASE_URL:?Set DATABASE_URL in a protected environment file} # Nginx Proxy Manager: edu.mstaut.ir → http://aut-management-faculty:3000 # DB: same Docker network as container `insaight-postgres` (not the public IP). expose: @@ -20,7 +20,7 @@ services: networks: - shared_web_network ports: - - "3000:3000" + - "127.0.0.1:3000:3000" networks: shared_web_network: diff --git a/docs/deployment.md b/docs/deployment.md new file mode 100644 index 0000000..675f384 --- /dev/null +++ b/docs/deployment.md @@ -0,0 +1,45 @@ +# 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, +backs up the database, applies versioned migrations, checks a candidate +container, and replaces `aut-management-faculty`. It retains the previous +container and automatically restores it if the new container fails its +initial HTTPS check. Database migrations are not automatically reversed. + +Production settings are stored in `/etc/edu-production.env`, owned by root +with mode `600`. It contains `DATABASE_URL`, `NEXT_PUBLIC_SITE_URL`, +`NODE_ENV`, `PORT`, and `HOSTNAME`. Database credentials must never be +committed. The database host is `insaight-postgres` on +`shared_web_network`. Database dumps are in `/opt/backups/edu`. + +Nginx proxies HTTPS to `127.0.0.1:3000`; the application port is bound only +to loopback. Nginx configuration is +`/etc/nginx/sites-available/edu.mstaut.ir.conf`. Certbot manages the +certificate under `/etc/letsencrypt/live/edu.mstaut.ir`, with automatic +renewal via `certbot.timer`. + +When enabling ArvanCloud, use HTTPS for the connection to the origin on +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 +docker logs --tail 100 aut-management-faculty +curl --resolve edu.mstaut.ir:443:127.0.0.1 https://edu.mstaut.ir/ +certbot renew --cert-name edu.mstaut.ir --dry-run +``` diff --git a/scripts/deploy-production.sh b/scripts/deploy-production.sh new file mode 100644 index 0000000..730d84a --- /dev/null +++ b/scripts/deploy-production.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "$0")/.." +exec 9>/var/lock/edu-production.lock +flock 9 + +ENV_FILE=/etc/edu-production.env +CONTAINER=aut-management-faculty +NETWORK=shared_web_network +REVISION=$(git rev-parse HEAD) +IMAGE="aut-management-faculty:$REVISION" + +test -s "$ENV_FILE" || { echo "Missing protected production environment: $ENV_FILE" >&2; exit 1; } +docker network inspect "$NETWORK" >/dev/null +docker inspect insaight-postgres --format '{{json .NetworkSettings.Networks}}' | grep -q '"shared_web_network"' + +# Keep the current app running throughout the build. +docker build --build-arg NEXT_PUBLIC_SITE_URL=https://edu.mstaut.ir -t "$IMAGE" . + +# Back up before applying the repository's transactional, versioned migrations. +install -d -m 700 /opt/backups/edu +BACKUP="/opt/backups/edu/database-$(date -u +%Y%m%dT%H%M%SZ).dump" +docker exec insaight-postgres sh -c 'pg_dump -U "$POSTGRES_USER" -d ManagmentWebSite_Db -Fc' > "$BACKUP" +chmod 600 "$BACKUP" +test -s "$BACKUP" +docker run --rm --network "$NETWORK" --env-file "$ENV_FILE" \ + -v "$PWD/db:/app/db:ro" --entrypoint node "$IMAGE" db/migrate.mjs + +# Check the database and rendered pages before switching the public upstream. +CANDIDATE="edu-candidate-${REVISION:0:12}" +docker run -d --name "$CANDIDATE" --network "$NETWORK" --env-file "$ENV_FILE" "$IMAGE" +cleanup_candidate() { docker rm -f "$CANDIDATE" >/dev/null 2>&1 || true; } +trap cleanup_candidate EXIT +READY=false +for attempt in $(seq 1 60); do + if docker exec "$CANDIDATE" node -e ' + (async () => { + const { Pool } = require("pg"); + const pool = new Pool({connectionString: process.env.DATABASE_URL, connectionTimeoutMillis: 5000}); + await pool.query("SELECT 1"); await pool.end(); + for (const route of ["/", "/programs", "/professional", "/admin/login"]) { + const response = await fetch("http://127.0.0.1:3000" + route); + if (!response.ok) throw new Error(route + ": " + response.status); + } + for (const resource of ["courses", "programs", "events", "articles"]) { + const response = await fetch("http://127.0.0.1:3000/api/" + resource); + const body = await response.json(); + if (!response.ok || !Array.isArray(body.data)) throw new Error("API check failed: " + resource); + } + })().catch(error => { console.error(error.message); process.exit(1); }); + '; then READY=true; break; fi + sleep 2 +done +if [ "$READY" != true ]; then docker logs --tail 50 "$CANDIDATE"; exit 1; fi +cleanup_candidate +trap - EXIT + +# Preserve the previous container for rollback if startup or HTTPS fails. +PREVIOUS="edu-previous-$(date -u +%Y%m%dT%H%M%SZ)" +HAD_PREVIOUS=false +if docker container inspect "$CONTAINER" >/dev/null 2>&1; then + docker stop "$CONTAINER" + docker rename "$CONTAINER" "$PREVIOUS" + HAD_PREVIOUS=true +fi +rollback() { + docker rm -f "$CONTAINER" >/dev/null 2>&1 || true + if [ "$HAD_PREVIOUS" = true ]; then + docker rename "$PREVIOUS" "$CONTAINER" + docker start "$CONTAINER" + fi +} +trap rollback ERR +docker run -d --name "$CONTAINER" --restart unless-stopped \ + --network "$NETWORK" --env-file "$ENV_FILE" -p 127.0.0.1:3000:3000 "$IMAGE" +READY=false +for attempt in $(seq 1 30); do + if curl -fsS --max-time 5 --resolve edu.mstaut.ir:443:127.0.0.1 https://edu.mstaut.ir/ >/dev/null; then + READY=true; break + fi + sleep 2 +done +test "$READY" = true +trap - ERR +docker image tag "$IMAGE" aut-management-faculty:latest +echo "Deployed $REVISION at https://edu.mstaut.ir (previous container: $PREVIOUS)"