Merge origin/master: keep upload volume and secure DATABASE_URL
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
commit
a99af426db
6 files changed
+174
-5
No files matched your search
@@ -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"
|
||||||
@@ -11,7 +11,7 @@ env:
|
|||||||
IMAGE_NAME: aut-management-faculty:latest
|
IMAGE_NAME: aut-management-faculty:latest
|
||||||
CONTAINER_NAME: aut-management-faculty
|
CONTAINER_NAME: aut-management-faculty
|
||||||
NETWORK_NAME: shared_web_network
|
NETWORK_NAME: shared_web_network
|
||||||
PUBLIC_HOST: aut.insaight.net
|
PUBLIC_HOST: edu.mstaut.ir
|
||||||
DOCKER_BUILDKIT: "0"
|
DOCKER_BUILDKIT: "0"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
@@ -30,7 +30,7 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
DISABLE_REACT_COMPILER: "1"
|
DISABLE_REACT_COMPILER: "1"
|
||||||
NEXT_TELEMETRY_DISABLED: "1"
|
NEXT_TELEMETRY_DISABLED: "1"
|
||||||
NEXT_PUBLIC_SITE_URL: https://aut.insaight.net
|
NEXT_PUBLIC_SITE_URL: https://edu.mstaut.ir
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
npm ci --no-audit --no-fund
|
npm ci --no-audit --no-fund
|
||||||
@@ -139,7 +139,7 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
# Same-host Docker: use Postgres container DNS name on shared_web_network.
|
# 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.
|
# 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: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
|
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ RUN npm ci
|
|||||||
# ---- Builder ----
|
# ---- Builder ----
|
||||||
FROM node:22-alpine AS builder
|
FROM node:22-alpine AS builder
|
||||||
WORKDIR /app
|
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
|
ENV NEXT_TELEMETRY_DISABLED=1
|
||||||
# Skip React Compiler in image builds (big CPU/RAM cost on self-hosted runners)
|
# Skip React Compiler in image builds (big CPU/RAM cost on self-hosted runners)
|
||||||
ENV DISABLE_REACT_COMPILER=1
|
ENV DISABLE_REACT_COMPILER=1
|
||||||
|
|||||||
+2
-2
@@ -12,7 +12,7 @@ services:
|
|||||||
- PORT=3000
|
- PORT=3000
|
||||||
- HOSTNAME=0.0.0.0
|
- HOSTNAME=0.0.0.0
|
||||||
- NEXT_PUBLIC_SITE_URL=${NEXT_PUBLIC_SITE_URL:-https://edu.mstaut.ir}
|
- 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}
|
||||||
- UPLOAD_DIR=/app/public/uploads
|
- UPLOAD_DIR=/app/public/uploads
|
||||||
# Nginx Proxy Manager: edu.mstaut.ir → http://aut-management-faculty:3000
|
# Nginx Proxy Manager: edu.mstaut.ir → http://aut-management-faculty:3000
|
||||||
# DB: same Docker network as container `insaight-postgres` (not the public IP).
|
# DB: same Docker network as container `insaight-postgres` (not the public IP).
|
||||||
@@ -23,7 +23,7 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- shared_web_network
|
- shared_web_network
|
||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "127.0.0.1:3000:3000"
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
uploads_data:
|
uploads_data:
|
||||||
|
|||||||
@@ -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
|
||||||
|
```
|
||||||
@@ -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)"
|
||||||
Reference in new issue
Block a user