name: Deploy Frontend to GitHub Pages
on:
push:
branches:
- main
- dev
- '**'
paths:
- 'frontend/**'
- '.github/workflows/deploy-frontend.yml'
workflow_dispatch:
jobs:
check-changes:
runs-on: ubuntu-latest
outputs:
should_deploy: ${{ steps.changes.outputs.should_deploy }}
branch_name: ${{ steps.determine_branch.outputs.branch_name }}
base_path: ${{ steps.determine_branch.outputs.base_path }}
steps:
- name: Checkout source repo
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Check for changes in frontend
id: changes
run: |
# Check if there are any changes in the frontend directory or workflow file
if git diff --name-only HEAD~1 HEAD | grep -qE "^frontend/|^\.github/workflows/deploy-frontend\.yml$"; then
echo "Frontend changes detected"
echo "should_deploy=true" >> $GITHUB_OUTPUT
else
echo "No frontend changes detected"
echo "should_deploy=false" >> $GITHUB_OUTPUT
fi
- name: Determine branch and base path
id: determine_branch
run: |
BRANCH_NAME="${GITHUB_REF##*/}"
if [ "$BRANCH_NAME" = "main" ]; then
BASE_PATH="/"
elif [ "$BRANCH_NAME" = "dev" ]; then
BASE_PATH="/dev/"
else
BASE_PATH="/daily/"
fi
echo "Branch: $BRANCH_NAME, Base path: $BASE_PATH"
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
echo "base_path=$BASE_PATH" >> $GITHUB_OUTPUT
build-and-deploy:
needs: check-changes
if: needs.check-changes.outputs.should_deploy == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout source repo
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Cache Node modules
uses: actions/cache@v4
with:
path: frontend/node_modules
key: ${{ runner.os }}-node-modules-${{ hashFiles('frontend/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-modules-
- name: Cache build output
uses: actions/cache@v4
with:
path: frontend/dist
key: ${{ runner.os }}-node-build-${{ hashFiles('frontend/package-lock.json') }}-${{ hashFiles('frontend/src/**', 'frontend/vite.config.ts') }}
restore-keys: |
${{ runner.os }}-node-build-${{ hashFiles('frontend/package-lock.json') }}-
${{ runner.os }}-node-build-
- name: Install dependencies
run: npm ci
working-directory: frontend
- name: Run frontend unit tests
run: npm run test:unit
working-directory: frontend
- name: Run frontend linting
run: npm run lint
working-directory: frontend
- name: Configure base path based on branch
run: |
BASE_PATH="${{ needs.check-changes.outputs.base_path }}"
if [ "$BASE_PATH" != "/" ]; then
echo "Updating vite.config.ts to use base path: $BASE_PATH"
sed -i.bak "s|base: '/'|base: '$BASE_PATH'|g" frontend/vite.config.ts
fi
- name: Configure API endpoint for deployment
run: |
BASE_PATH="${{ needs.check-changes.outputs.base_path }}"
# Create a config file to inject the API endpoint
mkdir -p frontend/public
if [ "$BASE_PATH" != "/" ]; then
# For subdirectory deployments, use production API endpoint
echo "// Production API configuration" > frontend/public/config.js
echo "window.IMITATOR_CONFIG = { defaultBackendUrl: 'https://imitatort.zeyuan.site' };" >> frontend/public/config.js
else
# For main branch, could use a different endpoint if needed
echo "// Production API configuration" > frontend/public/config.js
echo "window.IMITATOR_CONFIG = { defaultBackendUrl: 'https://imitatort.zeyuan.site' };" >> frontend/public/config.js
fi
- name: Build
run: npm run build
working-directory: frontend
- name: Restore vite.config.ts
if: always()
run: |
# Restore the original vite.config.ts if backup exists
if [ -f "frontend/vite.config.ts.bak" ]; then
mv frontend/vite.config.ts.bak frontend/vite.config.ts
fi
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.PAT }}
external_repository: ImitatorT/ImitatorT.github.io
publish_branch: main
publish_dir: frontend/dist
destination_dir: ${{ needs.check-changes.outputs.base_path == '/' && '.' || (needs.check-changes.outputs.base_path == '/dev/' && 'dev' || 'daily') }}
skip-deployment:
needs: check-changes
if: needs.check-changes.outputs.should_deploy == 'false'
runs-on: ubuntu-latest
steps:
- name: Skip deployment - no frontend changes
run: echo "No frontend changes detected, skipping deployment"