-
-
Notifications
You must be signed in to change notification settings - Fork 80
193 lines (157 loc) · 6.27 KB
/
deploy-preview.yml
File metadata and controls
193 lines (157 loc) · 6.27 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
name: Deploy Preview to Cloudflare Pages
on:
pull_request:
types: [opened, synchronize, closed]
concurrency:
group: preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
build:
if: github.event.action != 'closed'
env:
GH_TOKEN: ${{ github.token }}
runs-on: ubicloud-standard-2
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: "yarn"
- name: Add LLVM apt Repo
run: |-
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo add-apt-repository "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-21 main"
sudo apt update
- name: Install APT dependencies
run: xargs sudo apt-get install -y --no-install-recommends < Aptfile
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: bundle install
run: bundle install
- name: Render Templates
run: bundle exec rake templates
- name: Compile Herb
run: bundle exec rake make
- name: Yarn install
run: yarn install --frozen-lockfile
- name: Build tailwind-class-sorter package
run: yarn nx build @herb-tools/tailwind-class-sorter
- name: Build all JavaScript packages
run: yarn build
- name: Build docs site
run: cd docs && yarn build
- name: Upload docs artifact
uses: actions/upload-artifact@v4
with:
name: docs-preview
path: docs/.vitepress/dist
retention-days: 1
deploy-preview:
name: Deploy to Cloudflare Pages
needs: build
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
pull-requests: write
steps:
- name: Download docs artifact
uses: actions/download-artifact@v4
with:
name: docs-preview
path: dist
- name: Deploy to Cloudflare Pages
id: deploy
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: herb-tools
directory: dist
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
- name: Add comment with preview URL
uses: actions/github-script@v7
with:
script: |
const previewUrl = '${{ steps.deploy.outputs.url }}';
const comment = `## 🌿 Interactive Playground and Documentation Preview
A preview deployment has been built for this pull request. Try out the changes live in the interactive playground:
- **[Playground Preview](${previewUrl}/playground)**
- **[Documentation Preview](${previewUrl})**
- **[Website Preview](${previewUrl})**
---
<sub>🌱 Grown from commit [\`${context.payload.pull_request.head.sha.substring(0, 7)}\`](https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${context.payload.pull_request.head.sha})</sub>`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('🌿 Interactive Playground and Documentation Preview')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: comment
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
});
}
cleanup:
name: Cleanup preview deployment
if: github.event.action == 'closed'
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Delete Cloudflare Pages deployment
run: |
DEPLOYMENTS=$(curl -s -X GET \
"https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/herb-tools/deployments" \
-H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \
-H "Content-Type: application/json")
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
DEPLOYMENT_ID=$(echo "$DEPLOYMENTS" | jq -r ".result[] | select(.deployment_trigger.metadata.branch == \"$BRANCH_NAME\") | .id" | head -n 1)
if [ -n "$DEPLOYMENT_ID" ]; then
echo "Deleting deployment $DEPLOYMENT_ID for branch $BRANCH_NAME"
curl -X DELETE \
"https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/herb-tools/deployments/$DEPLOYMENT_ID" \
-H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \
-H "Content-Type: application/json"
else
echo "No deployment found for branch $BRANCH_NAME"
fi
- name: Update PR comment
uses: actions/github-script@v7
with:
script: |
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('🌿 Interactive Playground and Documentation Preview')
);
if (botComment) {
const updatedComment = botComment.body + '\n\n---\n\n✅ Preview deployment has been cleaned up.';
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: updatedComment
});
}