Skip to content

Commit cd4934a

Browse files
authored
chore: update workflows (#1080)
1 parent a0bca90 commit cd4934a

2 files changed

Lines changed: 70 additions & 114 deletions

File tree

.github/workflows/cloudflare-preview-build.yml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,11 @@ jobs:
3232
env:
3333
CF_WORKERS: 1
3434

35-
- name: Prepare deployment files
36-
run: |
37-
# Copy wrangler config and worker code
38-
mkdir -p deploy-package
39-
cp -r apps/web/dist deploy-package/
40-
cp apps/web/wrangler.jsonc deploy-package/wrangler.jsonc
41-
cp -r apps/web/worker deploy-package/
42-
43-
# Create a minimal package.json without workspace protocol
44-
cat > deploy-package/package.json << 'EOF'
45-
{
46-
"name": "md-preview",
47-
"version": "1.0.0",
48-
"private": true
49-
}
50-
EOF
51-
5235
- name: Upload deployment package
5336
uses: actions/upload-artifact@v4
5437
with:
5538
name: cloudflare-dist
56-
path: deploy-package
39+
path: apps/web/dist/**
5740
retention-days: 5
5841

5942
- name: Save PR number
Lines changed: 69 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,100 @@
1-
name: Cloudflare Preview Deploy
1+
name: Cloudflare Workers Preview
22

33
on:
44
workflow_run:
55
workflows: ["Cloudflare Preview Build"]
66
types:
77
- completed
8+
workflow_dispatch:
9+
10+
concurrency:
11+
group: cloudflare-preview-${{ github.event.workflow_run.event.pull_request.number }}
12+
cancel-in-progress: true
813

914
jobs:
10-
deploy:
15+
deploy-preview:
1116
runs-on: ubuntu-latest
12-
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' && github.repository == 'doocs/md'
17+
if: github.repository == 'doocs/md'
1318
permissions:
1419
contents: read
1520
deployments: write
1621
pull-requests: write
17-
steps:
18-
- name: Checkout
19-
uses: actions/checkout@v5
2022

21-
- name: Download PR artifact
22-
uses: dawidd6/action-download-artifact@v11
23+
steps:
24+
- name: Download build artifact
25+
uses: actions/download-artifact@v4
2326
with:
24-
workflow: ${{ github.event.workflow_run.workflow_id }}
25-
run_id: ${{ github.event.workflow_run.id }}
26-
name: cloudflare-pr
27+
name: cloudflare-dist
28+
path: apps/web/dist
2729

28-
- name: Save PR id
29-
id: pr
30-
run: |
31-
pr_id=$(<pr-id.txt)
32-
if ! [[ "$pr_id" =~ ^[0-9]+$ ]]; then
33-
echo "Error: pr-id.txt does not contain a valid numeric PR id. Please check."
34-
exit 1
35-
fi
36-
echo "id=$pr_id" >> $GITHUB_OUTPUT
37-
38-
- name: Download dist artifact
39-
uses: dawidd6/action-download-artifact@v11
30+
- name: Download PR number
31+
uses: actions/download-artifact@v4
4032
with:
41-
workflow: ${{ github.event.workflow_run.workflow_id }}
42-
run_id: ${{ github.event.workflow_run.id }}
43-
workflow_conclusion: success
44-
name: cloudflare-dist
45-
path: deploy-package
33+
name: cloudflare-pr
34+
path: .
4635

47-
- name: Update wrangler config
48-
run: |
49-
cd deploy-package
50-
# Update the assets directory path in wrangler.jsonc
51-
sed -i 's|"directory": "./public/"|"directory": "./dist/"|g' wrangler.jsonc
36+
- name: Read PR number
37+
id: read-pr
38+
run: echo "pr_number=$(cat pr-id.txt)" >> $GITHUB_OUTPUT
5239

5340
- name: Deploy to Cloudflare Workers
5441
id: deploy
5542
uses: cloudflare/wrangler-action@v3
5643
with:
5744
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
5845
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
59-
command: deploy --name md-pr-${{ steps.pr.outputs.id }}
60-
workingDirectory: deploy-package
61-
packageManager: npm
46+
command: deploy --name md-pr-${{ steps.read-pr.outputs.pr_number }}
47+
workingDirectory: apps/web
6248
env:
6349
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
6450
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
6551

66-
- name: Comment PR with preview link
67-
uses: actions-cool/maintain-one-comment@v3
68-
with:
69-
token: ${{ secrets.GITHUB_TOKEN }}
70-
body: |
71-
🚀 Cloudflare Workers Preview has been successfully deployed!
72-
73-
**Preview URL:** https://md-pr-${{ steps.pr.outputs.id }}.doocs.workers.dev
74-
75-
<sub>Built with commit ${{ github.event.workflow_run.head_sha }}</sub>
76-
77-
<!-- Cloudflare Workers Preview Comment -->
78-
body-include: '<!-- Cloudflare Workers Preview Comment -->'
79-
number: ${{ steps.pr.outputs.id }}
80-
81-
- name: Deploy failed
82-
if: ${{ failure() }}
83-
uses: actions-cool/maintain-one-comment@v3
84-
with:
85-
token: ${{ secrets.GITHUB_TOKEN }}
86-
body: |
87-
😭 Cloudflare Workers Preview deployment failed.
88-
89-
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
90-
91-
<!-- Cloudflare Workers Preview Comment -->
92-
body-include: '<!-- Cloudflare Workers Preview Comment -->'
93-
number: ${{ steps.pr.outputs.id }}
94-
95-
failed:
96-
runs-on: ubuntu-latest
97-
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' && github.repository == 'doocs/md'
98-
steps:
99-
- name: Download PR artifact
100-
uses: dawidd6/action-download-artifact@v11
101-
with:
102-
workflow: ${{ github.event.workflow_run.workflow_id }}
103-
run_id: ${{ github.event.workflow_run.id }}
104-
name: cloudflare-pr
105-
106-
- name: Save PR id
107-
id: pr
52+
- name: Get deployment URL
53+
id: deployment-url
10854
run: |
109-
pr_id=$(<pr-id.txt)
110-
if ! [[ "$pr_id" =~ ^[0-9]+$ ]]; then
111-
echo "Error: pr-id.txt does not contain a valid numeric PR id. Please check."
112-
exit 1
113-
fi
114-
echo "id=$pr_id" >> $GITHUB_OUTPUT
115-
116-
- name: Comment PR with build failure
117-
uses: actions-cool/maintain-one-comment@v3
118-
with:
119-
token: ${{ secrets.GITHUB_TOKEN }}
120-
body: |
121-
😭 Cloudflare Workers Preview build failed.
55+
PREVIEW_URL="https://md-pr-${{ steps.read-pr.outputs.pr_number }}.doocs.workers.dev"
56+
echo "url=$PREVIEW_URL" >> $GITHUB_OUTPUT
57+
echo "Preview URL: $PREVIEW_URL"
12258
123-
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}) for details.
124-
125-
<!-- Cloudflare Workers Preview Comment -->
126-
body-include: '<!-- Cloudflare Workers Preview Comment -->'
127-
number: ${{ steps.pr.outputs.id }}
59+
- name: Comment PR with preview link
60+
uses: actions/github-script@v7
61+
with:
62+
github-token: ${{ secrets.GITHUB_TOKEN }}
63+
script: |
64+
const previewUrl = '${{ steps.deployment-url.outputs.url }}';
65+
const prNumber = Number('${{ steps.read-pr.outputs.pr_number }}');
66+
67+
const comments = await github.rest.issues.listComments({
68+
owner: context.repo.owner,
69+
repo: context.repo.repo,
70+
issue_number: prNumber,
71+
});
72+
73+
const botComment = comments.data.find(comment =>
74+
comment.user.type === 'Bot' &&
75+
comment.body.includes('Cloudflare Workers Preview')
76+
);
77+
78+
const commentBody = `## 🚀 Cloudflare Workers Preview
79+
80+
Your preview is ready!
81+
82+
**Preview URL:** ${previewUrl}
83+
84+
<sub>Built with commit ${context.workflow_run.head_sha}</sub>`;
85+
86+
if (botComment) {
87+
await github.rest.issues.updateComment({
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
comment_id: botComment.id,
91+
body: commentBody
92+
});
93+
} else {
94+
await github.rest.issues.createComment({
95+
owner: context.repo.owner,
96+
repo: context.repo.repo,
97+
issue_number: prNumber,
98+
body: commentBody
99+
});
100+
}

0 commit comments

Comments
 (0)