# Tests every push and pull request; deploys pushes to master to the Plesk server. # Setup and what the deploy may touch: docs/deployment.md. # # Same pattern as the calendar frontend (Calendar_Frontend/.gitea/workflows/deploy.yml): build here, # deliver over SSH as the Plesk subscription user, restart Passenger. Unlike there, the app folder # also holds things the deploy must never touch (.env with the secrets, logs/, tmp/), so only the # app's own folders are synced with --delete and nothing is deleted at the top level. name: Test and deploy on: push: branches: [master] pull_request: jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version-file: .nvmrc - run: npm ci - run: npx tsc --noEmit # Unit tests only. The integration tests (npm run test:integration) start their own MariaDB # container and aren't run here. # FEEDBACK_IP_SALT (which the Jenkins job exported) isn't needed: vitest.config.ts sets one. - run: npm test deploy: needs: test if: github.event_name == 'push' && github.ref == 'refs/heads/master' runs-on: ubuntu-latest env: DEPLOY_HOST: ${{ vars.DEPLOY_HOST }} DEPLOY_PORT: ${{ vars.DEPLOY_PORT }} DEPLOY_USER: ${{ vars.DEPLOY_USER }} DEPLOY_PATH: ${{ vars.DEPLOY_PATH }} # Absolute on purpose: rsync passes its -e string to ssh without a shell, and ssh expands "~" # from the passwd entry, not $HOME (see the calendar frontend's docs/gotchas.md). SSH_DIR: /tmp/deploy-ssh steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version-file: .nvmrc - run: npm ci - run: npm run build # Only runtime dependencies go to the server (no typescript, vitest, ...). - run: npm prune --omit=dev - name: SSH setup env: DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }} DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }} run: | test -n "$DEPLOY_HOST" && test -n "$DEPLOY_USER" && test -n "$DEPLOY_PATH" if ! command -v rsync >/dev/null 2>&1 || ! command -v ssh >/dev/null 2>&1; then apt-get update && apt-get install -y --no-install-recommends rsync openssh-client fi install -m 700 -d "$SSH_DIR" printf '%s\n' "$DEPLOY_SSH_KEY" > "$SSH_DIR/key" chmod 600 "$SSH_DIR/key" # Pinned host key, never trust-on-first-use. printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$SSH_DIR/known_hosts" - name: Upload run: | rsh="ssh -p ${DEPLOY_PORT:-22} -i $SSH_DIR/key -o UserKnownHostsFile=$SSH_DIR/known_hosts -o IdentitiesOnly=yes -o BatchMode=yes" target="$DEPLOY_USER@$DEPLOY_HOST:$DEPLOY_PATH" # The app's own folders, each mirrored exactly (--delete within the folder only). # src/ is needed at runtime: swagger-jsdoc reads the route docs from src/models/**/*.ts. for dir in dist src public node_modules; do rsync -az --delete -e "$rsh" "$dir/" "$target/$dir/" done # Single files at the top level, without --delete: .env, logs/ and tmp/ live there too. # package.json is what makes Node treat dist/ as ES modules ("type": "module"). rsync -az -e "$rsh" package.json package-lock.json "$target/" - name: Restart run: | # Passenger restarts the app on its next request when tmp/restart.txt is touched. ssh -p "${DEPLOY_PORT:-22}" -i "$SSH_DIR/key" -o "UserKnownHostsFile=$SSH_DIR/known_hosts" \ -o IdentitiesOnly=yes -o BatchMode=yes "$DEPLOY_USER@$DEPLOY_HOST" \ "mkdir -p '$DEPLOY_PATH/tmp' && touch '$DEPLOY_PATH/tmp/restart.txt'" # Set DEPLOY_SMOKE_URL to an endpoint that must answer 200 without a session, e.g. # https://api.nachklang.art/calendar/events/public/json (what nachklang.art itself reads). - name: Smoke test if: vars.DEPLOY_SMOKE_URL != '' env: DEPLOY_SMOKE_URL: ${{ vars.DEPLOY_SMOKE_URL }} run: | # Passenger starts the app on the first request after the restart; give it a moment. for _ in 1 2 3 4 5 6; do if curl -fsS -o /dev/null "$DEPLOY_SMOKE_URL"; then echo "OK: $DEPLOY_SMOKE_URL"; exit 0; fi sleep 5 done echo "Smoke test failed: $DEPLOY_SMOKE_URL" exit 1 - name: Remove key if: always() run: rm -f "$SSH_DIR/key"