Refactor PR preview workflow for improved approval process #37
Workflow file for this run
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: Build and Deploy Cloudflare Preview | ||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| pr_number: | ||
| description: 'The pull request number' | ||
| required: true | ||
| type: string | ||
| pr_head_sha: | ||
| description: 'The SHA of the PR head commit' | ||
| required: true | ||
| type: string | ||
| pr_checkout_repository: | ||
| description: 'The repository to checkout (owner/repo)' | ||
| required: true | ||
| type: string | ||
| secrets: | ||
| cloudflare_api_token: | ||
| description: 'Cloudflare API Token' | ||
| required: true | ||
| cloudflare_account_id: | ||
| description: 'Cloudflare Account ID' | ||
| required: true | ||
| github_token: | ||
|
Check failure on line 25 in .github/workflows/cloudflare-preview.yml
|
||
| description: 'GitHub Token' | ||
| required: true | ||
| permissions: | ||
| pull-requests: write # To allow commenting on the PR | ||
| jobs: | ||
| build-deploy-and-comment: | ||
| name: Build, Deploy, and Comment | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout PR Code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| repository: ${{ inputs.pr_checkout_repository }} | ||
| ref: ${{ inputs.pr_head_sha }} | ||
| - name: Setup Ruby | ||
| uses: ruby/setup-ruby@v1 | ||
| with: | ||
| ruby-version: '3.4' | ||
| bundler-cache: true | ||
| - name: Build site | ||
| run: bundle exec rake rdoc | ||
| - name: Deploy to Cloudflare Pages | ||
| id: deploy | ||
| uses: cloudflare/wrangler-action@v3 | ||
| with: | ||
| apiToken: ${{ secrets.cloudflare_api_token }} | ||
| accountId: ${{ secrets.cloudflare_account_id }} | ||
| command: pages deploy ./_site --project-name=rdoc --branch="${{ inputs.pr_number }}-preview" | ||
| - name: Comment on PR with preview URL | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| github-token: ${{ secrets.github_token }} | ||
| script: | | ||
| const prNumber = ${{ inputs.pr_number }}; | ||
| const url = "${{ steps.deploy.outputs.deployment-url }}"; | ||
| const commentMarker = "🚀 Preview deployment available at:"; | ||
| const commitSha = '${{ inputs.pr_head_sha }}'; | ||
| const comments = await github.rest.issues.listComments({ | ||
| issue_number: prNumber, | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| per_page: 100 | ||
| }); | ||
| const existingComment = comments.data.find(comment => | ||
| comment.body.includes(commentMarker) | ||
| ); | ||
| const commentBody = `${commentMarker} [${url}](${url}) (commit: ${commitSha})`; | ||
| if (existingComment) { | ||
| await github.rest.issues.updateComment({ | ||
| comment_id: existingComment.id, | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| body: commentBody | ||
| }); | ||
| console.log("Updated existing preview comment"); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| issue_number: prNumber, | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| body: commentBody | ||
| }); | ||
| console.log("Created new preview comment"); | ||
| } | ||