-
Notifications
You must be signed in to change notification settings - Fork 5
674 lines (586 loc) · 22.3 KB
/
build-and-publish.yml
File metadata and controls
674 lines (586 loc) · 22.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
name: Build, Test, and Publish
# Manual workflow trigger with version input and publish option
on:
workflow_dispatch:
inputs:
version:
description: 'Release version (semantic versioning: X.Y.Z)'
required: true
type: string
publish:
description: 'Publish artifacts to GitHub Releases'
required: false
type: boolean
default: false
pr_number:
description: 'PR number that triggered this release (auto-filled by auto-version)'
required: false
type: string
default: ''
# Least-privilege permissions: read-only at workflow level, write only where needed
permissions:
contents: read
actions: read
# Environment variables used across jobs
env:
JAVA_VERSION: '17'
JAVA_DISTRIBUTION: 'temurin'
GRADLE_OPTS: -Dorg.gradle.daemon=false -Dorg.gradle.parallel=true -Dorg.gradle.caching=true
jobs:
# ============================================================================
# JOB 1: VALIDATE
# Validate version format and ensure version doesn't already exist
# ============================================================================
validate:
name: Validate Version
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
with:
persist-credentials: false
fetch-depth: 0 # Fetch all history for tag checking
- name: Validate semantic version format
env:
VERSION: ${{ inputs.version }}
run: |
# Validate semantic versioning format (X.Y.Z or X.Y.Z-suffix)
if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?$'; then
echo "::error::Invalid version format: $VERSION"
echo "::error::Expected semantic versioning format: X.Y.Z or X.Y.Z-suffix"
echo "::error::Examples: 1.0.0, 1.2.3, 2.0.0-beta.1"
exit 1
fi
echo "Version format is valid: $VERSION"
- name: Check if version/tag already exists
env:
VERSION: ${{ inputs.version }}
run: |
# Check if tag already exists locally or remotely
if git tag -l | grep -qx "v${VERSION}" || git tag -l | grep -qx "${VERSION}"; then
echo "::info::Version tag already exists on the branch: ${VERSION}"
fi
echo "Version is correct: $VERSION"
- name: Validation summary
env:
VERSION: ${{ inputs.version }}
PUBLISH: ${{ inputs.publish }}
run: |
{
echo "### Validation Passed"
echo "- **Version**: ${VERSION}"
echo "- **Publish**: ${PUBLISH}"
echo "- **Format**: Valid semantic version"
echo "- **Uniqueness**: No existing tag found"
} >> "$GITHUB_STEP_SUMMARY"
# ============================================================================
# JOB 2: SECURITY SCAN - REPOSITORY
# Scans repository for IaC misconfigurations, secrets, and security issues
# Runs in parallel with build for efficiency
# ============================================================================
security-scan-repo:
name: Security Scan - Repository
runs-on: ubuntu-latest
needs: validate
steps:
- name: Check out repository
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
with:
persist-credentials: false
- name: Run Trivy repository scan
uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1 # 0.35.0
with:
scan-type: 'config'
scan-ref: '.'
format: 'sarif'
output: 'trivy-repo-results.sarif'
severity: 'MEDIUM,HIGH,CRITICAL'
exit-code: '1' # Fail on vulnerabilities
hide-progress: false
scanners: 'misconfig,secret'
- name: Repository scan summary
if: success()
run: |
{
echo "### Repository Security Scan Passed"
echo "- **Scanner**: Trivy config + secrets"
echo "- **Severity Threshold**: MEDIUM+"
echo "- **Status**: No vulnerabilities detected"
} >> "$GITHUB_STEP_SUMMARY"
# ============================================================================
# JOB 3: BUILD
# Compiles code, runs linting, creates JAR artifacts with version override
# ============================================================================
build:
name: Build Application
runs-on: ubuntu-latest
needs: validate
outputs:
version: ${{ inputs.version }}
steps:
- name: Check out repository
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
with:
persist-credentials: false
- name: Set up JDK ${{ env.JAVA_VERSION }}
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: ${{ env.JAVA_DISTRIBUTION }}
- name: Setup Gradle
uses: gradle/actions/setup-gradle@0723195856401067f7a2779048b490ace7a47d7c # v5
with:
cache-read-only: false # Build job writes to cache
- name: Grant execute permission to gradlew
run: chmod +x ./gradlew
- name: Run Kotlin linting (ktlint)
run: ./gradlew ktlintCheck --no-daemon --stacktrace
- name: Build application (skip tests)
env:
VERSION: ${{ inputs.version }}
run: |
echo "Building with version override: ${VERSION}"
./gradlew clean build -x test \
-Pversion="${VERSION}" \
--no-daemon \
--stacktrace \
--warning-mode all
- name: Build shadow JAR (fat JAR)
env:
VERSION: ${{ inputs.version }}
run: |
./gradlew shadowJar \
-Pversion="${VERSION}" \
--no-daemon \
--stacktrace
- name: List build artifacts
env:
VERSION: ${{ inputs.version }}
run: |
echo "=== Files in build/libs/ ==="
ls -lh build/libs/
echo ""
echo "Expected version: ${VERSION}"
{
echo "### Build Artifacts:"
echo '```'
ls -lh build/libs/
echo '```'
} >> "$GITHUB_STEP_SUMMARY"
- name: Upload main JAR
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5
with:
name: main-jar
path: build/libs/*-${{ inputs.version }}.jar
if-no-files-found: error
retention-days: 30
- name: Upload shadow JAR
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5
with:
name: shadow-jar
path: build/libs/*-${{ inputs.version }}-all.jar
if-no-files-found: error
retention-days: 30
- name: Upload build outputs for reuse
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5
with:
name: build-outputs
path: |
build/classes/
build/resources/
build/libs/
build/kotlin/
.gradle/
retention-days: 1
- name: Build summary
env:
VERSION: ${{ inputs.version }}
run: |
{
echo "### Build Completed Successfully"
echo "- **Version**: ${VERSION}"
echo "- **Java Version**: ${JAVA_VERSION}"
echo "- **Kotlin Linting**: Passed"
echo "- **Artifacts**: Main JAR + Shadow JAR"
} >> "$GITHUB_STEP_SUMMARY"
# ============================================================================
# JOB 4: TEST
# Runs unit tests with JaCoCo code coverage
# Sequential after build to catch integration issues
# ============================================================================
test:
name: Run Tests & Coverage
runs-on: ubuntu-latest
needs: build
steps:
- name: Check out repository
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
with:
persist-credentials: false
- name: Set up JDK ${{ env.JAVA_VERSION }}
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: ${{ env.JAVA_DISTRIBUTION }}
- name: Setup Gradle
uses: gradle/actions/setup-gradle@0723195856401067f7a2779048b490ace7a47d7c # v5
with:
cache-read-only: true # Test job reads from cache
- name: Download build outputs
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6
with:
name: build-outputs
path: .
- name: Grant execute permission to gradlew
run: chmod +x ./gradlew
- name: Run tests with JaCoCo coverage
env:
VERSION: ${{ inputs.version }}
run: |
./gradlew test jacocoTestReport \
-Pversion="${VERSION}" \
--no-daemon \
--stacktrace
- name: Upload test results
if: always() # Upload even if tests fail
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5
with:
name: test-results
path: |
build/reports/tests/test/
build/test-results/test/
retention-days: 30
- name: Upload coverage report
if: always()
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5
with:
name: coverage-report
path: build/reports/jacoco/test/
retention-days: 30
- name: Test summary
if: success()
run: |
{
echo "### Tests Passed"
echo "- **Test Framework**: JUnit 5 + Kotlin Test"
echo "- **Coverage Tool**: JaCoCo"
echo "- **Status**: All tests passed"
# Count test results if available
if [ -d "build/test-results/test" ]; then
TEST_COUNT=$(find build/test-results/test -name "*.xml" | wc -l)
echo "- **Test Files**: $TEST_COUNT"
fi
} >> "$GITHUB_STEP_SUMMARY"
# ============================================================================
# JOB 5: SECURITY SCAN - DEPENDENCIES
# Scans project dependencies for known vulnerabilities (CVEs)
# ============================================================================
security-scan-dependencies:
name: Security Scan - Dependencies
runs-on: ubuntu-latest
needs: build
steps:
- name: Check out repository
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
with:
persist-credentials: false
- name: Download main JAR
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6
with:
name: main-jar
path: artifacts/
- name: Download shadow JAR
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6
with:
name: shadow-jar
path: artifacts/
- name: List artifacts for scanning
run: |
echo "Artifacts to scan:"
ls -lh artifacts/
- name: Run Trivy filesystem scan on JARs
uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1 # 0.35.0
with:
scan-type: 'fs'
scan-ref: 'artifacts/'
format: 'sarif'
output: 'trivy-dependencies-results.sarif'
severity: 'MEDIUM,HIGH,CRITICAL'
exit-code: '1' # Fail on vulnerabilities
scanners: 'vuln'
- name: Dependency scan summary
if: success()
run: |
{
echo "### Dependency Security Scan Passed"
echo "- **Scanner**: Trivy filesystem"
echo "- **Target**: Project dependencies"
echo "- **Severity Threshold**: MEDIUM+"
echo "- **Status**: No vulnerable dependencies detected"
} >> "$GITHUB_STEP_SUMMARY"
# ============================================================================
# JOB 6: SECURITY SCAN - ARTIFACTS
# Scans built JAR files for embedded vulnerabilities
# ============================================================================
security-scan-artifacts:
name: Security Scan - Artifacts
runs-on: ubuntu-latest
needs: build
steps:
- name: Download main JAR
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6
with:
name: main-jar
path: artifacts/
- name: Download shadow JAR
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6
with:
name: shadow-jar
path: artifacts/
- name: List artifacts for scanning
run: |
echo "Artifacts to scan:"
ls -lh artifacts/
- name: Run Trivy scan on main JAR
uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1 # 0.35.0
with:
scan-type: 'fs'
scan-ref: 'artifacts/'
format: 'sarif'
output: 'trivy-artifacts-results.sarif'
severity: 'MEDIUM,HIGH,CRITICAL'
exit-code: '1' # Fail on vulnerabilities
scanners: 'vuln'
- name: Artifact scan summary
if: success()
run: |
{
echo "### Artifact Security Scan Passed"
echo "- **Scanner**: Trivy filesystem"
echo "- **Targets**: Main JAR + Shadow JAR"
echo "- **Severity Threshold**: MEDIUM+"
echo "- **Status**: No vulnerabilities in artifacts"
} >> "$GITHUB_STEP_SUMMARY"
# ============================================================================
# JOB 7: CODEQL ANALYSIS
# Scans source code for security vulnerabilities
# Only runs when preparing to publish a release
# ============================================================================
codeql-scan:
name: CodeQL Security Analysis
runs-on: ubuntu-latest
needs: build
if: inputs.publish == true
steps:
- name: Checkout repository
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
with:
persist-credentials: false
- name: Set up JDK ${{ env.JAVA_VERSION }}
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: ${{ env.JAVA_DISTRIBUTION }}
- name: Initialize CodeQL
uses: github/codeql-action/init@c10b8064de6f491fea524254123dbe5e09572f13 # v4
with:
languages: java
queries: security-extended
- name: Setup Gradle
uses: gradle/actions/setup-gradle@0723195856401067f7a2779048b490ace7a47d7c # v5
with:
cache-read-only: true
- name: Grant execute permission to gradlew
run: chmod +x ./gradlew
- name: Build for CodeQL analysis
env:
VERSION: ${{ inputs.version }}
run: |
./gradlew clean build -x test \
-Pversion="${VERSION}" \
--rerun-tasks \
--no-daemon \
--stacktrace
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@c10b8064de6f491fea524254123dbe5e09572f13 # v4
with:
category: "release-scan"
upload: false
- name: CodeQL scan summary
if: success()
run: |
{
echo "### CodeQL Security Scan Passed"
echo "- **Language**: Java/Kotlin"
echo "- **Queries**: Security-only"
echo "- **Status**: No code vulnerabilities detected"
} >> "$GITHUB_STEP_SUMMARY"
# ============================================================================
# JOB 8: PUBLISH
# Creates GitHub Release and attaches JAR artifacts
# Only runs if publish checkbox is enabled and all checks pass
# ============================================================================
publish:
name: Publish to GitHub Releases
runs-on: ubuntu-latest
needs: [test, security-scan-repo, security-scan-dependencies, security-scan-artifacts, codeql-scan]
if: inputs.publish == true
permissions:
contents: write # Required for creating releases and tags
steps:
- name: Check out repository
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
with:
persist-credentials: false
fetch-depth: 0
- name: Download main JAR
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6
with:
name: main-jar
path: release-artifacts/
- name: Download shadow JAR
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6
with:
name: shadow-jar
path: release-artifacts/
- name: List release artifacts
run: |
echo "Artifacts to publish:"
ls -lh release-artifacts/
- name: Generate release notes
id: release-notes
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ inputs.version }}
PR_NUMBER: ${{ inputs.pr_number }}
run: |
# Get latest tag for comparison
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
# Start release notes
: > release-notes.md
# If triggered by a tagged PR, prepend PR content
if [ -n "$PR_NUMBER" ]; then
PR_TITLE=$(gh pr view "$PR_NUMBER" --json title --jq '.title' 2>/dev/null || true)
PR_BODY=$(gh pr view "$PR_NUMBER" --json body --jq '.body' 2>/dev/null || true)
if [ -n "$PR_TITLE" ]; then
{
echo "## What's New in v${VERSION}"
echo ""
echo "### ${PR_TITLE} (#${PR_NUMBER})"
echo ""
echo "${PR_BODY}"
echo ""
echo "---"
echo ""
} >> release-notes.md
fi
fi
cat >> release-notes.md << EOF
## Release v${VERSION}
### Artifacts
This release includes:
- **Main JAR**: Standard library JAR (without dependencies)
- **Shadow JAR**: All-in-one fat JAR with embedded dependencies
### Security
All artifacts have been scanned and verified:
- [PASS] Repository configuration scan passed
- [PASS] Dependency vulnerability scan passed
- [PASS] Artifact security scan passed
- [PASS] All tests passed with coverage
### Installation
**Gradle (Kotlin DSL)**:
\`\`\`kotlin
repositories {
maven {
url = uri("https://github.com/CDCgov/prime-fhir-converter/releases/download/v${VERSION}/")
}
}
dependencies {
implementation("gov.cdc.prime:prime-fhir-converter:${VERSION}")
}
\`\`\`
**Maven**:
\`\`\`xml
<dependency>
<groupId>gov.cdc.prime</groupId>
<artifactId>fhirconverter</artifactId>
<version>${VERSION}</version>
</dependency>
\`\`\`
Or download the JARs directly from the assets below.
EOF
if [ -n "$LATEST_TAG" ]; then
{
echo ""
echo "### Changes Since $LATEST_TAG"
echo ""
git log "${LATEST_TAG}..HEAD" --pretty=format:"- %s (%h)"
} >> release-notes.md
fi
cat release-notes.md
- name: Create GitHub Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ inputs.version }}
run: |
gh release create "v${VERSION}" \
--title "Release v${VERSION}" \
--notes-file release-notes.md \
release-artifacts/*.jar
- name: Publish summary
env:
VERSION: ${{ inputs.version }}
SERVER_URL: ${{ github.server_url }}
REPO: ${{ github.repository }}
run: |
{
echo "### Release Published Successfully"
echo ""
echo "- **Version**: v${VERSION}"
echo "- **Tag**: v${VERSION}"
echo "- **Artifacts**: 2 JARs attached"
echo "- **URL**: ${SERVER_URL}/${REPO}/releases/tag/v${VERSION}"
echo ""
echo "[PASS] **All security scans passed**"
echo "[PASS] **All tests passed**"
echo "[PASS] **Build artifacts verified**"
} >> "$GITHUB_STEP_SUMMARY"
# ============================================================================
# WORKFLOW SUMMARY
# This job always runs and provides final status
# ============================================================================
workflow-summary:
name: Workflow Summary
runs-on: ubuntu-latest
needs: [validate, build, test, security-scan-repo, security-scan-dependencies, security-scan-artifacts]
if: always()
steps:
- name: Generate workflow summary
env:
VALIDATE_RESULT: ${{ needs.validate.result }}
BUILD_RESULT: ${{ needs.build.result }}
TEST_RESULT: ${{ needs.test.result }}
REPO_SCAN_RESULT: ${{ needs.security-scan-repo.result }}
DEP_SCAN_RESULT: ${{ needs.security-scan-dependencies.result }}
ART_SCAN_RESULT: ${{ needs.security-scan-artifacts.result }}
PUBLISH: ${{ inputs.publish }}
run: |
status() { [ "$1" = "success" ] && echo "[PASS]" || echo "[FAIL]"; }
{
echo "## Workflow Execution Summary"
echo ""
echo "| Stage | Status |"
echo "|-------|--------|"
echo "| Validation | $(status "$VALIDATE_RESULT") |"
echo "| Build | $(status "$BUILD_RESULT") |"
echo "| Tests | $(status "$TEST_RESULT") |"
echo "| Repo Security Scan | $(status "$REPO_SCAN_RESULT") |"
echo "| Dependency Scan | $(status "$DEP_SCAN_RESULT") |"
echo "| Artifact Scan | $(status "$ART_SCAN_RESULT") |"
echo ""
if [ "$PUBLISH" == "true" ]; then
echo "**Publish**: Enabled (conditional on all checks passing)"
else
echo "**Publish**: Disabled"
fi
} >> "$GITHUB_STEP_SUMMARY"