-
Notifications
You must be signed in to change notification settings - Fork 17
161 lines (142 loc) · 5.72 KB
/
cloudflare-web-preview.yml
File metadata and controls
161 lines (142 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
name: Cloudflare Worker Preview Deploy
on:
pull_request:
paths:
- 'src/**'
- 'index.html'
- 'package.json'
- 'package-lock.json'
- 'vite.config.ts'
- 'tsconfig.json'
- '.github/workflows/cloudflare-web-preview.yml'
- '.github/actions/setup/**'
push:
branches:
- dev
paths:
- 'src/**'
- 'index.html'
- 'package.json'
- 'package-lock.json'
- 'vite.config.ts'
- 'tsconfig.json'
- '.github/workflows/cloudflare-web-preview.yml'
- '.github/actions/setup/**'
concurrency:
group: cloudflare-worker-preview-${{ github.event.pull_request.number || github.ref_name }}
cancel-in-progress: true
jobs:
deploy:
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
- name: Prepare preview metadata
id: metadata
shell: bash
run: |
preview_message="$(git log -1 --pretty=%s)"
preview_message="$(printf '%s' "$preview_message" | head -c 100)"
{
echo 'preview_message<<EOF'
echo "$preview_message"
echo EOF
} >> "$GITHUB_OUTPUT"
- name: Setup app and build
uses: ./.github/actions/setup
with:
build: 'true'
- name: Set deploy alias
id: alias
shell: bash
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "alias=pr-${{ github.event.pull_request.number }}" >> "$GITHUB_OUTPUT"
else
branch="${GITHUB_REF_NAME}"
alias="$(echo "$branch" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g')"
echo "alias=${alias}" >> "$GITHUB_OUTPUT"
fi
- name: Upload Worker preview
id: deploy
uses: cloudflare/wrangler-action@da0e0dfe58b7a431659754fdf3f186c529afbe65 # v3.14.1
env:
PREVIEW_MESSAGE: ${{ steps.metadata.outputs.preview_message }}
with:
apiToken: ${{ secrets.TF_CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.TF_VAR_ACCOUNT_ID }}
command: >
versions upload
-c dist/wrangler.json
--preview-alias ${{ steps.alias.outputs.alias }}
--message "$PREVIEW_MESSAGE"
- name: Resolve preview URL
id: preview
env:
DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }}
COMMAND_OUTPUT: ${{ steps.deploy.outputs.command-output }}
ALIAS: ${{ steps.alias.outputs.alias }}
shell: bash
run: |
alias_url_pattern="^https?://${ALIAS}-[^[:space:]]+$"
preview_url=""
if printf '%s\n' "$DEPLOYMENT_URL" | grep -Eq "$alias_url_pattern"; then
preview_url="$DEPLOYMENT_URL"
else
preview_url="$(printf '%s\n' "$COMMAND_OUTPUT" | grep -Eo "https?://${ALIAS}-[^[:space:]\"'<>)]+" | head -n 1 || true)"
fi
if ! printf '%s\n' "$preview_url" | grep -Eq "$alias_url_pattern"; then
echo "Failed to resolve aliased Worker preview URL." >&2
exit 1
fi
echo "preview_url=${preview_url}" >> "$GITHUB_OUTPUT"
- name: Publish summary and PR comment
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
MARKER: '<!-- cloudflare-worker-preview -->'
PREVIEW_URL: ${{ steps.preview.outputs.preview_url }}
PREVIEW_ALIAS: ${{ steps.alias.outputs.alias }}
SHORT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const marker = process.env.MARKER;
const previewUrl = process.env.PREVIEW_URL;
const previewAlias = process.env.PREVIEW_ALIAS;
const shortSha = process.env.SHORT_SHA?.slice(0, 7);
const now = new Date().toUTCString().replace(':00 GMT', ' UTC');
if (!previewUrl) {
core.setFailed("Missing preview URL from Cloudflare deploy step.");
return;
}
const tableRow = "| ✅ Deployment successful! | <a href='" + previewUrl + "'>" + previewUrl + "</a> | " + shortSha + " | `" + previewAlias + "` | " + now + " |";
const comment = [
marker,
`## Deploying with <a href="https://workers.dev"><img alt="Cloudflare Workers" src="https://workers.cloudflare.com/logo.svg" width="16"></a> Cloudflare Workers`,
``,
`| Status | Preview URL | Commit | Alias | Updated (UTC) |`,
`| - | - | - | - | - |`,
tableRow,
].join("\n");
await core.summary.addRaw(comment.replace(marker + "\n", "")).write();
if (context.eventName !== 'pull_request') return;
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const comments = await github.paginate(github.rest.issues.listComments, {
owner, repo, issue_number,
});
const existing = comments.find(
(c) => c.user.type === "Bot" && c.body.includes(marker),
);
if (existing) {
await github.rest.issues.deleteComment({
owner, repo, comment_id: existing.id,
});
}
await github.rest.issues.createComment({ owner, repo, issue_number, body: comment });