1+ name : Build and Deploy Cloudflare Preview
2+
3+ on :
4+ repository_dispatch :
5+ types : [pr-preview-deploy]
6+
7+ permissions :
8+ pull-requests : write # To allow commenting on the PR
9+
10+ jobs :
11+ build-deploy-and-comment :
12+ name : Build, Deploy, and Comment
13+ runs-on : ubuntu-latest
14+ steps :
15+ - name : Checkout PR Code
16+ uses : actions/checkout@v4
17+ with :
18+ repository : ${{ github.event.client_payload.pr_checkout_repository }}
19+ ref : ${{ github.event.client_payload.pr_head_sha }}
20+
21+ - name : Setup Ruby
22+ uses : ruby/setup-ruby@v1
23+ with :
24+ ruby-version : ' 3.4'
25+ bundler-cache : true
26+
27+ - name : Build site
28+ run : bundle exec rake rdoc
29+
30+ - name : Deploy to Cloudflare Pages
31+ id : deploy
32+ uses : cloudflare/wrangler-action@v3
33+ with :
34+ apiToken : ${{ secrets.CLOUDFLARE_API_TOKEN }}
35+ accountId : ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
36+ command : pages deploy ./_site --project-name=rdoc --branch="${{ github.event.client_payload.pr_number }}-preview"
37+
38+ - name : Comment on PR with preview URL
39+ uses : actions/github-script@v7
40+ with :
41+ github-token : ${{ secrets.MATZBOT_GITHUB_TOKEN }}
42+ script : |
43+ const prNumber = ${{ github.event.client_payload.pr_number }};
44+ const url = "${{ steps.deploy.outputs.deployment-url }}";
45+ const commentMarker = "🚀 Preview deployment available at:";
46+ const commitSha = '${{ github.event.client_payload.pr_head_sha }}';
47+
48+ const comments = await github.rest.issues.listComments({
49+ issue_number: prNumber,
50+ owner: context.repo.owner,
51+ repo: context.repo.repo,
52+ per_page: 100
53+ });
54+
55+ const existingComment = comments.data.find(comment =>
56+ comment.body.includes(commentMarker)
57+ );
58+
59+ const commentBody = `${commentMarker} [${url}](${url}) (commit: ${commitSha})`;
60+
61+ if (existingComment) {
62+ await github.rest.issues.updateComment({
63+ comment_id: existingComment.id,
64+ owner: context.repo.owner,
65+ repo: context.repo.repo,
66+ body: commentBody
67+ });
68+ console.log("Updated existing preview comment");
69+ } else {
70+ await github.rest.issues.createComment({
71+ issue_number: prNumber,
72+ owner: context.repo.owner,
73+ repo: context.repo.repo,
74+ body: commentBody
75+ });
76+ console.log("Created new preview comment");
77+ }
0 commit comments