diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 04bd246..7b1bd37 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -13,20 +13,27 @@ jobs: deploy: runs-on: local-host steps: - - 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) + - name: Build and deploy on the VPS env: - NEXT_PUBLIC_SITE_URL: https://edu.mstaut.ir - run: bash scripts/ci-build-prebuilt.sh - - - name: Roll out (runtime image, backup, migrate, smoke, swap) + REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO_ACTOR: ${{ github.actor }} + REPO_NAME: ${{ github.repository }} + DEPLOY_SHA: ${{ github.sha }} run: | set -euo pipefail - rm -rf prebuilt/static - PREBUILT_DIR="$GITHUB_WORKSPACE/prebuilt" bash scripts/deploy-production.sh + 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 + rm -f \ + "$TARGET/.git/shallow.lock" \ + "$TARGET/.git/index.lock" \ + "$TARGET/.git/HEAD.lock" \ + "$TARGET/.git/config.lock" + 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/vps-autodeploy.sh" diff --git a/scripts/vps-autodeploy.sh b/scripts/vps-autodeploy.sh new file mode 100644 index 0000000..1cbe7f1 --- /dev/null +++ b/scripts/vps-autodeploy.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Run on the VPS after the repo is checked out (Gitea local-host runner). +# No GitHub Actions dependencies — only git, Docker, and bash. +set -euo pipefail + +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +cd "$ROOT" + +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}" + +echo "Building Next.js at $ROOT (Node 22 container)…" +DOCKER_BUILDKIT=1 docker run --rm \ + -e DISABLE_REACT_COMPILER=1 \ + -e NEXT_TELEMETRY_DISABLED=1 \ + -e NEXT_PUBLIC_SITE_URL \ + -e NODE_OPTIONS \ + -v "$ROOT:/app" -w /app \ + --mount type=cache,target=/root/.npm \ + node:22-alpine \ + sh -ec 'npm ci --no-audit --no-fund && npm run build' + +bash scripts/assemble-prebuilt.sh "$ROOT/prebuilt" +rm -rf "$ROOT/prebuilt/static" +PREBUILT_DIR="$ROOT/prebuilt" bash scripts/deploy-production.sh