Skip to content

Commit d72d261

Browse files
committed
Merge deploy-preview into javascript and update playground version info
1 parent bc63947 commit d72d261

File tree

4 files changed

+186
-226
lines changed

4 files changed

+186
-226
lines changed

.github/workflows/deploy-preview.yml

Lines changed: 0 additions & 193 deletions
This file was deleted.

.github/workflows/javascript.yml

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
branches:
66
- main
77
pull_request:
8-
types: [opened, synchronize, labeled]
8+
types: [opened, synchronize, labeled, closed]
99
pull_request_review:
1010
types: [submitted]
1111
release:
@@ -17,6 +17,7 @@ permissions:
1717

1818
jobs:
1919
main:
20+
if: github.event.action != 'closed'
2021
env:
2122
GH_TOKEN: ${{ github.token }}
2223

@@ -71,6 +72,7 @@ jobs:
7172
- name: Run `build` for all NX packages
7273
env:
7374
RELEASE_BUILD: ${{ github.event_name == 'release' && 'true' || 'false' }}
75+
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
7476
run: yarn build
7577

7678
- name: Run `test` for all NX packages
@@ -84,6 +86,15 @@ jobs:
8486
if-no-files-found: error
8587
retention-days: 1
8688

89+
- name: Upload docs artifact
90+
if: github.event_name == 'pull_request'
91+
uses: actions/upload-artifact@v4
92+
with:
93+
name: docs-preview
94+
path: docs/.vitepress/dist
95+
if-no-files-found: error
96+
retention-days: 1
97+
8798
# NX is not able to properly detect affected changes if things change in C source or the WASM build
8899
#
89100
# - name: Set NX SHAs
@@ -213,3 +224,128 @@ jobs:
213224
run: |
214225
cd javascript/packages/vscode
215226
npx ovsx publish -p $OVSX_PAT
227+
228+
deploy-preview:
229+
name: Deploy docs preview to Cloudflare Pages
230+
if: github.event_name == 'pull_request' && github.event.action != 'closed'
231+
needs: main
232+
runs-on: ubuntu-latest
233+
234+
permissions:
235+
contents: read
236+
deployments: write
237+
pull-requests: write
238+
239+
steps:
240+
- name: Download docs artifact
241+
uses: actions/download-artifact@v4
242+
with:
243+
name: docs-preview
244+
path: dist
245+
246+
- name: Deploy to Cloudflare Pages
247+
id: deploy
248+
uses: cloudflare/pages-action@v1
249+
with:
250+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
251+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
252+
projectName: herb-tools
253+
directory: dist
254+
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
255+
256+
- name: Add comment with preview URL
257+
uses: actions/github-script@v7
258+
with:
259+
script: |
260+
const previewUrl = '${{ steps.deploy.outputs.url }}';
261+
const comment = `### 🌿 Interactive Playground and Documentation Preview
262+
263+
A preview deployment has been built for this pull request. Try out the changes live in the interactive playground:
264+
265+
- **[Playground Preview](${previewUrl}/playground)**
266+
- **[Documentation Preview](${previewUrl})**
267+
- **[Website Preview](${previewUrl})**
268+
269+
---
270+
<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>`;
271+
272+
const { data: comments } = await github.rest.issues.listComments({
273+
owner: context.repo.owner,
274+
repo: context.repo.repo,
275+
issue_number: context.issue.number,
276+
});
277+
278+
const botComment = comments.find(comment =>
279+
comment.user.type === 'Bot' &&
280+
comment.body.includes('🌿 Interactive Playground and Documentation Preview')
281+
);
282+
283+
if (botComment) {
284+
await github.rest.issues.updateComment({
285+
owner: context.repo.owner,
286+
repo: context.repo.repo,
287+
comment_id: botComment.id,
288+
body: comment
289+
});
290+
} else {
291+
await github.rest.issues.createComment({
292+
owner: context.repo.owner,
293+
repo: context.repo.repo,
294+
issue_number: context.issue.number,
295+
body: comment
296+
});
297+
}
298+
299+
cleanup-preview:
300+
name: Cleanup docs preview deployment
301+
if: github.event_name == 'pull_request' && github.event.action == 'closed'
302+
runs-on: ubuntu-latest
303+
304+
permissions:
305+
pull-requests: write
306+
307+
steps:
308+
- name: Delete Cloudflare Pages deployment
309+
run: |
310+
DEPLOYMENTS=$(curl -s -X GET \
311+
"https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/herb-tools/deployments" \
312+
-H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \
313+
-H "Content-Type: application/json")
314+
315+
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
316+
DEPLOYMENT_ID=$(echo "$DEPLOYMENTS" | jq -r ".result[] | select(.deployment_trigger.metadata.branch == \"$BRANCH_NAME\") | .id" | head -n 1)
317+
318+
if [ -n "$DEPLOYMENT_ID" ]; then
319+
echo "Deleting deployment $DEPLOYMENT_ID for branch $BRANCH_NAME"
320+
curl -X DELETE \
321+
"https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects/herb-tools/deployments/$DEPLOYMENT_ID" \
322+
-H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \
323+
-H "Content-Type: application/json"
324+
else
325+
echo "No deployment found for branch $BRANCH_NAME"
326+
fi
327+
328+
- name: Update PR comment
329+
uses: actions/github-script@v7
330+
with:
331+
script: |
332+
const { data: comments } = await github.rest.issues.listComments({
333+
owner: context.repo.owner,
334+
repo: context.repo.repo,
335+
issue_number: context.issue.number,
336+
});
337+
338+
const botComment = comments.find(comment =>
339+
comment.user.type === 'Bot' &&
340+
comment.body.includes('🌿 Interactive Playground and Documentation Preview')
341+
);
342+
343+
if (botComment) {
344+
const updatedComment = botComment.body + '\n\n---\n\n✅ Preview deployment has been cleaned up.';
345+
await github.rest.issues.updateComment({
346+
owner: context.repo.owner,
347+
repo: context.repo.repo,
348+
comment_id: botComment.id,
349+
body: updatedComment
350+
});
351+
}

playground/src/controllers/playground_controller.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -778,16 +778,26 @@ export default class extends Controller {
778778
if (this.hasCommitHashTarget) {
779779
if (typeof __COMMIT_INFO__ !== 'undefined') {
780780
const commitInfo = __COMMIT_INFO__
781-
const githubUrl = `https://github.com/marcoroth/herb/commit/${commitInfo.hash}`
782781

783-
if (commitInfo.ahead > 0) {
784-
this.commitHashTarget.textContent = `${commitInfo.tag} (+${commitInfo.ahead} commits) ${commitInfo.hash}`
782+
if (commitInfo.prNumber) {
783+
const prUrl = `https://github.com/marcoroth/herb/pull/${commitInfo.prNumber}`
784+
const commitUrl = `https://github.com/marcoroth/herb/commit/${commitInfo.hash}`
785+
786+
this.commitHashTarget.textContent = `PR #${commitInfo.prNumber} @ ${commitInfo.hash}`
787+
this.commitHashTarget.href = prUrl
788+
this.commitHashTarget.title = `View PR #${commitInfo.prNumber} on GitHub (commit ${commitInfo.hash})`
785789
} else {
786-
this.commitHashTarget.textContent = `${commitInfo.tag} ${commitInfo.hash}`
787-
}
790+
const githubUrl = `https://github.com/marcoroth/herb/commit/${commitInfo.hash}`
791+
792+
if (commitInfo.ahead > 0) {
793+
this.commitHashTarget.textContent = `${commitInfo.tag} (+${commitInfo.ahead} commits) ${commitInfo.hash}`
794+
} else {
795+
this.commitHashTarget.textContent = `${commitInfo.tag} ${commitInfo.hash}`
796+
}
788797

789-
this.commitHashTarget.href = githubUrl
790-
this.commitHashTarget.title = `View commit ${commitInfo.hash} on GitHub`
798+
this.commitHashTarget.href = githubUrl
799+
this.commitHashTarget.title = `View commit ${commitInfo.hash} on GitHub`
800+
}
791801
} else {
792802
this.commitHashTarget.textContent = 'unknown'
793803
this.commitHashTarget.removeAttribute('href')

0 commit comments

Comments
 (0)