-
-
Notifications
You must be signed in to change notification settings - Fork 12
455 lines (394 loc) · 18.9 KB
/
dev-to-main.yml
File metadata and controls
455 lines (394 loc) · 18.9 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
# ─────────────────────────────────────────────────────────────────
# Release to Main Workflow (Release Gates)
# ─────────────────────────────────────────────────────────────────
# Validates release pull requests before merging to main (production).
#
# Release Gates:
# - Source must be dev or release/* branches
# - Production build must succeed
# - Smoke tests must pass
# - Security quick scan (informational only)
# - Deployment readiness checklist
#
# Allowed source branches:
# - dev: Production releases (standard flow)
# - release/*: Emergency hotfix releases
# - dependabot/*: Automated dependency updates
#
# All other branches (feature/*, fix/*, test/*) must merge to dev first.
#
# Author: Alireza Rezvani
# Date: 2025-11-06
# ─────────────────────────────────────────────────────────────────
name: Release to Main
on:
pull_request:
types:
- opened
- synchronize
- reopened
branches:
- main
permissions:
contents: read
pull-requests: write
statuses: write
# Only one release PR can be processed at a time
concurrency:
group: release-to-main
cancel-in-progress: false
jobs:
# ─────────────────────────────────────────────────────────────────
# Source Branch Validation
# ─────────────────────────────────────────────────────────────────
validate-source:
name: Validate Source Branch
runs-on: ubuntu-latest
steps:
- name: Validate source is dev branch
run: |
SOURCE_BRANCH="${{ github.head_ref }}"
ACTOR="${{ github.actor }}"
echo "🔍 Validating source branch: $SOURCE_BRANCH (Actor: $ACTOR)"
# Allow dependabot PRs to bypass dev branch requirement
# Check both actor name and branch name prefix
if [[ "$ACTOR" == "dependabot[bot]" ]] || [[ "$SOURCE_BRANCH" =~ ^dependabot/ ]]; then
echo "✅ Dependabot PR detected - skipping source branch validation"
echo " Dependabot PRs are allowed to merge directly to main"
echo " Actor: $ACTOR"
echo " Branch: $SOURCE_BRANCH"
exit 0
fi
# Allowlist: branches permitted to merge to main
# Only: dev (production), release/* (hotfixes)
# All other branches (feature/*, fix/*, test/*) must go through dev first
ALLOWED_REGEX='^(dev|release/.*)$'
if [[ ! $SOURCE_BRANCH =~ $ALLOWED_REGEX ]]; then
echo "❌ Invalid source branch: $SOURCE_BRANCH"
echo ""
echo "Only dev and release/* branches may be merged to 'main'."
echo ""
echo "Allowed patterns:"
echo " - dev (production releases)"
echo " - release/* (emergency hotfixes)"
echo ""
echo "Got: $SOURCE_BRANCH → main"
echo ""
echo "All other branches (feature/*, fix/*, test/*) must merge to dev first:"
echo " $SOURCE_BRANCH → dev → main"
echo ""
echo "If using a different branching strategy, adjust ALLOWED_REGEX in this workflow."
exit 1
fi
echo "✅ Source branch is valid: $SOURCE_BRANCH → main"
# ─────────────────────────────────────────────────────────────────
# Production Build Validation
# ─────────────────────────────────────────────────────────────────
build-prod:
name: Production Build
runs-on: ubuntu-latest
needs: validate-source
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup Node.js and pnpm
uses: ./.github/actions/setup-node-pnpm
with:
node-version: '20'
pnpm-version: '9'
- name: Build for production
run: |
echo "🏗️ Building for production..."
pnpm run build:prod || pnpm run build
env:
NODE_ENV: production
CI: true
- name: Verify build artifacts
run: |
echo "🔍 Verifying build artifacts..."
# Common build output directories
BUILD_DIRS=("dist" "build" "out" ".next")
FOUND_BUILD=false
for dir in "${BUILD_DIRS[@]}"; do
if [ -d "$dir" ]; then
echo "✅ Found build directory: $dir"
echo " Size: $(du -sh $dir | cut -f1)"
echo " Files: $(find $dir -type f | wc -l)"
FOUND_BUILD=true
break
fi
done
if [[ "$FOUND_BUILD" == "false" ]]; then
echo "⚠️ No standard build directory found. This may be expected for some project types."
fi
- name: Upload production build
uses: actions/upload-artifact@v7
with:
name: production-build
path: |
dist/
build/
out/
.next/
retention-days: 7
if-no-files-found: warn
# ─────────────────────────────────────────────────────────────────
# Smoke Tests (Critical Paths)
# ─────────────────────────────────────────────────────────────────
smoke-tests:
name: Smoke Tests
runs-on: ubuntu-latest
needs: build-prod
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup Node.js and pnpm
uses: ./.github/actions/setup-node-pnpm
with:
node-version: '20'
pnpm-version: '9'
- name: Download production build
uses: actions/download-artifact@v6
with:
name: production-build
- name: Run smoke tests
run: |
echo "🔥 Running smoke tests..."
# Try common smoke test scripts
if pnpm run test:smoke 2>/dev/null; then
echo "✅ Smoke tests passed"
elif pnpm run test:e2e:smoke 2>/dev/null; then
echo "✅ Smoke tests passed"
elif pnpm run test -- --testNamePattern="smoke" 2>/dev/null; then
echo "✅ Smoke tests passed"
else
echo "⚠️ No smoke tests found. Running basic sanity checks..."
# Basic sanity: Check if build is valid
if [ -d "dist" ] || [ -d "build" ] || [ -d "out" ] || [ -d ".next" ]; then
echo "✅ Build artifacts exist - basic sanity passed"
else
echo "❌ Build artifacts not found"
exit 1
fi
fi
env:
CI: true
- name: Upload smoke test results on failure
if: failure()
uses: actions/upload-artifact@v7
with:
name: smoke-test-results
path: |
test-results/
screenshots/
retention-days: 7
if-no-files-found: ignore
# ─────────────────────────────────────────────────────────────────
# Security Quick Scan (Informational Only)
# ─────────────────────────────────────────────────────────────────
security-quickscan:
name: Security Quick Scan
runs-on: ubuntu-latest
needs: validate-source
continue-on-error: true
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup Node.js and pnpm
uses: ./.github/actions/setup-node-pnpm
with:
node-version: '20'
pnpm-version: '9'
- name: Run npm audit
run: |
echo "🔒 Running npm security audit..."
pnpm audit --audit-level=high || echo "⚠️ Vulnerabilities found (non-blocking)"
- name: Check for common security issues
run: |
echo "🔍 Checking for common security issues..."
ISSUES_FOUND=0
# Check for hardcoded secrets (basic patterns)
echo "Checking for potential secrets..."
if grep -r -i "api[_-]key\|api[_-]secret\|password\|token" --include="*.ts" --include="*.js" --exclude-dir=node_modules --exclude-dir=dist | grep -v "^\s*//" | grep -v "^\s*\*"; then
echo "⚠️ Potential hardcoded secrets found (review manually)"
((ISSUES_FOUND++))
fi
# Check for console.log in production
echo "Checking for debug statements..."
if grep -r "console\.log\|debugger" --include="*.ts" --include="*.tsx" --include="*.js" --exclude-dir=node_modules --exclude-dir=dist | grep -v "^\s*//" | grep -v "^\s*\*" | wc -l | grep -v "^0$"; then
echo "⚠️ Debug statements found (consider removing for production)"
((ISSUES_FOUND++))
fi
if [[ $ISSUES_FOUND -eq 0 ]]; then
echo "✅ No obvious security issues found"
else
echo "⚠️ Found $ISSUES_FOUND potential issues (review recommended)"
fi
- name: Generate security report
if: always()
run: |
echo "# 🔒 Security Scan Report" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Status:** Informational (non-blocking)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Security scan completed. Review the logs above for details." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "⚠️ **Note:** This is a quick scan only. Consider running:" >> $GITHUB_STEP_SUMMARY
echo "- Full security audit with npm audit" >> $GITHUB_STEP_SUMMARY
echo "- SAST tools (Snyk, SonarQube)" >> $GITHUB_STEP_SUMMARY
echo "- Dependency vulnerability scanning" >> $GITHUB_STEP_SUMMARY
# ─────────────────────────────────────────────────────────────────
# Deployment Readiness Checklist
# ─────────────────────────────────────────────────────────────────
deployment-readiness:
name: Deployment Readiness
runs-on: ubuntu-latest
needs:
- validate-source
- build-prod
- smoke-tests
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Check version bump
id: version-check
run: |
echo "📦 Checking version..."
if [ -f "package.json" ]; then
VERSION=$(jq -r '.version' package.json)
echo "Current version: $VERSION"
echo "version=$VERSION" >> $GITHUB_OUTPUT
else
echo "⚠️ package.json not found - skipping version check"
echo "version=unknown" >> $GITHUB_OUTPUT
fi
- name: Check changelog
id: changelog-check
run: |
echo "📝 Checking changelog..."
CHANGELOG_FILES=("CHANGELOG.md" "HISTORY.md" "RELEASES.md")
CHANGELOG_FOUND=false
for file in "${CHANGELOG_FILES[@]}"; do
if [ -f "$file" ]; then
echo "✅ Found changelog: $file"
CHANGELOG_FOUND=true
break
fi
done
if [[ "$CHANGELOG_FOUND" == "false" ]]; then
echo "⚠️ No changelog file found. Consider creating one for production releases."
fi
echo "changelog-found=$CHANGELOG_FOUND" >> $GITHUB_OUTPUT
- name: Generate deployment readiness report
run: |
echo "# 🚀 Deployment Readiness Report" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "## ✅ Pre-Deployment Checks" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Check | Status |" >> $GITHUB_STEP_SUMMARY
echo "|-------|--------|" >> $GITHUB_STEP_SUMMARY
echo "| Source Branch | ✅ dev → main |" >> $GITHUB_STEP_SUMMARY
echo "| Production Build | ✅ Passed |" >> $GITHUB_STEP_SUMMARY
echo "| Smoke Tests | ✅ Passed |" >> $GITHUB_STEP_SUMMARY
echo "| Security Scan | ℹ️ Informational |" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "## 📊 Release Information" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- **Version:** \`${{ steps.version-check.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
echo "- **Changelog:** ${{ steps.changelog-check.outputs.changelog-found == 'true' && '✅ Found' || '⚠️ Not found' }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "## 📋 Pre-Merge Checklist" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Before merging this release PR, ensure:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- [ ] Version bumped in package.json" >> $GITHUB_STEP_SUMMARY
echo "- [ ] Changelog updated with release notes" >> $GITHUB_STEP_SUMMARY
echo "- [ ] All linked issues are tested" >> $GITHUB_STEP_SUMMARY
echo "- [ ] Breaking changes documented" >> $GITHUB_STEP_SUMMARY
echo "- [ ] Migration guide provided (if needed)" >> $GITHUB_STEP_SUMMARY
echo "- [ ] Deployment plan reviewed" >> $GITHUB_STEP_SUMMARY
echo "- [ ] Rollback plan documented" >> $GITHUB_STEP_SUMMARY
echo "- [ ] Stakeholders notified" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# ─────────────────────────────────────────────────────────────────
# Final Release Status
# ─────────────────────────────────────────────────────────────────
release-status:
name: Release Gate Status
runs-on: ubuntu-latest
needs:
- validate-source
- build-prod
- smoke-tests
- deployment-readiness
if: always()
steps:
- name: Generate final release summary
run: |
echo "# 🎯 Release Gate Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "## ✅ Required Checks" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Check | Status |" >> $GITHUB_STEP_SUMMARY
echo "|-------|--------|" >> $GITHUB_STEP_SUMMARY
# Source validation
if [[ "${{ needs.validate-source.result }}" == "success" ]]; then
echo "| Source Branch | ✅ Valid (dev → main) |" >> $GITHUB_STEP_SUMMARY
else
echo "| Source Branch | ❌ Invalid |" >> $GITHUB_STEP_SUMMARY
fi
# Production build
if [[ "${{ needs.build-prod.result }}" == "success" ]]; then
echo "| Production Build | ✅ Passed |" >> $GITHUB_STEP_SUMMARY
else
echo "| Production Build | ❌ Failed |" >> $GITHUB_STEP_SUMMARY
fi
# Smoke tests
if [[ "${{ needs.smoke-tests.result }}" == "success" ]]; then
echo "| Smoke Tests | ✅ Passed |" >> $GITHUB_STEP_SUMMARY
else
echo "| Smoke Tests | ❌ Failed |" >> $GITHUB_STEP_SUMMARY
fi
# Deployment readiness
if [[ "${{ needs.deployment-readiness.result }}" == "success" ]]; then
echo "| Deployment Readiness | ✅ Ready |" >> $GITHUB_STEP_SUMMARY
else
echo "| Deployment Readiness | ❌ Not Ready |" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
# Overall status - check if ALL gates passed
# Treat anything other than "success" as blocking (including "skipped", "failure", "cancelled")
VALIDATE_RESULT="${{ needs.validate-source.result }}"
BUILD_RESULT="${{ needs.build-prod.result }}"
SMOKE_RESULT="${{ needs.smoke-tests.result }}"
DEPLOY_RESULT="${{ needs.deployment-readiness.result }}"
if [[ "$VALIDATE_RESULT" != "success" ]] || \
[[ "$BUILD_RESULT" != "success" ]] || \
[[ "$SMOKE_RESULT" != "success" ]] || \
[[ "$DEPLOY_RESULT" != "success" ]]; then
echo "## ❌ Release Blocked" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "One or more release gates did not pass. Please fix the issues before merging to production." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Gate Results:**" >> $GITHUB_STEP_SUMMARY
echo "- Source validation: $VALIDATE_RESULT" >> $GITHUB_STEP_SUMMARY
echo "- Production build: $BUILD_RESULT" >> $GITHUB_STEP_SUMMARY
echo "- Smoke tests: $SMOKE_RESULT" >> $GITHUB_STEP_SUMMARY
echo "- Deployment readiness: $DEPLOY_RESULT" >> $GITHUB_STEP_SUMMARY
exit 1
else
echo "## ✅ Release Approved" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "All release gates passed! This release is ready to be deployed to production." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 🚀 Next Steps:" >> $GITHUB_STEP_SUMMARY
echo "1. Review the pre-merge checklist above" >> $GITHUB_STEP_SUMMARY
echo "2. Get required approvals" >> $GITHUB_STEP_SUMMARY
echo "3. Merge this PR to trigger deployment" >> $GITHUB_STEP_SUMMARY
echo "4. Monitor deployment status" >> $GITHUB_STEP_SUMMARY
echo "5. Verify production deployment" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "---" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "_Release validation completed at $(date -u '+%Y-%m-%d %H:%M:%S UTC')_" >> $GITHUB_STEP_SUMMARY