187 lines
6.0 KiB
YAML
187 lines
6.0 KiB
YAML
name: Build & Deploy (Self-Hosted)
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- master
|
|
- main
|
|
workflow_dispatch:
|
|
|
|
env:
|
|
IMAGE_NAME: aut-management-faculty:latest
|
|
CONTAINER_NAME: aut-management-faculty
|
|
NETWORK_NAME: shared_web_network
|
|
PUBLIC_HOST: aut.insaight.net
|
|
DOCKER_BUILDKIT: "0"
|
|
|
|
jobs:
|
|
build:
|
|
name: Build (ubuntu)
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- uses: actions/setup-node@v4
|
|
with:
|
|
node-version: "22"
|
|
cache: npm
|
|
|
|
- name: Install & build
|
|
env:
|
|
DISABLE_REACT_COMPILER: "1"
|
|
NEXT_TELEMETRY_DISABLED: "1"
|
|
NEXT_PUBLIC_SITE_URL: https://aut.insaight.net
|
|
run: |
|
|
set -euo pipefail
|
|
npm ci --no-audit --no-fund
|
|
npm run build
|
|
test -f .next/standalone/server.js
|
|
test -f .next/BUILD_ID
|
|
|
|
- name: Assemble runnable standalone tree
|
|
run: |
|
|
set -euo pipefail
|
|
rm -rf prebuilt
|
|
mkdir -p prebuilt
|
|
|
|
# Flatten standalone to artifact root (includes .next + node_modules)
|
|
cp -a .next/standalone/. prebuilt/
|
|
|
|
# Next must NOT see a root-level ./static (legacy); only ./.next/static
|
|
rm -rf prebuilt/static
|
|
rm -rf prebuilt/.next/static
|
|
mkdir -p prebuilt/.next
|
|
cp -a .next/static/. prebuilt/.next/static/
|
|
|
|
if [ -d public ]; then
|
|
rm -rf prebuilt/public
|
|
cp -a public prebuilt/public
|
|
else
|
|
mkdir -p prebuilt/public
|
|
fi
|
|
|
|
# Build recipe for deploy job (excluded from image via .dockerignore)
|
|
cp Dockerfile.prebuilt prebuilt/Dockerfile
|
|
printf '%s\n' 'Dockerfile' '.dockerignore' > prebuilt/.dockerignore
|
|
|
|
echo "Assembled tree:"
|
|
ls -la prebuilt
|
|
ls -la prebuilt/.next
|
|
test -f prebuilt/server.js
|
|
test -f prebuilt/.next/BUILD_ID
|
|
test -d prebuilt/.next/static
|
|
test -d prebuilt/node_modules
|
|
# Fail if legacy root static leaked in
|
|
if [ -e prebuilt/static ]; then
|
|
echo "ERROR: root-level static/ must not exist" >&2
|
|
exit 1
|
|
fi
|
|
|
|
- uses: actions/upload-artifact@v4
|
|
with:
|
|
name: next-standalone
|
|
path: prebuilt/
|
|
retention-days: 1
|
|
include-hidden-files: true
|
|
|
|
deploy:
|
|
name: Deploy (self-hosted)
|
|
runs-on: [self-hosted, linux]
|
|
needs: build
|
|
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
|
|
steps:
|
|
- name: Download artifact
|
|
uses: actions/download-artifact@v4
|
|
with:
|
|
name: next-standalone
|
|
path: prebuilt
|
|
|
|
- name: Ensure shared_web_network exists
|
|
run: |
|
|
docker network inspect "$NETWORK_NAME" >/dev/null 2>&1 \
|
|
|| docker network create "$NETWORK_NAME"
|
|
|
|
- name: Verify artifact & build image
|
|
run: |
|
|
set -euo pipefail
|
|
echo "== artifact root =="
|
|
ls -la prebuilt
|
|
echo "== artifact .next =="
|
|
ls -la prebuilt/.next
|
|
|
|
test -f prebuilt/server.js
|
|
test -f prebuilt/.next/BUILD_ID
|
|
test -d prebuilt/.next/static
|
|
test -d prebuilt/node_modules
|
|
test -f prebuilt/Dockerfile
|
|
|
|
# Drop legacy root static from context if present (Next deprecation)
|
|
rm -rf prebuilt/static
|
|
|
|
docker build -t "$IMAGE_NAME" prebuilt
|
|
|
|
echo "== image checks =="
|
|
docker run --rm --entrypoint sh "$IMAGE_NAME" -c '
|
|
set -e
|
|
ls -la /app
|
|
ls -la /app/.next
|
|
test -f /app/server.js || { echo "FAIL: missing /app/server.js"; exit 1; }
|
|
test -f /app/.next/BUILD_ID || { echo "FAIL: missing /app/.next/BUILD_ID"; exit 1; }
|
|
test -d /app/.next/static || { echo "FAIL: missing /app/.next/static"; exit 1; }
|
|
if [ -e /app/static ]; then
|
|
echo "FAIL: legacy /app/static still present"
|
|
exit 1
|
|
fi
|
|
echo "OK: runtime files present"
|
|
'
|
|
|
|
- name: Restart container
|
|
env:
|
|
# Postgres runs on the Docker host. From inside a bridge network,
|
|
# the public IP often times out (hairpin/NAT). Reach it via the
|
|
# host gateway instead — requires Postgres listening on 0.0.0.0
|
|
# (or docker0) and pg_hba allowing the Docker subnet.
|
|
DATABASE_URL: postgresql://managment_web:hD3H9seTqX1GJMu1mDJBGDYr@host.docker.internal:5432/ManagmentWebSite_Db
|
|
run: |
|
|
set -euo pipefail
|
|
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
|
|
|
|
RUN_ARGS=(
|
|
-d
|
|
--name "$CONTAINER_NAME"
|
|
--restart unless-stopped
|
|
--network "$NETWORK_NAME"
|
|
--add-host=host.docker.internal:host-gateway
|
|
-e NODE_ENV=production
|
|
-e PORT=3000
|
|
-e HOSTNAME=0.0.0.0
|
|
-e "DATABASE_URL=${DATABASE_URL}"
|
|
--expose 3000
|
|
)
|
|
|
|
docker run "${RUN_ARGS[@]}" "$IMAGE_NAME"
|
|
sleep 2
|
|
docker logs "$CONTAINER_NAME" 2>&1 | tail -n 30
|
|
|
|
# Fail the job early if the app cannot reach Postgres from the container
|
|
if ! docker exec "$CONTAINER_NAME" node -e "
|
|
const { Pool } = require('pg');
|
|
const pool = new Pool({
|
|
connectionString: process.env.DATABASE_URL,
|
|
connectionTimeoutMillis: 5000,
|
|
});
|
|
pool.query('SELECT 1')
|
|
.then(() => { console.log('DB_OK'); return pool.end(); })
|
|
.catch(async (e) => { console.error('DB_FAIL', e.message); try { await pool.end(); } catch {} process.exit(1); });
|
|
"; then
|
|
echo "::error::Container cannot reach Postgres via DATABASE_URL"
|
|
docker logs "$CONTAINER_NAME" 2>&1 | tail -n 50
|
|
exit 1
|
|
fi
|
|
|
|
echo "Deployed ${CONTAINER_NAME} on ${NETWORK_NAME}"
|
|
echo "Point Nginx Proxy Manager: ${PUBLIC_HOST} → http://${CONTAINER_NAME}:3000"
|
|
|
|
- name: Prune dangling images
|
|
run: docker image prune -f
|