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+ }
0 commit comments