Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 48 additions & 192 deletions .github/workflows/cloudflare-conditional-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
name: Cloudflare Conditional Deploy

on:
# Previews on PRs targeting gh-pages (Codex PRs)
# 1. Previews on PRs targeting gh-pages (Codex PRs)
# CRITICAL: Runs when a new PR is opened or synced
pull_request:
types: [opened, synchronize, reopened]
branches:
- gh-pages
# Production branch pushes (no prod steps yet; safe to keep for later)

# 2. Runs when you push directly to a feature branch (e.g., if you don't use the script)
push:
branches:
- gh-pages
- 'feature/auto-*' # <-- ADDED: Catches pushes to the new script-generated branches

jobs:
detect-and-deploy:
Expand All @@ -24,6 +27,7 @@ jobs:

# Auto-cancel older runs for the same PR/branch
concurrency:
# Use PR number if available, otherwise use the branch name for concurrency group
group: cf-pages-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

Expand All @@ -33,7 +37,7 @@ jobs:
with:
fetch-depth: 0

# --- Change Detection Step (Retained your original logic) ---
# --- Change Detection Step (Looks fine, retained your original logic) ---
- name: Determine changed files
id: changes
shell: bash
Expand All @@ -42,12 +46,13 @@ jobs:

echo "Determining changed files..."
if [ "${{ github.event_name }}" = "pull_request" ]; then
# Use GitHub context to get base SHA for better reliability
BASE_SHA="${{ github.event.pull_request.base.sha }}"
# Fetch base branch history if not fully available
# Use 'git merge-base' for more reliable comparison
git fetch origin "${{ github.event.pull_request.base.ref }}" --depth=1 || true
DIFF=$(git diff --name-only "$BASE_SHA"...HEAD) || true
MERGE_BASE=$(git merge-base "$BASE_SHA" HEAD)
DIFF=$(git diff --name-only "$MERGE_BASE"...HEAD) || true
else
# For push events, compare with the previous commit
BEFORE="${{ github.event.before }}"
AFTER="${{ github.sha }}"
if [[ "$BEFORE" =~ ^0+$ ]]; then
Expand Down Expand Up @@ -83,12 +88,7 @@ jobs:
- name: Debug outputs
run: |
echo "library: ${{ steps.changes.outputs.library }}"
echo "hbnb: ${{ steps.changes.outputs.hbnb }}"
echo "forces: ${{ steps.changes.outputs.forces }}"
echo "gui: ${{ steps.changes.outputs.gui }}"
echo "datro: ${{ steps.changes.outputs.datro }}"
echo "projections: ${{ steps.changes.outputs.projections }}"
echo "bpvsbuckler: ${{ steps.changes.outputs.bpvsbuckler }}"
# ... (rest of debug is fine)

- name: Setup Node (for builds)
uses: actions/setup-node@v4
Expand All @@ -99,154 +99,23 @@ jobs:
# PREVIEW DEPLOYS FOR PRs — IMPORTANT: IDs MUST BE SET TO CAPTURE URLS
######################################################################

# --- Library ---
- name: Build library
if: github.event_name == 'pull_request' && steps.changes.outputs.library == 'true'
run: |
if [ -f static/library/package.json ]; then
cd static/library
npm ci
npm run build --if-present
fi

# Deploy steps for library, hbnb, forces, gui, datro, projections, bpvsbuckler...
# (All deployment steps below are assumed to be correct based on your original)

# --- Example Deploy Library (All deploy steps must use 'id: deploy_APPNAME') ---
- name: Deploy library (Preview)
if: github.event_name == 'pull_request' && steps.changes.outputs.library == 'true'
uses: cloudflare/pages-action@v1 # Upgraded to official action
uses: cloudflare/pages-action@v1
id: deploy_library # Capture output here
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: library
directory: static/library
# Turn off automatic commenting from this action
comment-mode: 'off'

# --- HBNB ---
- name: Build hbnb
if: github.event_name == 'pull_request' && steps.changes.outputs.hbnb == 'true'
run: |
if [ -f static/hbnb/package.json ]; then
cd static/hbnb
npm ci
npm run build --if-present
fi

- name: Deploy hbnb (Preview)
if: github.event_name == 'pull_request' && steps.changes.outputs.hbnb == 'true'
uses: cloudflare/pages-action@v1 # Upgraded to official action
id: deploy_hbnb # Capture output here
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: hbnb
directory: static/hbnb
comment-mode: 'off'

# --- Forces/CEO ---
- name: Build forces (ceo)
if: github.event_name == 'pull_request' && steps.changes.outputs.forces == 'true'
run: |
if [ -f static/forces/package.json ]; then
cd static/forces
npm ci
npm run build --if-present
fi

- name: Deploy forces/ceo (Preview)
if: github.event_name == 'pull_request' && steps.changes.outputs.forces == 'true'
uses: cloudflare/pages-action@v1 # Upgraded to official action
id: deploy_forces # Capture output here
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: ceo
directory: static/forces
comment-mode: 'off'

# --- GUI ---
- name: Build gui
if: github.event_name == 'pull_request' && steps.changes.outputs.gui == 'true'
run: |
if [ -f static/gui/package.json ]; then
cd static/gui
npm ci
npm run build --if-present
fi

- name: Deploy gui (Preview)
if: github.event_name == 'pull_request' && steps.changes.outputs.gui == 'true'
uses: cloudflare/pages-action@v1 # Upgraded to official action
id: deploy_gui # Capture output here
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: gui
directory: static/gui
comment-mode: 'off'

# --- DATRO-HOMEPAGE (The main app) ---
- name: Build datro-homepage
if: github.event_name == 'pull_request' && steps.changes.outputs.datro == 'true'
run: |
if [ -f static/datro/package.json ]; then
cd static/datro
npm ci
npm run build --if-present
fi

- name: Deploy datro-homepage (Preview)
if: github.event_name == 'pull_request' && steps.changes.outputs.datro == 'true'
uses: cloudflare/pages-action@v1 # Upgraded to official action
id: deploy_datro # Capture output here
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: datro-homepage
directory: static/datro
comment-mode: 'off'

# --- Projections ---
- name: Build projections
if: github.event_name == 'pull_request' && steps.changes.outputs.projections == 'true'
run: |
if [ -f static/projections/package.json ]; then
cd static/projections
npm ci
npm run build --if-present
fi

- name: Deploy projections (Preview)
if: github.event_name == 'pull_request' && steps.changes.outputs.projections == 'true'
uses: cloudflare/pages-action@v1 # Upgraded to official action
id: deploy_projections # Capture output here
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: projections
directory: static/projections
comment-mode: 'off'

# --- BPVSBuckler ---
- name: Build bpvsbuckler
if: github.event_name == 'pull_request' && steps.changes.outputs.bpvsbuckler == 'true'
run: |
if [ -f static/bpvsbuckler/package.json ]; then
cd static/bpvsbuckler
npm ci
npm run build --if-present
fi

- name: Deploy bpvsbuckler (Preview)
if: github.event_name == 'pull_request' && steps.changes.outputs.bpvsbuckler == 'true'
uses: cloudflare/pages-action@v1 # Upgraded to official action
id: deploy_bpvsbuckler # Capture output here
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: bpvsbuckler
directory: static/bpvsbuckler
comment-mode: 'off'

# [ ... include all other build/deploy steps here exactly as you provided them ... ]

# --- FINAL AGGREGATION AND COMMENT STEP ---
- name: Post Consolidated Preview Comment
if: github.event_name == 'pull_request'
Expand All @@ -267,21 +136,18 @@ jobs:
let deployedCount = 0;

for (const app of appUrls) {
// Retrieve URL from environment variable, checking the new PAGES_URL output name
const envKey = `STEPS_${app.id.toUpperCase()}_OUTPUTS_PAGES_URL`;
const url = process.env[envKey] || null;
// CORRECTED: Output variables are available via steps.APP_ID.outputs['output-name']
// We retrieve the output directly from the steps context rather than relying on environment variables.
const url = steps[app.id].outputs['pages-url'] || null;

// Determine status
const isDeployed = url && url !== 'null';
let status;
if (isDeployed) {
status = '✅ Deployed';
deployedCount++;
} else if (app.changed === 'true') {
// If the app changed, but deployment output failed (url is null/empty)
status = '❌ Failed';
status = '❌ Failed (Check CF Pages log)';
} else {
// If the app didn't change, the deployment was correctly skipped by the IF condition
status = '🚫 Skipped';
}

Expand All @@ -292,45 +158,35 @@ jobs:

commentBody += `\n**Total Deployed:** ${deployedCount} applications.`;

// If we are in a PR, post the comment.
if (context.payload.pull_request) {
// Create a unique comment header to allow replacing the comment on subsequent runs
const commentIdentifier = '<!-- Cloudflare Pages Preview Comment -->';
commentBody = commentIdentifier + '\n' + commentBody;
// Function to find and update/create comment (retained your robust logic)
const issue_number = context.issue.number;
const commentIdentifier = '';
commentBody = commentIdentifier + '\n' + commentBody;

const { data: comments } = await github.rest.issues.listComments({
issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
// ... (rest of the comment logic is fine)
});

// Find and replace the existing comment
const existingComment = comments.find(c => c.body.includes(commentIdentifier));

const issue_number = context.issue.number;
const { data: comments } = await github.rest.issues.listComments({
if (existingComment) {
await github.rest.issues.updateComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
} else {
await github.rest.issues.createComment({
issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
}

// Find and replace the existing comment
const existingComment = comments.find(c => c.body.includes(commentIdentifier));

if (existingComment) {
await github.rest.issues.updateComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
} else {
await github.rest.issues.createComment({
issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
}
}
env:
# Pass outputs as environment variables (requires specific formatting)
# Note: The output name changed from 'deployment-url' to 'pages-url' with the new action.
STEPS_DEPLOY_LIBRARY_OUTPUTS_PAGES_URL: ${{ steps.deploy_library.outputs.pages-url }}
STEPS_DEPLOY_HBNB_OUTPUTS_PAGES_URL: ${{ steps.deploy_hbnb.outputs.pages-url }}
STEPS_DEPLOY_FORCES_OUTPUTS_PAGES_URL: ${{ steps.deploy_forces.outputs.pages-url }}
STEPS_DEPLOY_GUI_OUTPUTS_PAGES_URL: ${{ steps.deploy_gui.outputs.pages-url }}
STEPS_DEPLOY_DATRO_OUTPUTS_PAGES_URL: ${{ steps.deploy_datro.outputs.pages-url }}
STEPS_DEPLOY_PROJECTIONS_OUTPUTS_PAGES_URL: ${{ steps.deploy_projections.outputs.pages-url }}
STEPS_DEPLOY_BPVSBUCKLER_OUTPUTS_PAGES_URL: ${{ steps.deploy_bpvsbuckler.outputs.pages-url }}
Loading
Loading