Skip to content

Merge pull request #538 from ACM-VIT/dev #201

Merge pull request #538 from ACM-VIT/dev

Merge pull request #538 from ACM-VIT/dev #201

name: Deploy App Service
on:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
BUILD_DIR: .appservice-build
concurrency:
group: deploy-appservice-production
cancel-in-progress: true
jobs:
deploy:
name: Build and Deploy
runs-on: ubuntu-latest
environment:
name: production
url: https://exam-cooker.acmvit.in
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
AUTH_SECRET: ci-auth-secret-for-build-only
NEXTAUTH_SECRET: ci-auth-secret-for-build-only
AUTH_GOOGLE_ID: ci-google-id.apps.googleusercontent.com
AUTH_GOOGLE_SECRET: ci-google-secret
NEXT_PUBLIC_BASE_URL: https://examcooker.acmvit.in
NEXT_PUBLIC_MICROSERVICE_URL: https://upload.examcooker.acmvit.in
NEXT_PUBLIC_POSTHOG_KEY: ${{ secrets.NEXT_PUBLIC_POSTHOG_KEY }}
NEXT_PUBLIC_POSTHOG_HOST: ${{ vars.NEXT_PUBLIC_POSTHOG_HOST }}
NEXT_PUBLIC_POSTHOG_PROXY_PATH: ${{ vars.NEXT_PUBLIC_POSTHOG_PROXY_PATH }}
# Source-map upload is opt-in: it only runs once POSTHOG_CLI_TOKEN (a
# personal API key) and the POSTHOG_CLI_ENV_ID project id are configured.
# Until then this evaluates to "false" and the build behaves exactly as
# before (no source maps emitted, no upload step).
POSTHOG_SOURCEMAP_UPLOAD: ${{ secrets.POSTHOG_CLI_TOKEN != '' && vars.POSTHOG_CLI_ENV_ID != '' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 24
- name: Prepare build workspace
run: |
set -eux
rm -rf "${BUILD_DIR}"
mkdir -p "${BUILD_DIR}"
rsync -a \
--delete \
--exclude .git \
--exclude node_modules \
--exclude .next \
--exclude .turbo \
--exclude .appservice-build \
--exclude .DS_Store \
./ "${BUILD_DIR}/"
- name: Restore Next.js build cache
uses: actions/cache@v4
with:
path: ${{ env.BUILD_DIR }}/.next/cache
key: ${{ runner.os }}-next-${{ hashFiles('package.json', 'next.config.ts', 'tsconfig.json', 'tailwind.config.ts', 'postcss.config.mjs', 'prisma/schema.prisma') }}
restore-keys: |
${{ runner.os }}-next-
- name: Install dependencies
working-directory: ${{ env.BUILD_DIR }}
run: npm install --no-fund --no-audit
- name: Build application
working-directory: ${{ env.BUILD_DIR }}
run: npm run build
# Symbolicate client stack traces in PostHog error tracking. Injects a chunk
# id into each built JS chunk, uploads the matching source maps, then strips
# the `.map` files so they are never served publicly. Gated on the CLI
# secrets and best-effort (continue-on-error) so it can never block a deploy.
- name: Upload source maps to PostHog
if: ${{ env.POSTHOG_SOURCEMAP_UPLOAD == 'true' }}
continue-on-error: true
working-directory: ${{ env.BUILD_DIR }}
env:
POSTHOG_CLI_TOKEN: ${{ secrets.POSTHOG_CLI_TOKEN }}
POSTHOG_CLI_ENV_ID: ${{ vars.POSTHOG_CLI_ENV_ID }}
# PostHog region host for the CLI; defaults to EU to match this project.
POSTHOG_CLI_HOST: ${{ vars.POSTHOG_CLI_HOST || 'https://eu.posthog.com' }}
run: |
set -eux
npx --yes @posthog/cli@latest --host "${POSTHOG_CLI_HOST}" sourcemap inject --directory .next/static
npx --yes @posthog/cli@latest --host "${POSTHOG_CLI_HOST}" sourcemap upload --directory .next/static
- name: Remove public source maps
if: ${{ always() && env.POSTHOG_SOURCEMAP_UPLOAD == 'true' }}
working-directory: ${{ env.BUILD_DIR }}
run: |
set -eu
find .next/static -name '*.map' -type f -delete
- name: Package deployment payload
run: |
set -eux
rm -rf deploy
mkdir -p deploy
rsync -a "${BUILD_DIR}/.next/standalone/" deploy/
mkdir -p deploy/.next
rsync -a "${BUILD_DIR}/.next/static/" deploy/.next/static/
if [ -d "${BUILD_DIR}/prisma" ]; then
rsync -a "${BUILD_DIR}/prisma/" deploy/prisma/
fi
if [ -d "${BUILD_DIR}/lib/generated" ]; then
mkdir -p deploy/lib/generated
rsync -a "${BUILD_DIR}/lib/generated/" deploy/lib/generated/
fi
cp "${BUILD_DIR}/startup.sh" deploy/startup.sh
if [ -d "${BUILD_DIR}/public" ]; then
rsync -a "${BUILD_DIR}/public/" deploy/public/
fi
cd deploy
zip -qry ../webapp-package.zip .
- name: Deploy to Azure App Service
uses: azure/webapps-deploy@v3
with:
app-name: examcooker-2024
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: webapp-package.zip