-
Notifications
You must be signed in to change notification settings - Fork 0
141 lines (120 loc) · 4.19 KB
/
pr-preview.yml
File metadata and controls
141 lines (120 loc) · 4.19 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
name: Deploy PR Preview
on:
pull_request:
types:
- opened
- reopened
- synchronize
- closed
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: write
pages: write
id-token: write
pull-requests: write
# Allow only one concurrent deployment per PR
concurrency:
group: pr-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
deploy-preview:
runs-on: ubuntu-latest
# Don't run on closed PRs
if: github.event.action != 'closed'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '22.9.0'
- name: Setup Yarn Corepack
run: corepack enable
- name: Install dependencies
run: yarn install
- name: Build Storybook
run: yarn build-storybook
# Verify the build output
- name: Verify build output
run: |
echo "Checking build output directory..."
ls -la apps/docs/storybook-static
echo "Checking for index.html..."
if [ -f apps/docs/storybook-static/index.html ]; then
echo "index.html exists"
else
echo "index.html does not exist"
exit 1
fi
# Deploy to GitHub Pages in a PR-specific directory
- name: Deploy PR Preview
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: apps/docs/storybook-static
target-folder: pr-preview/pr-${{ github.event.pull_request.number }}
clean: true
clean-exclude: |
pr-preview/pr-*
# Add a comment to the PR with the preview URL
- name: Comment PR
uses: actions/github-script@v6
with:
script: |
const previewUrl = `https://lambda-curry.github.io/forms/pr-preview/pr-${context.issue.number}/`;
const commentBody = `📝 **Storybook Preview**: [View Storybook](${previewUrl})
This preview will be updated automatically when you push new changes to this PR.`;
// Get existing comments
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
// Check if we already have a comment
const botComment = comments.data.find(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('Storybook Preview')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: commentBody
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: commentBody
});
}
# Clean up when PR is closed
cleanup:
runs-on: ubuntu-latest
if: github.event.action == 'closed'
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: gh-pages
- name: Delete PR Preview
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
PR_PREVIEW_PATH="pr-preview/pr-$PR_NUMBER"
if [ -d "$PR_PREVIEW_PATH" ]; then
echo "Removing PR preview at $PR_PREVIEW_PATH"
rm -rf "$PR_PREVIEW_PATH"
# Commit and push the changes
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git add -A
git commit -m "Remove PR preview for PR #$PR_NUMBER" || echo "No changes to commit"
git push
else
echo "PR preview directory not found"
fi