-
Notifications
You must be signed in to change notification settings - Fork 0
484 lines (403 loc) · 19.3 KB
/
ci.yml
File metadata and controls
484 lines (403 loc) · 19.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
name: CI
on:
push:
branches: [main, beta, alpha]
pull_request:
branches: [main]
# Template CI: This workflow automatically detects if the project is an
# unconfigured template and applies default values for testing.
# After running 'npm run setup', this CI will work with your configured values.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: write
pull-requests: write
pages: write
id-token: write
jobs:
lint-and-format:
name: Lint & Format
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Setup template for CI (if needed)
run: |
# Check if this is an unconfigured template
if grep -q "{{PROJECT_NAME}}" package.json; then
echo "⚠️ Detected unconfigured template, applying CI defaults..."
# Replace placeholders with CI-safe defaults
sed -i 's/{{PROJECT_NAME}}/nextjs-template-test/g' package.json
sed -i 's/{{PROJECT_DESCRIPTION}}/Next.js Template Test Build/g' package.json
sed -i 's/{{REPOSITORY_URL}}/https:\/\/github.com\/ExaDev\/NextJS-Template/g' package.json
sed -i 's/{{AUTHOR_NAME}}/ExaDev/g' package.json
sed -i 's/{{AUTHOR_EMAIL}}/dev@exadev.com/g' package.json
sed -i 's/{{LICENSE}}/MIT/g' package.json
# Setup React component placeholders (note the spaces in templates)
sed -i 's/{{ APP_NAME }}/Next.js Template/g' src/app/page.tsx
sed -i 's/{{ APP_DESCRIPTION }}/A production-ready Next.js template/g' src/app/page.tsx
sed -i 's/{{ FEATURE_1_TITLE }}/Modern Development/g' src/app/page.tsx
sed -i 's/{{ FEATURE_1_DESCRIPTION }}/Built with the latest Next.js and React/g' src/app/page.tsx
sed -i 's/{{ FEATURE_2_TITLE }}/Type Safety/g' src/app/page.tsx
sed -i 's/{{ FEATURE_2_DESCRIPTION }}/Full TypeScript support/g' src/app/page.tsx
sed -i 's/{{ FEATURE_3_TITLE }}/Testing Ready/g' src/app/page.tsx
sed -i 's/{{ FEATURE_3_DESCRIPTION }}/Comprehensive testing setup/g' src/app/page.tsx
sed -i 's/{{ FEATURE_4_TITLE }}/CI\/CD Pipeline/g' src/app/page.tsx
sed -i 's/{{ FEATURE_4_DESCRIPTION }}/Automated testing and deployment/g' src/app/page.tsx
sed -i 's/{{ CTA_TITLE }}/Get Started/g' src/app/page.tsx
sed -i 's/{{ CTA_DESCRIPTION }}/Start building your next project/g' src/app/page.tsx
sed -i 's/{{ CTA_BUTTON_TEXT }}/Start Building/g' src/app/page.tsx
# Format the code after template replacement to fix any long lines
npm run lint:fix || true
echo "✓ Template configured for CI testing"
else
echo "✓ Project is already configured"
fi
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run Biome check
run: npm run lint
- name: Check TypeScript
run: npm run type-check
test:
name: Test & Coverage
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Setup template for CI (if needed)
run: |
# Check if this is an unconfigured template
if grep -q "{{PROJECT_NAME}}" package.json; then
echo "⚠️ Detected unconfigured template, applying CI defaults..."
# Replace placeholders with CI-safe defaults
sed -i 's/{{PROJECT_NAME}}/nextjs-template-test/g' package.json
sed -i 's/{{PROJECT_DESCRIPTION}}/Next.js Template Test Build/g' package.json
sed -i 's/{{REPOSITORY_URL}}/https:\/\/github.com\/ExaDev\/NextJS-Template/g' package.json
sed -i 's/{{AUTHOR_NAME}}/ExaDev/g' package.json
sed -i 's/{{AUTHOR_EMAIL}}/dev@exadev.com/g' package.json
sed -i 's/{{LICENSE}}/MIT/g' package.json
# Setup React component placeholders (note the spaces in templates)
sed -i 's/{{ APP_NAME }}/Next.js Template/g' src/app/page.tsx
sed -i 's/{{ APP_DESCRIPTION }}/A production-ready Next.js template/g' src/app/page.tsx
sed -i 's/{{ FEATURE_1_TITLE }}/Modern Development/g' src/app/page.tsx
sed -i 's/{{ FEATURE_1_DESCRIPTION }}/Built with the latest Next.js and React/g' src/app/page.tsx
sed -i 's/{{ FEATURE_2_TITLE }}/Type Safety/g' src/app/page.tsx
sed -i 's/{{ FEATURE_2_DESCRIPTION }}/Full TypeScript support/g' src/app/page.tsx
sed -i 's/{{ FEATURE_3_TITLE }}/Testing Ready/g' src/app/page.tsx
sed -i 's/{{ FEATURE_3_DESCRIPTION }}/Comprehensive testing setup/g' src/app/page.tsx
sed -i 's/{{ FEATURE_4_TITLE }}/CI\/CD Pipeline/g' src/app/page.tsx
sed -i 's/{{ FEATURE_4_DESCRIPTION }}/Automated testing and deployment/g' src/app/page.tsx
sed -i 's/{{ CTA_TITLE }}/Get Started/g' src/app/page.tsx
sed -i 's/{{ CTA_DESCRIPTION }}/Start building your next project/g' src/app/page.tsx
sed -i 's/{{ CTA_BUTTON_TEXT }}/Start Building/g' src/app/page.tsx
# Format the code after template replacement to fix any long lines
npm run lint:fix || true
echo "✓ Template configured for CI testing"
else
echo "✓ Project is already configured"
fi
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests with coverage
run: npm run coverage
- name: Generate Coverage Summary
run: |
echo "## 📊 Coverage Report" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Parse coverage summary and create detailed report
if [ -f "coverage/coverage-summary.json" ]; then
cat coverage/coverage-summary.json | jq -r '
"### Overall Coverage:\n",
"| Metric | Coverage | Threshold | Status |",
"| --- | --- | --- | --- |",
"| **Statements** | " + (.total.statements.pct | tostring) + "% (" + (.total.statements.covered | tostring) + "/" + (.total.statements.total | tostring) + ") | 80% | " + (if .total.statements.pct >= 80 then "✅ Pass" else "❌ Fail" end) + " |",
"| **Branches** | " + (.total.branches.pct | tostring) + "% (" + (.total.branches.covered | tostring) + "/" + (.total.branches.total | tostring) + ") | 70% | " + (if .total.branches.pct >= 70 then "✅ Pass" else "❌ Fail" end) + " |",
"| **Functions** | " + (.total.functions.pct | tostring) + "% (" + (.total.functions.covered | tostring) + "/" + (.total.functions.total | tostring) + ") | 80% | " + (if .total.functions.pct >= 80 then "✅ Pass" else "❌ Fail" end) + " |",
"| **Lines** | " + (.total.lines.pct | tostring) + "% (" + (.total.lines.covered | tostring) + "/" + (.total.lines.total | tostring) + ") | 80% | " + (if .total.lines.pct >= 80 then "✅ Pass" else "❌ Fail" end) + " |"
' >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "📈 [View detailed coverage report in artifacts](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})" >> $GITHUB_STEP_SUMMARY
else
echo "⚠️ Coverage summary file not found" >> $GITHUB_STEP_SUMMARY
fi
- name: Upload coverage to GitHub
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage/
retention-days: 30
- name: Generate Coverage Report Summary
run: |
# Extract coverage percentage for reporting
COVERAGE=$(cat coverage/coverage-summary.json | jq -r '.total.statements.pct')
echo "📊 Coverage Summary: ${COVERAGE}%"
echo "coverage-percentage=${COVERAGE}" >> $GITHUB_OUTPUT
- name: Validate Coverage Thresholds
run: |
echo "Checking coverage thresholds against Vitest configuration..."
# Check if coverage summary exists
if [ ! -f "coverage/coverage-summary.json" ]; then
echo "❌ Coverage summary not found"
exit 1
fi
# Parse coverage and check thresholds using jq
STATEMENTS=$(cat coverage/coverage-summary.json | jq '.total.statements.pct')
BRANCHES=$(cat coverage/coverage-summary.json | jq '.total.branches.pct')
FUNCTIONS=$(cat coverage/coverage-summary.json | jq '.total.functions.pct')
LINES=$(cat coverage/coverage-summary.json | jq '.total.lines.pct')
echo "Current coverage:"
echo " Statements: ${STATEMENTS}% (threshold: 80%)"
echo " Branches: ${BRANCHES}% (threshold: 70%)"
echo " Functions: ${FUNCTIONS}% (threshold: 80%)"
echo " Lines: ${LINES}% (threshold: 80%)"
FAILED=false
if (( $(echo "$STATEMENTS < 80" | bc -l) )); then
echo "❌ Statements coverage below threshold: ${STATEMENTS}% < 80%"
FAILED=true
fi
if (( $(echo "$BRANCHES < 70" | bc -l) )); then
echo "❌ Branches coverage below threshold: ${BRANCHES}% < 70%"
FAILED=true
fi
if (( $(echo "$FUNCTIONS < 80" | bc -l) )); then
echo "❌ Functions coverage below threshold: ${FUNCTIONS}% < 80%"
FAILED=true
fi
if (( $(echo "$LINES < 80" | bc -l) )); then
echo "❌ Lines coverage below threshold: ${LINES}% < 80%"
FAILED=true
fi
if [ "$FAILED" = true ]; then
echo "❌ Coverage thresholds not met"
exit 1
else
echo "✅ All coverage thresholds met"
fi
security-audit:
name: Security Audit
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup template for CI (if needed)
run: |
# Check if this is an unconfigured template
if grep -q "{{PROJECT_NAME}}" package.json; then
echo "⚠️ Detected unconfigured template, applying CI defaults..."
# Replace placeholders with CI-safe defaults
sed -i 's/{{PROJECT_NAME}}/nextjs-template-test/g' package.json
sed -i 's/{{PROJECT_DESCRIPTION}}/Next.js Template Test Build/g' package.json
sed -i 's/{{REPOSITORY_URL}}/https:\/\/github.com\/ExaDev\/NextJS-Template/g' package.json
sed -i 's/{{AUTHOR_NAME}}/ExaDev/g' package.json
sed -i 's/{{AUTHOR_EMAIL}}/dev@exadev.com/g' package.json
sed -i 's/{{LICENSE}}/MIT/g' package.json
# Format the code after template replacement to fix any long lines
npm run lint:fix || true
echo "✓ Template configured for CI testing"
else
echo "✓ Project is already configured"
fi
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run npm audit
run: npm audit --audit-level=moderate
- name: Run npm audit for production dependencies only
run: npm audit --omit=dev --audit-level=high
- name: Check for known security issues
run: |
echo "Checking for high/critical vulnerabilities in production dependencies..."
if npm audit --omit=dev --audit-level=high; then
echo "✓ No high/critical vulnerabilities found in production dependencies"
else
echo "✗ High/critical vulnerabilities found in production dependencies"
exit 1
fi
build:
name: Build & Deploy Test
runs-on: ubuntu-latest
needs: [lint-and-format, test, security-audit]
steps:
- name: Checkout
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: Setup template for CI (if needed)
run: |
# Check if this is an unconfigured template
if grep -q "{{PROJECT_NAME}}" package.json; then
echo "⚠️ Detected unconfigured template, applying CI defaults..."
# Replace placeholders with CI-safe defaults
sed -i 's/{{PROJECT_NAME}}/nextjs-template-test/g' package.json
sed -i 's/{{PROJECT_DESCRIPTION}}/Next.js Template Test Build/g' package.json
sed -i 's/{{REPOSITORY_URL}}/https:\/\/github.com\/ExaDev\/NextJS-Template/g' package.json
sed -i 's/{{AUTHOR_NAME}}/ExaDev/g' package.json
sed -i 's/{{AUTHOR_EMAIL}}/dev@exadev.com/g' package.json
sed -i 's/{{LICENSE}}/MIT/g' package.json
# Setup React component placeholders (note the spaces in templates)
sed -i 's/{{ APP_NAME }}/Next.js Template/g' src/app/page.tsx
sed -i 's/{{ APP_DESCRIPTION }}/A production-ready Next.js template/g' src/app/page.tsx
sed -i 's/{{ FEATURE_1_TITLE }}/Modern Development/g' src/app/page.tsx
sed -i 's/{{ FEATURE_1_DESCRIPTION }}/Built with the latest Next.js and React/g' src/app/page.tsx
sed -i 's/{{ FEATURE_2_TITLE }}/Type Safety/g' src/app/page.tsx
sed -i 's/{{ FEATURE_2_DESCRIPTION }}/Full TypeScript support/g' src/app/page.tsx
sed -i 's/{{ FEATURE_3_TITLE }}/Testing Ready/g' src/app/page.tsx
sed -i 's/{{ FEATURE_3_DESCRIPTION }}/Comprehensive testing setup/g' src/app/page.tsx
sed -i 's/{{ FEATURE_4_TITLE }}/CI\/CD Pipeline/g' src/app/page.tsx
sed -i 's/{{ FEATURE_4_DESCRIPTION }}/Automated testing and deployment/g' src/app/page.tsx
sed -i 's/{{ CTA_TITLE }}/Get Started/g' src/app/page.tsx
sed -i 's/{{ CTA_DESCRIPTION }}/Start building your next project/g' src/app/page.tsx
sed -i 's/{{ CTA_BUTTON_TEXT }}/Start Building/g' src/app/page.tsx
# Format the code after template replacement to fix any long lines
npm run lint:fix || true
echo "✓ Template configured for CI testing"
else
echo "✓ Project is already configured"
fi
- name: Build application
env:
NEXT_PUBLIC_COMMIT_HASH: ${{ github.sha }}
NEXT_PUBLIC_BUILD_TIME: ${{ github.event.head_commit.timestamp }}
NEXT_PUBLIC_BRANCH: ${{ github.ref_name }}
run: |
# Get version from package.json
VERSION=$(node -p "require('./package.json').version")
# Add branch suffix for non-main branches
if [ "${{ github.ref_name }}" != "main" ]; then
export NEXT_PUBLIC_APP_VERSION="${VERSION}-${{ github.ref_name }}"
else
export NEXT_PUBLIC_APP_VERSION="${VERSION}"
fi
echo "Building with version info:"
echo " Version: $NEXT_PUBLIC_APP_VERSION"
echo " Commit: $NEXT_PUBLIC_COMMIT_HASH"
echo " Build Time: $NEXT_PUBLIC_BUILD_TIME"
echo " Branch: $NEXT_PUBLIC_BRANCH"
npm run build
- name: Build Storybook
run: |
echo "Building Storybook for documentation..."
npm run build-storybook
if [ -d "storybook-static" ]; then
echo "✓ Storybook build successful - 'storybook-static' directory created"
ls -la storybook-static/
else
echo "✗ Storybook build failed - 'storybook-static' directory not found"
exit 1
fi
- name: Test static export
run: |
if [ -d "out" ]; then
echo "✓ Static export successful - 'out' directory created"
ls -la out/
else
echo "✗ Static export failed - 'out' directory not found"
exit 1
fi
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: build-output
path: out/
retention-days: 7
- name: Upload Storybook artifacts
uses: actions/upload-artifact@v4
with:
name: storybook-output
path: storybook-static/
retention-days: 7
deploy:
name: Deploy to GitHub Pages
runs-on: ubuntu-latest
needs: [lint-and-format, test, security-audit, build]
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source
# Deploy to the github-pages environment
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: build-output
path: ./out
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Upload to GitHub Pages
uses: actions/upload-pages-artifact@v3
with:
path: ./out
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
release:
name: Release
runs-on: ubuntu-latest
needs: [lint-and-format, test, security-audit, build]
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
# Grant specific permissions required for semantic-release
permissions:
contents: write # to be able to publish a GitHub release
issues: write # to be able to comment on released issues
pull-requests: write # to be able to comment on released pull requests
id-token: write # to enable use of OIDC for npm provenance
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check if template is configured
id: check-configured
run: |
if grep -q "{{PROJECT_NAME}}" package.json; then
echo "configured=false" >> $GITHUB_OUTPUT
echo "⚠️ Skipping release for unconfigured template"
else
echo "configured=true" >> $GITHUB_OUTPUT
echo "✓ Template is configured, proceeding with release"
fi
- name: Setup Node.js
if: steps.check-configured.outputs.configured == 'true'
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
if: steps.check-configured.outputs.configured == 'true'
run: npm ci
- name: Release
if: steps.check-configured.outputs.configured == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npm run release