Skip to content

Commit d1adf2a

Browse files
committed
add deploy preview
1 parent f303301 commit d1adf2a

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
name: Deploy Preview
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
pr_number:
7+
required: true
8+
type: string
9+
commit_sha:
10+
required: true
11+
type: string
12+
action:
13+
required: true
14+
type: string
15+
description: "create or delete preview"
16+
secrets:
17+
GITHUB_TOKEN:
18+
required: true
19+
20+
jobs:
21+
deploy-preview:
22+
name: Deploy Preview
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@v4
27+
with:
28+
token: ${{ secrets.GITHUB_TOKEN }}
29+
ref: gh-pages
30+
path: gh-pages
31+
32+
- name: Setup Node.js
33+
uses: actions/setup-node@v4
34+
with:
35+
node-version: 20
36+
cache: npm
37+
38+
- name: Install dependencies
39+
run: npm install --frozen-lockfile
40+
41+
- name: Restore docs build output
42+
if: inputs.action == 'create'
43+
uses: actions/cache@v4
44+
with:
45+
path: docs/public
46+
key: docs-build-${{ inputs.commit_sha }}
47+
restore-keys: |
48+
docs-build-
49+
50+
- name: Create preview directory
51+
if: inputs.action == 'create'
52+
run: |
53+
mkdir -p gh-pages/preview/PR${{ inputs.pr_number }}
54+
cp -r docs/public/* gh-pages/preview/PR${{ inputs.pr_number }}/
55+
56+
- name: Remove preview directory
57+
if: inputs.action == 'delete'
58+
run: |
59+
rm -rf gh-pages/preview/PR${{ inputs.pr_number }}
60+
61+
- name: Commit and push changes
62+
run: |
63+
cd gh-pages
64+
git config user.name "github-actions[bot]"
65+
git config user.email "github-actions[bot]@users.noreply.github.com"
66+
67+
if [ "${{ inputs.action }}" = "create" ]; then
68+
git add preview/PR${{ inputs.pr_number }}
69+
git commit -m "Add preview for PR #${{ inputs.pr_number }}"
70+
else
71+
git add -A
72+
git commit -m "Remove preview for PR #${{ inputs.pr_number }}"
73+
fi
74+
75+
git push origin gh-pages
76+
77+
- name: Comment on PR
78+
if: inputs.action == 'create'
79+
uses: actions/github-script@v7
80+
with:
81+
script: |
82+
const previewUrl = `https://${{ github.repository_owner }}.github.io/execution-apis/preview/PR${{ inputs.pr_number }}/`;
83+
github.rest.issues.createComment({
84+
issue_number: ${{ inputs.pr_number }},
85+
owner: '${{ github.repository_owner }}',
86+
repo: '${{ github.event.repository.name }}',
87+
body: `🚀 **Deploy Preview Available!**
88+
89+
Your changes are now available for preview at: **${previewUrl}**
90+
91+
This preview will be automatically removed when the PR is merged.`
92+
});
93+
94+
- name: Remove preview comment
95+
if: inputs.action == 'delete'
96+
uses: actions/github-script@v7
97+
with:
98+
script: |
99+
const { data: comments } = await github.rest.issues.listComments({
100+
issue_number: ${{ inputs.pr_number }},
101+
owner: '${{ github.repository_owner }}',
102+
repo: '${{ github.event.repository.name }}'
103+
});
104+
105+
const previewComment = comments.find(comment =>
106+
comment.body.includes('Deploy Preview Available!') &&
107+
comment.user.login === 'github-actions[bot]'
108+
);
109+
110+
if (previewComment) {
111+
await github.rest.issues.deleteComment({
112+
comment_id: previewComment.id,
113+
owner: '${{ github.repository_owner }}',
114+
repo: '${{ github.event.repository.name }}'
115+
});
116+
}

.github/workflows/test-deploy.yaml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ on:
44
pull_request:
55
branches:
66
- main
7-
# Review gh actions docs if you want to further define triggers, paths, etc
8-
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on
97
workflow_dispatch:
108

119
jobs:
@@ -29,4 +27,23 @@ jobs:
2927
- name: Test build website
3028
run: |
3129
npm run build
32-
npm run build:docs
30+
npm run build:docs
31+
- name: Cache docs build output
32+
uses: actions/cache@v4
33+
with:
34+
path: docs/public
35+
key: docs-build-${{ github.sha }}
36+
restore-keys: |
37+
docs-build-
38+
39+
deploy-preview:
40+
name: Deploy Preview
41+
needs: test-deploy
42+
if: github.event_name == 'pull_request'
43+
uses: ./.github/workflows/deploy-preview.yaml
44+
with:
45+
pr_number: ${{ github.event.number }}
46+
commit_sha: ${{ github.sha }}
47+
action: ${{ github.event.action == 'closed' && 'delete' || 'create' }}
48+
secrets:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)