From 5cd9bf3dfb77d593f323db90d151dbbd6e182ac6 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 7 Mar 2025 18:57:58 +0000 Subject: [PATCH] ci: try to trigger deployments from cloudflare comments --- .github/workflows/cf-sync.yml | 132 ++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 .github/workflows/cf-sync.yml diff --git a/.github/workflows/cf-sync.yml b/.github/workflows/cf-sync.yml new file mode 100644 index 00000000..105ba91c --- /dev/null +++ b/.github/workflows/cf-sync.yml @@ -0,0 +1,132 @@ +name: 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 + steps: + - name: Get PR branch + id: get-pr + uses: actions/github-script@v6 + 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@v6 + with: + script: | + const commentBody = context.payload.comment.body; + + const isDeployStarting = commentBody.includes('Build in progress'); + const isDeployFinished = commentBody.includes('Deploy successful'); + + let status = ''; + let deployUrl = ''; + + if (isDeployStarting) { + status = 'starting'; + } else if (isDeployFinished) { + status = 'finished'; + deployUrl = commentBody.match(/Branch Preview URL:[\S\s]+/)[1]; + } else { + status = 'unknown'; + } + + console.log(`Detected deployment status: ${status}`); + return { status, deployUrl }; + + - name: Create deployment + id: deployment + if: steps.parse-comment.outputs.result.status == 'starting' || steps.parse-comment.outputs.result.status == 'finished' + uses: actions/github-script@v6 + with: + script: | + const { owner, repo } = context.repo; + const prNumber = context.issue.number; + const sha = steps.get-pr.outputs.result.sha; + const branch = steps.get-pr.outputs.result.branch; + const status = steps.parse-comment.outputs.result.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; + + // If there's no existing deployment or we're starting a new one + if (existingDeployments.data.length === 0 || 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 Pages deployment for PR #${prNumber}`, + environment: 'cloudflare-preview', + auto_merge: false, + required_contexts: [] + }); + + deploymentId = deployment.data.id; + console.log(`Created new deployment with ID: ${deploymentId}`); + + // If we're starting, set status to in_progress + if (status === 'starting') { + await github.rest.repos.createDeploymentStatus({ + owner, + repo, + deployment_id: deploymentId, + state: 'in_progress', + description: 'Cloudflare Pages is deploying' + }); + } + } else { + deploymentId = existingDeployments.data[0].id; + console.log(`Using existing deployment ID: ${deploymentId}`); + } + + return { id: deploymentId }; + + - name: Update deployment status (finished) + if: steps.parse-comment.outputs.result.status == 'finished' + uses: actions/github-script@v6 + with: + script: | + const { owner, repo } = context.repo; + const prNumber = context.issue.number; + const deployUrl = steps.parse-comment.outputs.result.deployUrl; + const deploymentId = steps.deployment.outputs.result.id; + + 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 Pages deployment successful' + }); + + console.log(`Updated deployment ${deploymentId} status to success with URL ${deployUrl}`); -- knotmirror 0.0.1