-
Notifications
You must be signed in to change notification settings - Fork 325
212 lines (190 loc) · 7.44 KB
/
pr-preview.yml
File metadata and controls
212 lines (190 loc) · 7.44 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
name: PR Preview
on:
pull_request:
types: [opened, reopened, synchronize, closed, ready_for_review]
paths:
- 'content/**'
- 'layouts/**'
- 'assets/**'
- 'data/**'
- 'api-docs/**'
- 'openapi/**'
permissions:
contents: write
pull-requests: write
concurrency:
group: pr-preview-${{ github.event.number }}
cancel-in-progress: true
jobs:
# Skip draft PRs entirely
check-draft:
runs-on: ubuntu-latest
outputs:
should-run: ${{ steps.check.outputs.should-run }}
steps:
- id: check
run: |
if [[ "${{ github.event.pull_request.draft }}" == "true" ]]; then
echo "should-run=false" >> $GITHUB_OUTPUT
echo "Skipping draft PR"
else
echo "should-run=true" >> $GITHUB_OUTPUT
fi
# Notify fork PRs that preview is not available
fork-notice:
needs: check-draft
if: |
needs.check-draft.outputs.should-run == 'true' &&
github.event.action != 'closed' &&
github.event.pull_request.head.repo.full_name != github.repository
runs-on: ubuntu-latest
steps:
- name: Post fork notice 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 marker = '<!-- pr-preview-fork-notice -->';
const existing = comments.find(c => c.body.includes(marker));
if (!existing) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `${marker}\n## 📝 PR Preview Not Available\n\nPR previews are not available for pull requests from forks due to GitHub Actions security restrictions.\n\nTo preview your changes locally, run:\n\`\`\`bash\nnpx hugo server\n\`\`\`\n\nOnce merged, your changes will be visible on the production site.`
});
}
# Build and deploy preview
preview:
needs: check-draft
# Skip fork PRs - GITHUB_TOKEN doesn't have write access to push to gh-pages
if: |
needs.check-draft.outputs.should-run == 'true' &&
github.event.action != 'closed' &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'yarn'
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Detect preview pages
id: detect
env:
PR_BODY: ${{ github.event.pull_request.body }}
BASE_REF: origin/${{ github.base_ref }}
run: node .github/scripts/detect-preview-pages.js
- name: Post pending comment (needs input)
if: steps.detect.outputs.needs-author-input == 'true'
uses: actions/github-script@v7
with:
script: |
const { upsertPreviewComment } = await import('${{ github.workspace }}/.github/scripts/preview-comment.js');
await upsertPreviewComment(github, context, {
status: 'pending',
needsInput: true,
prNumber: context.issue.number
});
- name: Skip if no pages to deploy
if: steps.detect.outputs.pages-to-deploy == '[]'
run: |
echo "No pages to deploy - skipping preview"
echo "Reason: ${{ steps.detect.outputs.skip-reason || steps.detect.outputs.change-summary }}"
- name: Build Hugo site
if: steps.detect.outputs.pages-to-deploy != '[]'
id: build
env:
# Set baseURL for GitHub Pages preview subdirectory
PREVIEW_BASE_URL: https://influxdata.github.io/docs-v2/pr-preview/pr-${{ github.event.number }}/
run: |
START_TIME=$(date +%s)
# Use pr-preview environment for path offset config
npx hugo --minify --baseURL "$PREVIEW_BASE_URL" -e pr-preview
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
echo "build-time=${DURATION}s" >> $GITHUB_OUTPUT
- name: Prepare preview files
if: steps.detect.outputs.pages-to-deploy != '[]'
run: |
node .github/scripts/prepare-preview-files.js \
'${{ steps.detect.outputs.pages-to-deploy }}' \
public \
preview-staging
- name: Deploy preview
if: steps.detect.outputs.pages-to-deploy != '[]'
uses: rossjrw/pr-preview-action@v1.4.8
with:
source-dir: ./preview-staging
preview-branch: gh-pages
umbrella-dir: pr-preview
action: deploy
- name: Post success comment
if: steps.detect.outputs.pages-to-deploy != '[]'
uses: actions/github-script@v7
with:
script: |
const { upsertPreviewComment } = await import('${{ github.workspace }}/.github/scripts/preview-comment.js');
const pages = JSON.parse('${{ steps.detect.outputs.pages-to-deploy }}');
const previewUrl = `https://influxdata.github.io/docs-v2/pr-preview/pr-${{ github.event.number }}/`;
await upsertPreviewComment(github, context, {
status: 'success',
previewUrl,
pages,
buildTime: '${{ steps.build.outputs.build-time }}',
prNumber: context.issue.number
});
- name: Post skipped comment
if: steps.detect.outputs.pages-to-deploy == '[]' && steps.detect.outputs.needs-author-input != 'true'
uses: actions/github-script@v7
with:
script: |
const { upsertPreviewComment } = await import('${{ github.workspace }}/.github/scripts/preview-comment.js');
await upsertPreviewComment(github, context, {
status: 'skipped',
skipReason: '${{ steps.detect.outputs.change-summary }}',
prNumber: context.issue.number
});
# Cleanup on PR close
cleanup:
if: github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages
- name: Remove preview directory
run: |
PREVIEW_DIR="pr-preview/pr-${{ github.event.number }}"
if [ -d "$PREVIEW_DIR" ]; then
rm -rf "$PREVIEW_DIR"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "Clean up preview for PR #${{ github.event.number }}" || echo "Nothing to commit"
git push
echo "Cleaned up $PREVIEW_DIR"
else
echo "No preview directory found for PR #${{ github.event.number }}"
fi
- name: Checkout scripts for comment deletion
uses: actions/checkout@v4
with:
path: scripts-checkout
- name: Delete preview comment
uses: actions/github-script@v7
with:
script: |
const { deletePreviewComment } = await import('${{ github.workspace }}/scripts-checkout/.github/scripts/preview-comment.js');
await deletePreviewComment(github, context);