# Tests every push and pull request; deploys pushes to master to the Plesk server (api.plutodev.de). # Setup and what the deploy may touch: DEPLOYMENT.md. # # Same pattern as the Nachklang API (git.plutodev.de/Nachklang/API): build here, deliver over SSH as # the Plesk subscription user (plesk-plutodev), restart Passenger. 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: # One job, not test + deploy: every job starts in a fresh container, so two jobs meant checking out, # setting up Node and running `npm ci` twice (~2 min each, 2026-11-08). The deploy steps only run # for pushes to master; on pull requests the job ends after the tests. The condition is spelled out # on every deploy step rather than stored in an env flag: a flag compared as a string ('true') would # skip the deploy silently - and green - if the runner ever rendered the boolean differently. test-and-deploy: 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 (found in the Nachklang calendar migration, 2026-11-08). SSH_DIR: /tmp/deploy-ssh steps: - uses: actions/checkout@v4 # .nvmrc = 26, the Node version Plesk runs this app with from the switchover on (it was # 12.22.12 until 2026-11-16). Build and test with the same one. - uses: actions/setup-node@v4 with: node-version-file: .nvmrc - run: npm ci - run: npx tsc --noEmit - run: npm test - name: Build if: github.event_name == 'push' && github.ref == 'refs/heads/master' run: npm run build # Only runtime dependencies go to the server (no typescript, jest, ...). - name: Prune dev dependencies if: github.event_name == 'push' && github.ref == 'refs/heads/master' run: npm prune --omit=dev - name: SSH setup if: github.event_name == 'push' && github.ref == 'refs/heads/master' 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 if: github.event_name == 'push' && github.ref == 'refs/heads/master' 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). public/ is # not in the repo (gitignored) and not deployed - the server's own copy stays. # src/ is needed at runtime: swagger-jsdoc reads the route docs from src/models/**/*.ts. for dir in dist src 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 if: github.event_name == 'push' && github.ref == 'refs/heads/master' 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 a page that must answer 200 without a key, e.g. # https://api.plutodev.de/docs/ (the Swagger UI; also proves src/ was deployed). - name: Smoke test if: github.event_name == 'push' && github.ref == 'refs/heads/master' && 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() && github.event_name == 'push' && github.ref == 'refs/heads/master' run: rm -f "$SSH_DIR/key"