forked from gardener/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
343 lines (284 loc) · 12.5 KB
/
pr-preview.yaml
File metadata and controls
343 lines (284 loc) · 12.5 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
name: PR Preview
on:
pull_request_target:
types:
- labeled
- closed
permissions:
pull-requests: write
concurrency: preview-${{ github.ref }}
jobs:
deploy-preview:
if: ${{ github.event.action == 'labeled' && github.event.label.name == vars.DEFAULT_LABEL_OK_TO_TEST && vars.DEFAULT_LABEL_OK_TO_TEST != '' }}
runs-on: ubuntu-latest
steps:
- uses: actions/create-github-app-token@v2
id: app-token
with:
# required
app-id: ${{ vars.GARDENER_GITHUB_ACTIONS_APP_ID }}
private-key: ${{ secrets.GARDENER_GITHUB_ACTIONS_PRIVATE_KEY }}
owner: gardener
repositories: documentation-demo
- name: Checkout
uses: gardener/cc-utils/.github/actions/trusted-checkout@master
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 24
cache: npm
- name: Docforge
run: |
wget https://github.com/gardener/docforge/releases/download/v0.55.0/docforge-linux-amd64
mv docforge-linux-amd64 docforge
chmod +x docforge
DOCFORGE_CONFIG=.docforge/config.yaml GITHUB_OAUTH_TOKEN=${{ steps.app-token.outputs.token }} ./docforge
- name: Install dependencies
run: npm ci
- name: Postprocessing Content
run: |
make post-process
- name: Set Base Path
id: set-base-path
run: |
echo "VITE_PUBLIC_BASE_PATH=/pr-preview/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_ENV
echo "Base path set to: /pr-preview/pr-${{ github.event.pull_request.number }}/"
- name: Build with VitePress
run: |
echo "Building with VITE_PUBLIC_BASE_PATH: '$VITE_PUBLIC_BASE_PATH'"
npm run docs:build
- uses: actions/checkout@v6
with:
token: ${{ steps.app-token.outputs.token }}
repository: gardener/documentation-demo
path: other-repo
- name: setup-git-identity
uses: gardener/cc-utils/.github/actions/setup-git-identity/@master
with:
user_name: "GitHub Actions Bot"
user_email: "actions@github.com"
- name: commit-and-push
shell: bash
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
echo "=== Starting commit-and-push ==="
echo "PR_NUMBER: ${PR_NUMBER:-<not set>}"
if [ -z "${PR_NUMBER:-}" ]; then
echo "Error: PR_NUMBER environment variable is required"
exit 1
fi
echo "Changing directory to: other-repo"
cd other-repo
echo "Current directory: $(pwd)"
# Set git identity for this repository (not inherited from parent repo)
echo "Setting git identity..."
git config user.name "GitHub Actions Bot"
git config user.email "actions@github.com"
echo "=== Git Configuration in other-repo ==="
echo "Git user.name: $(git config user.name || echo '<not set>')"
echo "Git user.email: $(git config user.email || echo '<not set>')"
echo "Git remote origin URL: $(git config --get remote.origin.url || echo '<not set>')"
echo "========================================"
# Switch to gh-pages branch
echo "Fetching gh-pages branch from origin"
git fetch origin gh-pages
echo "Checking out gh-pages branch"
git checkout gh-pages
echo "Current branch: $(git branch --show-current)"
# Remove existing preview directory if it exists
PREVIEW_DIR="pr-preview/pr-${PR_NUMBER}"
echo "Removing existing preview directory: ${PREVIEW_DIR}"
rm -rf "${PREVIEW_DIR}"
# Create pr-preview directory structure
echo "Creating preview directory: ${PREVIEW_DIR}"
mkdir -p "${PREVIEW_DIR}"
# Copy built files
SOURCE_DIR="../.vitepress/dist"
echo "Copying built files from: ${SOURCE_DIR}"
echo "Destination: ${PREVIEW_DIR}/"
cp -r ${SOURCE_DIR}/* "${PREVIEW_DIR}/"
echo "Files copied. Directory contents:"
ls -la "${PREVIEW_DIR}/" | head -20
# Stage changes
echo "Staging changes: git add ${PREVIEW_DIR}"
git add "${PREVIEW_DIR}"
# Check if there are changes to commit
echo "Checking for staged changes..."
if git diff --staged --quiet; then
echo "No changes detected in preview files. Skipping commit and push."
exit 0
fi
echo "Changes detected. Git status:"
git status --short
# Commit and push
COMMIT_MSG="Deploy PR #${PR_NUMBER} preview"
echo "Committing changes with message: ${COMMIT_MSG}"
git commit -m "${COMMIT_MSG}"
REMOTE_BRANCH="origin gh-pages"
echo "Pushing to: ${REMOTE_BRANCH}"
git push origin gh-pages
echo "Preview deployed successfully"
echo "Preview URL should be available at: https://<username>.github.io/<repo>/pr-preview/pr-${PR_NUMBER}/"
- name: Comment on PR
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
const previewUrl = `https://documentation-demo.gardener.cloud/pr-preview/pr-${prNumber}/`;
const commentBody = `## 🚀 Preview Deployed
Your preview is being built and will be available at:
${previewUrl}
**Note:** The preview will only be available after the GitHub Pages deployment completes in the demo repository.
To view the deployment status, visit: https://github.com/gardener/documentation-demo/actions`;
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Preview Deployed')
);
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: prNumber,
body: commentBody
});
}
cleanup-preview:
if: ${{ github.event.action == 'closed' }}
runs-on: ubuntu-latest
steps:
- uses: actions/create-github-app-token@v2
id: app-token
with:
# required
app-id: ${{ vars.GARDENER_GITHUB_ACTIONS_APP_ID }}
private-key: ${{ secrets.GARDENER_GITHUB_ACTIONS_PRIVATE_KEY }}
owner: gardener
repositories: documentation-demo
- name: Checkout main repository
uses: gardener/cc-utils/.github/actions/trusted-checkout@master
- uses: actions/checkout@v6
with:
token: ${{ steps.app-token.outputs.token }}
repository: gardener/documentation-demo
path: other-repo
- name: setup-git-identity
uses: gardener/cc-utils/.github/actions/setup-git-identity/@master
with:
user_name: "GitHub Actions Bot"
user_email: "actions@github.com"
- name: Remove preview and push
shell: bash
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
echo "=== Starting remove-preview ==="
echo "PR_NUMBER: ${PR_NUMBER:-<not set>}"
if [ -z "${PR_NUMBER:-}" ]; then
echo "Error: PR_NUMBER environment variable is required"
exit 1
fi
echo "Changing directory to: other-repo"
cd other-repo
echo "Current directory: $(pwd)"
# Set git identity for this repository (not inherited from parent repo)
echo "Setting git identity..."
git config user.name "GitHub Actions Bot"
git config user.email "actions@github.com"
echo "=== Git Configuration in other-repo ==="
echo "Git user.name: $(git config user.name || echo '<not set>')"
echo "Git user.email: $(git config user.email || echo '<not set>')"
echo "Git remote origin URL: $(git config --get remote.origin.url || echo '<not set>')"
echo "========================================"
# Switch to gh-pages branch
echo "Fetching gh-pages branch from origin"
git fetch origin gh-pages
echo "Checking out gh-pages branch"
git checkout gh-pages
echo "Current branch: $(git branch --show-current)"
# Remove preview directory if it exists
PREVIEW_DIR="pr-preview/pr-${PR_NUMBER}"
echo "Checking for preview directory: ${PREVIEW_DIR}"
if [ -d "${PREVIEW_DIR}" ]; then
echo "Preview directory exists. Removing: ${PREVIEW_DIR}"
rm -rf "${PREVIEW_DIR}"
echo "Staging removal: git add -A ${PREVIEW_DIR}"
git add -A "${PREVIEW_DIR}"
echo "Checking for staged changes..."
if git diff --staged --quiet; then
echo "No changes detected after removal. Nothing to commit."
exit 0
fi
echo "Changes detected. Git status:"
git status --short
COMMIT_MSG="Remove PR #${PR_NUMBER} preview"
echo "Committing changes with message: ${COMMIT_MSG}"
git commit -m "${COMMIT_MSG}"
REMOTE_BRANCH="origin gh-pages"
echo "Pushing to: ${REMOTE_BRANCH}"
git push origin gh-pages
echo "Preview removed successfully"
else
echo "Preview directory does not exist: ${PREVIEW_DIR}"
echo "Nothing to clean up"
fi
- name: Update PR comment
uses: actions/github-script@v7
with:
script: |
console.log('=== Starting update-pr-comment ===');
const prNumber = context.payload.pull_request.number;
console.log(`PR Number: ${prNumber}`);
console.log(`Repository: ${context.repo.owner}/${context.repo.repo}`);
console.log(`Event: ${context.eventName}`);
const commentBody = `## 🧹 Preview Removed
The preview for this PR is no longer available since the PR has been closed.`;
console.log('Comment body to post:');
console.log(commentBody);
// Find existing comment
console.log(`Fetching comments for PR #${prNumber}...`);
console.log(`API call: github.rest.issues.listComments({ owner: "${context.repo.owner}", repo: "${context.repo.repo}", issue_number: ${prNumber} })`);
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber
});
console.log(`Found ${comments.length} total comments on PR #${prNumber}`);
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Preview Deployed')
);
if (botComment) {
console.log(`Found existing preview comment (ID: ${botComment.id})`);
console.log(`Comment author: ${botComment.user.login} (${botComment.user.type})`);
console.log(`API call: github.rest.issues.updateComment({ owner: "${context.repo.owner}", repo: "${context.repo.repo}", comment_id: ${botComment.id}, body: "..." })`);
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: commentBody
});
console.log(`✅ Successfully updated comment ID: ${botComment.id}`);
} else {
console.log('⚠️ No existing preview comment found to update');
console.log('Searched for comments from Bot users containing "Preview Deployed"');
}
console.log('=== update-pr-comment completed ===');