Skip to content

Refactor PR preview workflow for improved approval process #37

Refactor PR preview workflow for improved approval process

Refactor PR preview workflow for improved approval process #37

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

View workflow run for this annotation

GitHub Actions / .github/workflows/cloudflare-preview.yml

Invalid workflow file

secret name `github_token` within `workflow_call` can not be used since it would collide with system reserved name
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");
}