Skip to content

Merge develop: Fix dual-branch GitHub Pages deployment #5

Merge develop: Fix dual-branch GitHub Pages deployment

Merge develop: Fix dual-branch GitHub Pages deployment #5

Workflow file for this run

name: Build and Deploy
# Run on push to main/master/develop branch
on:
push:
branches:
- main
- master
- develop
workflow_dispatch: # Allow manual triggering
# Grant necessary permissions
permissions:
contents: write
pages: write
id-token: write
issues: write
pull-requests: write
# Ensure only one deployment runs at a time per branch
concurrency:
group: "pages-${{ github.ref_name }}"
cancel-in-progress: false
jobs:
# Job 1: Build with Vite
build:
name: Build with Vite
needs: []
runs-on: ubuntu-latest
outputs:
build_status: ${{ steps.build_check.outputs.status }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.ref_name }}
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: |
echo "📦 Installing dependencies..."
npm ci
- name: Build with Vite
id: build_check
run: |
echo "🏗️ Building with Vite..."
npm run build
# Check if build succeeded
if [ ! -d "dist" ]; then
echo "❌ Build failed - dist directory not created!"
echo "status=failed" >> $GITHUB_OUTPUT
exit 1
fi
# Verify critical files exist
if [ ! -f "dist/index.html" ]; then
echo "❌ Build failed - index.html not found in dist!"
echo "status=failed" >> $GITHUB_OUTPUT
exit 1
fi
echo "✅ Vite build completed successfully"
echo "📦 Build output:"
ls -lh dist/
echo ""
echo "📦 Assets:"
ls -lh dist/assets/ | head -20
echo "status=success" >> $GITHUB_OUTPUT
- name: Copy additional files to dist
run: |
echo "📋 Copying additional files..."
# Copy files that Vite doesn't process but are needed
cp -r vendor dist/ 2>/dev/null || echo "No vendor directory to copy"
cp -r fonts dist/ 2>/dev/null || echo "No fonts directory to copy"
cp -r PolliLibJS dist/ 2>/dev/null || echo "No PolliLibJS directory to copy"
cp robots.txt dist/ 2>/dev/null || echo "No robots.txt to copy"
cp sitemap.xml dist/ 2>/dev/null || echo "No sitemap.xml to copy"
cp BingSiteAuth.xml dist/ 2>/dev/null || echo "No BingSiteAuth.xml to copy"
cp visitor-tracking.js dist/ 2>/dev/null || echo "No visitor-tracking.js to copy"
# Copy page-specific JS files
cp about/about.js dist/about/ 2>/dev/null || echo "No about.js to copy"
cp ai/demo/age-verification.js dist/ai/demo/ 2>/dev/null || echo "No age-verification.js to copy"
cp ai/demo/js/main.js dist/ai/demo/js/ 2>/dev/null || mkdir -p dist/ai/demo/js && cp ai/demo/js/main.js dist/ai/demo/js/ || echo "No main.js to copy"
echo "✅ Additional files copied"
- name: Upload artifact for deployment
uses: actions/upload-pages-artifact@v3
with:
path: 'dist'
# Job 4a: Report Build Status
report-status:
name: Report Build Status
needs: build
runs-on: ubuntu-latest
if: always()
steps:
- name: Report success
if: needs.build.outputs.build_status == 'success'
run: |
echo "✅ BUILD SUCCESSFUL"
echo "================================"
echo "Built with: Vite"
echo "Status: SUCCESS"
echo "Ready for deployment"
echo "================================"
- name: Report failure
if: needs.build.outputs.build_status == 'failed'
run: |
echo "❌ BUILD FAILED"
echo "================================"
echo "Built with: Vite"
echo "Status: FAILED"
echo "Check build logs for details"
echo "================================"
exit 1
- name: Create status comment (if PR)
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const status = '${{ needs.build.outputs.build_status }}';
const icon = status === 'success' ? '✅' : '❌';
const message = status === 'success' ? 'Build successful!' : 'Build failed!';
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `${icon} **${message}**\n\n**Built with:** Vite\n**Status:** ${status.toUpperCase()}`
});
# Job 4b: Deploy to GitHub Pages
deploy:
name: Deploy to GitHub Pages
needs: build
runs-on: ubuntu-latest
if: needs.build.outputs.build_status == 'success'
steps:
- name: Checkout gh-pages branch
uses: actions/checkout@v4
with:
ref: gh-pages
path: gh-pages-repo
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: github-pages
path: artifact
- name: Extract artifact
run: |
cd artifact
tar -xf artifact.tar
mkdir -p ../dist
mv -f * ../dist/ 2>/dev/null || true
cd ..
rm -rf artifact
- name: Determine deployment path and update files
id: deploy-path
run: |
if [ "${{ github.ref_name }}" == "develop" ]; then
echo "📍 Deploying develop branch to /development/ subdirectory"
echo "url=https://www.unityailab.com/development/" >> $GITHUB_OUTPUT
# Remove old development folder and copy new build
rm -rf gh-pages-repo/development
mkdir -p gh-pages-repo/development
cp -r dist/* gh-pages-repo/development/
echo "✅ Updated /development/ folder only"
else
echo "📍 Deploying main branch to root directory"
echo "url=https://www.unityailab.com/" >> $GITHUB_OUTPUT
# Preserve CNAME and development folder
if [ -f "gh-pages-repo/CNAME" ]; then
cp gh-pages-repo/CNAME /tmp/CNAME
echo "💾 Preserved CNAME file"
fi
if [ -d "gh-pages-repo/development" ]; then
mv gh-pages-repo/development /tmp/development
echo "💾 Preserved /development/ folder"
fi
# Clear root and copy new build
rm -rf gh-pages-repo/*
cp -r dist/* gh-pages-repo/
# Restore preserved files
if [ -f "/tmp/CNAME" ]; then
mv /tmp/CNAME gh-pages-repo/CNAME
echo "♻️ Restored CNAME file"
fi
if [ -d "/tmp/development" ]; then
mv /tmp/development gh-pages-repo/development
echo "♻️ Restored /development/ folder"
fi
echo "✅ Updated root files while preserving /development/"
fi
- name: Commit and push to gh-pages
run: |
cd gh-pages-repo
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git add -A
if git diff --staged --quiet; then
echo "No changes to deploy"
else
git commit -m "Deploy from ${{ github.ref_name }} branch - $(date +'%Y-%m-%d %H:%M:%S')"
git push origin gh-pages
echo "✅ Deployed successfully"
fi
- name: Report deployment success
run: |
echo "🚀 DEPLOYMENT SUCCESSFUL"
echo "================================"
echo "Branch: ${{ github.ref_name }}"
echo "URL: ${{ steps.deploy-path.outputs.url }}"
echo "Built with: Vite (optimized)"
echo "================================"
echo ""
echo "✨ Your site is now live with the latest updates!"
echo "📦 Bundled, minified, and optimized for production"