This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to Single Cloudflare Worker | ||
on: | ||
push: | ||
branches: [main, v1] | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
permissions: | ||
contents: read | ||
deployments: write | ||
pull-requests: write # For PR comments | ||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
submodules: recursive | ||
- name: Set up Hugo (extended) | ||
uses: peaceiris/actions-hugo@v2 | ||
with: | ||
extended: true | ||
- name: Make scripts executable | ||
run: chmod +x scripts/*.sh tools/*/*/*.sh || true | ||
- name: Build site with Makefile | ||
run: make dist | ||
- name: Determine deployment configuration | ||
id: deploy-config | ||
run: | | ||
if [[ "${{ github.event_name }}" == "pull_request" ]]; then | ||
# For PRs, use PR number | ||
DEPLOY_PATH="pr-${{ github.event.number }}" | ||
URL_PATH="/docs/pr-${{ github.event.number }}" | ||
DEPLOYMENT_TYPE="pr" | ||
echo "π Deploying PR #${{ github.event.number }}" | ||
elif [[ "${{ github.ref_name }}" == "main" ]]; then | ||
DEPLOY_PATH="v2" | ||
URL_PATH="/docs/v2" | ||
DEPLOYMENT_TYPE="main" | ||
echo "π Deploying main branch to /docs/v2" | ||
elif [[ "${{ github.ref_name }}" == "v1" ]]; then | ||
DEPLOY_PATH="v1" | ||
URL_PATH="/docs/v1" | ||
DEPLOYMENT_TYPE="v1" | ||
echo "π Deploying v1 branch to /docs/v1" | ||
else | ||
echo "β Unsupported branch: ${{ github.ref_name }}" | ||
exit 1 | ||
fi | ||
echo "deploy-path=$DEPLOY_PATH" >> $GITHUB_OUTPUT | ||
echo "url-path=$URL_PATH" >> $GITHUB_OUTPUT | ||
echo "deployment-type=$DEPLOYMENT_TYPE" >> $GITHUB_OUTPUT | ||
- name: Create Worker with smart routing | ||
run: | | ||
mkdir -p src | ||
cat > src/index.js <<'JS' | ||
// Smart router for unionai-docs - serves different content based on URL path | ||
export default { | ||
async fetch(request, env) { | ||
const url = new URL(request.url); | ||
const pathname = url.pathname; | ||
console.log('Request for:', pathname); | ||
// Root redirects | ||
if (pathname === '/' || pathname === '/docs' || pathname === '/docs/') { | ||
return Response.redirect(url.origin + '/docs/v2/', 302); | ||
} | ||
// Route to appropriate content | ||
if (pathname.startsWith('/docs/v2/')) { | ||
if (env.ASSETS_V2) { | ||
return env.ASSETS_V2.fetch(request); | ||
} else { | ||
console.log('ASSETS_V2 not available'); | ||
return new Response('v2 content not yet deployed', { status: 503 }); | ||
} | ||
} | ||
if (pathname.startsWith('/docs/v1/')) { | ||
if (env.ASSETS_V1) { | ||
return env.ASSETS_V1.fetch(request); | ||
} else { | ||
console.log('ASSETS_V1 not available'); | ||
return new Response('v1 content not yet deployed', { status: 503 }); | ||
} | ||
} | ||
// Handle PR paths: /docs/pr-123/ | ||
const prMatch = pathname.match(/^\/docs\/(pr-\d+)\//); | ||
if (prMatch) { | ||
const prFolder = prMatch[1].toUpperCase().replace('-', '_'); // pr-123 -> PR_123 | ||
const assetBinding = env['ASSETS_' + prFolder]; | ||
if (assetBinding) { | ||
return assetBinding.fetch(request); | ||
} else { | ||
console.log('PR assets not found for:', prFolder); | ||
return new Response('PR content not found', { status: 404 }); | ||
} | ||
} | ||
// Fallback | ||
console.log('No route found for:', pathname); | ||
return new Response('Not found', { status: 404 }); | ||
} | ||
}; | ||
JS | ||
- name: Create wrangler configuration | ||
run: | | ||
cat > wrangler.json <<EOF | ||
{ | ||
"name": "unionai-docs-smart", | ||
"compatibility_date": "2025-08-28", | ||
"main": "src/index.js" | ||
} | ||
EOF | ||
- name: Debug - Check deployment setup | ||
run: | | ||
echo "=== Deployment Configuration ===" | ||
echo "Branch/Event: ${{ github.ref_name }} (${{ github.event_name }})" | ||
echo "Deploy Path: ${{ steps.deploy-config.outputs.deploy-path }}" | ||
echo "URL Path: ${{ steps.deploy-config.outputs.url-path }}" | ||
echo "Type: ${{ steps.deploy-config.outputs.deployment-type }}" | ||
echo "" | ||
echo "=== Built site structure ===" | ||
find dist -type f | head -10 | ||
echo "" | ||
echo "=== Worker configuration ===" | ||
cat wrangler.json | ||
echo "" | ||
echo "=== Worker code preview ===" | ||
head -20 src/index.js | ||
- name: Deploy Worker (initial/structure only) | ||
uses: cloudflare/wrangler-action@v3 | ||
with: | ||
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
command: deploy | ||
- name: Output deployment information | ||
run: | | ||
WORKER_URL="https://unionai-docs-smart.${CF_WORKERS_SUBDOMAIN:-summer-butterfly-4aa4}.workers.dev" | ||
CONTENT_URL="${WORKER_URL}${{ steps.deploy-config.outputs.url-path }}" | ||
echo "π Worker deployed to: $WORKER_URL" | ||
echo "π Content available at: $CONTENT_URL" | ||
echo "" | ||
echo "β οΈ NOTE: This deployment only created the Worker structure." | ||
echo " Content upload for ${{ steps.deploy-config.outputs.deploy-path }} will be implemented in next steps." | ||
env: | ||
CF_WORKERS_SUBDOMAIN: ${{ secrets.CF_WORKERS_SUBDOMAIN }} | ||
- name: Comment on PR (if applicable) | ||
if: github.event_name == 'pull_request' | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const workerUrl = `https://unionai-docs-smart.${process.env.CF_WORKERS_SUBDOMAIN || 'summer-butterfly-4aa4'}.workers.dev`; | ||
const contentUrl = `${workerUrl}/docs/pr-${{ github.event.number }}`; | ||
await github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
body: `π **PR Preview Deployed** | ||
Preview URL: ${contentUrl} | ||
β οΈ **Note**: This is the first phase deployment (Worker structure only). Content upload is coming in the next implementation phase.` | ||
}); | ||
env: | ||
CF_WORKERS_SUBDOMAIN: ${{ secrets.CF_WORKERS_SUBDOMAIN }} |