-
Notifications
You must be signed in to change notification settings - Fork 7
355 lines (306 loc) Β· 14.3 KB
/
deploy.yml
File metadata and controls
355 lines (306 loc) Β· 14.3 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
344
345
346
347
348
349
350
351
352
353
354
355
name: Deploy to GitHub Pages
on:
push:
branches:
- main
- develop
- multi
pull_request:
types: [ opened, synchronize, reopened, closed ]
permissions:
contents: read
pages: write
id-token: write
pull-requests: write
statuses: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
# Build job
build:
runs-on: ubuntu-latest
if: ${{ !(github.event_name == 'pull_request' && github.event.action == 'closed') }}
outputs:
deployment-path: ${{ steps.determine-path.outputs.path }}
deployment-url: ${{ steps.determine-path.outputs.url }}
branch-name: ${{ steps.determine-path.outputs.branch }}
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build documentation
run: npm run render
- name: Determine deployment path and URL
id: determine-path
run: |
if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "path=." >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/DIIP" >> $GITHUB_OUTPUT
echo "branch=main" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/develop" ]]; then
echo "path=draft" >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/DIIP/draft" >> $GITHUB_OUTPUT
echo "branch=develop" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/multi" ]]; then
echo "path=draft" >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/DIIP/draft" >> $GITHUB_OUTPUT
echo "branch=multi" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
PR_NUMBER="${{ github.event.pull_request.number }}"
echo "path=pr/pr-${PR_NUMBER}" >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/DIIP/pr/pr-${PR_NUMBER}" >> $GITHUB_OUTPUT
echo "branch=pr-${PR_NUMBER}" >> $GITHUB_OUTPUT
fi
- name: Checkout existing gh-pages content
uses: actions/checkout@v4
with:
ref: gh-pages
path: existing-pages
continue-on-error: true
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Prepare deployment directory with simultaneous deployments
run: |
DEPLOY_PATH="${{ steps.determine-path.outputs.path }}"
# Create _site directory for deployment
mkdir -p _site
# Copy existing gh-pages content if it exists
if [ -d "existing-pages" ]; then
# Copy everything except .git
rsync -av --exclude='.git' existing-pages/ _site/ 2>/dev/null || true
echo "Preserved existing deployments"
else
echo "No existing deployments found"
fi
# Update the specific deployment path
if [[ "$DEPLOY_PATH" == "." ]]; then
# For main branch, update root files but preserve subdirectories
find _site -maxdepth 1 -name "*.html" -delete 2>/dev/null || true
find _site -maxdepth 1 -name "*.png" -delete 2>/dev/null || true
find _site -maxdepth 1 -name "*.svg" -delete 2>/dev/null || true
find _site -maxdepth 1 -name "*.js" -delete 2>/dev/null || true
rm -rf _site/assets _site/custom-assets 2>/dev/null || true
# Copy new files to root
cp ./*.html _site/ 2>/dev/null || true
cp ./*.png _site/ 2>/dev/null || true
cp ./*.svg _site/ 2>/dev/null || true
cp ./*.js _site/ 2>/dev/null || true
cp -r ./assets _site/ 2>/dev/null || true
cp -r ./custom-assets _site/ 2>/dev/null || true
# Also create/update latest directory
rm -rf _site/latest
mkdir -p _site/latest
cp ./*.html _site/latest/ 2>/dev/null || true
cp ./*.png _site/latest/ 2>/dev/null || true
cp ./*.svg _site/latest/ 2>/dev/null || true
cp ./*.js _site/latest/ 2>/dev/null || true
cp -r ./assets _site/latest/ 2>/dev/null || true
cp -r ./custom-assets _site/latest/ 2>/dev/null || true
echo "Updated main deployment (root + /latest)"
else
# For develop/PR branches, update specific subdirectory
rm -rf "_site/$DEPLOY_PATH"
mkdir -p "_site/$DEPLOY_PATH"
cp ./*.html "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp ./*.png "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp ./*.svg "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp ./*.js "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp -r ./assets "_site/$DEPLOY_PATH/" 2>/dev/null || true
cp -r ./custom-assets "_site/$DEPLOY_PATH/" 2>/dev/null || true
echo "Updated deployment: $DEPLOY_PATH"
fi
# Create a deployment info file
echo "# GitHub Pages Deployment Info" > _site/deployment-info.md
echo "Last updated: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> _site/deployment-info.md
echo "Deployed from: ${{ steps.determine-path.outputs.branch }}" >> _site/deployment-info.md
echo "Deployment path: $DEPLOY_PATH" >> _site/deployment-info.md
echo "" >> _site/deployment-info.md
echo "## Available deployments:" >> _site/deployment-info.md
find _site -type f -name "index.html" | sed 's|_site/||' | sed 's|/index.html||' | sed 's|^$|/ (main)|' | sort >> _site/deployment-info.md
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
# Single deployment job that handles all cases
deploy:
environment:
name: github-pages
url: ${{ needs.build.outputs.deployment-url }}
runs-on: ubuntu-latest
needs: build
if: ${{ github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed') }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
- name: Comment on PR with preview link
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const deployUrl = '${{ needs.build.outputs.deployment-url }}';
const prNumber = context.payload.pull_request.number;
const message = `π **Preview deployment successful!**
π **Preview URL:** ${deployUrl}
π **PR:** #${prNumber}
π **Status:** Ready for review
This preview will be updated automatically when you push new commits to this PR.
> The preview will be removed when the PR is closed or merged.`;
// Check if we already commented
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existingComment = comments.data.find(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('Preview deployment successful!')
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: message
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: message
});
}
- name: Create commit status for develop branch
if: github.event_name == 'push' && (github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/multi')
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
try {
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.sha,
state: 'success',
description: 'Draft deployed successfully',
target_url: '${{ needs.build.outputs.deployment-url }}',
context: 'github-pages/draft'
});
console.log('Commit status created successfully');
} catch (error) {
console.log('Could not create commit status:', error.message);
console.log('This is not critical - deployment was successful');
}
- name: Check GitHub Pages status and provide setup instructions
uses: actions/github-script@v7
with:
script: |
const deployUrl = '${{ needs.build.outputs.deployment-url }}';
const path = '${{ needs.build.outputs.deployment-path }}';
try {
// Try to get pages info to see if it's enabled
const pages = await github.rest.repos.getPages({
owner: context.repo.owner,
repo: context.repo.repo,
});
console.log(`GitHub Pages is enabled and configured`);
console.log(`Site URL: ${pages.data.html_url}`);
console.log(`Deployment URL: ${deployUrl}`);
} catch (error) {
if (error.status === 404) {
console.log('GitHub Pages is not enabled for this repository.');
console.log('');
console.log('To enable GitHub Pages:');
console.log(' 1. Go to repository Settings > Pages');
console.log(' 2. Under "Source", select "Deploy from a branch"');
console.log(' 3. Select branch "gh-pages" and folder "/ (root)"');
console.log(' 4. Click "Save"');
console.log('');
console.log(`After setup, your site will be available at: ${deployUrl}`);
// Create a summary for the workflow
await core.summary
.addHeading('GitHub Pages Setup Required')
.addRaw('GitHub Pages is not enabled for this repository. ')
.addRaw('The content has been deployed to the gh-pages branch, but you need to enable Pages manually.')
.addHeading('Setup Instructions')
.addList([
'Go to repository **Settings > Pages**',
'Under **Source**, select **Deploy from a branch**',
'Select branch **gh-pages** and folder **/ (root)**',
'Click **Save**'
])
.addRaw(`After setup, your site will be available at: **${deployUrl}**`)
.write();
} else {
console.log('Error checking pages status:', error.message);
}
}
# Cleanup job for closed PRs
cleanup-pr:
runs-on: ubuntu-latest
needs: build
if: ${{ github.event_name == 'pull_request' && github.event.action == 'closed' }}
steps:
- name: Checkout existing gh-pages content
uses: actions/checkout@v4
with:
ref: gh-pages
path: existing-pages
continue-on-error: true
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Remove PR directory and redeploy
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
PR_DIR="pr/pr-${PR_NUMBER}"
# Create _site directory for deployment
mkdir -p _site
# Copy existing gh-pages content if it exists
if [ -d "existing-pages" ]; then
rsync -av --exclude='.git' existing-pages/ _site/
echo "Copied existing deployments"
# Remove the specific PR directory
if [ -d "_site/$PR_DIR" ]; then
rm -rf "_site/$PR_DIR"
echo "Removed PR directory: $PR_DIR"
# Update deployment info
echo "# GitHub Pages Deployment Info" > _site/deployment-info.md
echo "Last updated: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> _site/deployment-info.md
echo "Action: Cleaned up PR #${PR_NUMBER}" >> _site/deployment-info.md
echo "" >> _site/deployment-info.md
echo "## Available deployments:" >> _site/deployment-info.md
find _site -type f -name "index.html" | sed 's|_site/||' | sed 's|/index.html||' | sed 's|^$|/ (main)|' | sort >> _site/deployment-info.md
else
echo "PR directory $PR_DIR does not exist, nothing to clean up"
exit 0
fi
else
echo "No existing deployments found, nothing to clean up"
exit 0
fi
- name: Upload artifact for cleanup
uses: actions/upload-pages-artifact@v3
- name: Deploy cleaned up pages
id: cleanup-deployment
uses: actions/deploy-pages@v4
- name: Comment on PR about cleanup
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `π§Ή **Preview cleaned up**
The preview deployment for this PR has been removed since the PR was closed.
Thank you for your contribution! π`
});