Files
Aut.ManagementFaculty/.github/workflows/deploy.yml
T

150 lines
4.4 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
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
docker build -t "$IMAGE_NAME" prebuilt
# Confirm BUILD_ID is actually inside the image
docker run --rm --entrypoint sh "$IMAGE_NAME" -c \
'test -f /app/.next/BUILD_ID && test -f /app/server.js && test -d /app/.next/static && test ! -e /app/static && ls -la /app && ls -la /app/.next'
- name: Restart container
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
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"
-e NODE_ENV=production
-e PORT=3000
-e HOSTNAME=0.0.0.0
--expose 3000
)
if [ -n "${DATABASE_URL:-}" ]; then
RUN_ARGS+=(-e "DATABASE_URL=${DATABASE_URL}")
fi
docker run "${RUN_ARGS[@]}" "$IMAGE_NAME"
sleep 2
docker logs "$CONTAINER_NAME" 2>&1 | tail -n 30
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