Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/roe.dev. This is the code and content for my personal website, built in Nuxt. roe.dev
Something went wrong. Try again.
YAML
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215name: Cloudflare Deployment Sync
on: issue_comment: types: [created, edited]
jobs: process-cloudflare-comment: # Only process comments from the Cloudflare bot on PRs if: github.event.issue.pull_request && github.event.comment.user.login == 'cloudflare-workers-and-pages[bot]' runs-on: ubuntu-latest # Add workflow permissions to create deployments permissions: pull-requests: read contents: write deployments: write
steps: - name: Get PR branch id: get-pr uses: actions/github-script@v7 with: script: | const { owner, repo, number } = context.issue; const pr = await github.rest.pulls.get({ owner, repo, pull_number: number }); return { branch: pr.data.head.ref, sha: pr.data.head.sha };
- name: Parse comment for deployment status id: parse-comment uses: actions/github-script@v7 with: script: | const commentBody = context.payload.comment.body;
const isDeployStarting = commentBody.includes('In progress'); const isDeployFinished = commentBody.includes('Deployment successful!'); const isDeployFailed = commentBody.includes('❌') || commentBody.includes('Build failed') || commentBody.includes('Deployment failed');
let status = ''; let deployUrl = '';
if (isDeployStarting) { status = 'starting'; } else if (isDeployFinished) { status = 'finished'; // Extract URL from the new Cloudflare Workers format: <a href='https://...'>Commit Preview URL</a> const urlMatch = commentBody.match(/<a href='([^']+)'>Commit Preview URL<\/a>/); if (urlMatch) { deployUrl = urlMatch[1]; } } else if (isDeployFailed) { status = 'failed'; } else { status = 'unknown'; }
console.log(`Detected deployment status: ${status}`); console.log(`Deploy URL: ${deployUrl}`); // Set outputs directly core.setOutput('status', status); core.setOutput('deployUrl', deployUrl || '');
- name: Create deployment id: deployment # Use outputs directly, not nested in result if: steps.parse-comment.outputs.status == 'starting' || steps.parse-comment.outputs.status == 'finished' || steps.parse-comment.outputs.status == 'failed' uses: actions/github-script@v7 with: github-token: ${{ secrets.WORKFLOW_PAT || secrets.GITHUB_TOKEN }} script: | const { owner, repo } = context.repo; const prNumber = context.issue.number;
// Parse the JSON result string from get-pr step const prData = JSON.parse('${{ steps.get-pr.outputs.result }}'); const sha = prData.sha; const branch = prData.branch;
const status = '${{ steps.parse-comment.outputs.status }}';
console.log(`Processing deployment for PR #${prNumber}, branch ${branch}, SHA ${sha}, status: ${status}`);
// Get existing deployments for this environment and PR const existingDeployments = await github.rest.repos.listDeployments({ owner, repo, environment: 'cloudflare-preview', ref: sha });
let deploymentId; let existingInProgressDeployment = false;
// Check if we already have a deployment in progress if (existingDeployments.data.length > 0) { // For each existing deployment, check if it's in progress for (const deployment of existingDeployments.data) { // Ensure deployment is defined before accessing its id if (!deployment) continue;
const statuses = await github.rest.repos.listDeploymentStatuses({ owner, repo, deployment_id: deployment.id });
const latestStatus = statuses.data[0]; if (latestStatus && latestStatus.state === 'in_progress') { existingInProgressDeployment = true; deploymentId = deployment.id; console.log(`Found existing in-progress deployment with ID: ${deploymentId}`); break; } } }
// If there's no existing deployment or no in-progress deployment and we're starting a new one if ((existingDeployments.data.length === 0 || !existingInProgressDeployment) && status === 'starting') { console.log(`Creating new deployment for PR #${prNumber}, branch ${branch}, SHA ${sha}`);
const deployment = await github.rest.repos.createDeployment({ owner, repo, ref: sha, description: `Cloudflare Workers deployment for PR #${prNumber}`, environment: 'cloudflare-preview', auto_merge: false, required_contexts: [] });
// Ensure deployment.data exists before accessing id if (deployment && deployment.data) { deploymentId = deployment.data.id; console.log(`Created new deployment with ID: ${deploymentId}`);
// Set status to in_progress await github.rest.repos.createDeploymentStatus({ owner, repo, deployment_id: deploymentId, state: 'in_progress', description: 'Cloudflare Workers is deploying' }); } else { console.log('Failed to create deployment: deployment or deployment.data is undefined'); } } else if (!deploymentId && existingDeployments.data.length > 0 && existingDeployments.data[0]) { // If we didn't find an in-progress deployment but we have existing ones deploymentId = existingDeployments.data[0].id; console.log(`Using most recent deployment ID: ${deploymentId}`); }
// Only set deployment ID as output if it's defined if (deploymentId) { core.setOutput('deploymentId', deploymentId.toString()); } else { console.log('No valid deployment ID found'); core.setOutput('deploymentId', ''); }
- name: Update deployment status (finished) if: steps.parse-comment.outputs.status == 'finished' uses: actions/github-script@v7 with: github-token: ${{ secrets.WORKFLOW_PAT || secrets.GITHUB_TOKEN }} script: | const { owner, repo } = context.repo; const prNumber = context.issue.number; const deployUrl = '${{ steps.parse-comment.outputs.deployUrl }}'; const deploymentId = parseInt('${{ steps.deployment.outputs.deploymentId }}');
console.log(`Updating deployment ${deploymentId} for PR #${prNumber} with URL ${deployUrl}`);
await github.rest.repos.createDeploymentStatus({ owner, repo, deployment_id: deploymentId, state: 'success', environment_url: deployUrl, log_url: deployUrl, description: 'Cloudflare Workers deployment successful' });
console.log(`Updated deployment ${deploymentId} status to success with URL ${deployUrl}`);
- name: Update deployment status (failed) if: steps.parse-comment.outputs.status == 'failed' uses: actions/github-script@v7 with: github-token: ${{ secrets.WORKFLOW_PAT || secrets.GITHUB_TOKEN }} script: | const { owner, repo } = context.repo; const prNumber = context.issue.number; const deploymentId = parseInt('${{ steps.deployment.outputs.deploymentId }}');
console.log(`Updating deployment ${deploymentId} for PR #${prNumber} as failed`);
// Mark the deployment as failed await github.rest.repos.createDeploymentStatus({ owner, repo, deployment_id: deploymentId, state: 'failure', description: 'Cloudflare Workers deployment failed' });
console.log(`Updated deployment ${deploymentId} status to failure`);