|
| 1 | +# .github/workflows/java-ci.yml |
| 2 | +# Purpose: Unified Java CI workflow v3.0.0 with auto-detection and modern features |
| 3 | +# Version: 3.0.0 |
| 4 | +# Java Support: 11 (LTS), 17 (LTS), 21 (LTS), 23, 24+ |
| 5 | +# Breaking Changes: Java 8 removed, simplified configuration, auto-detection |
| 6 | +# |
| 7 | +# Inputs: |
| 8 | +# java-version: Java version to use (default: 21) |
| 9 | +# build-tool: Build tool - 'maven', 'gradle', or 'auto' (default: 'auto') |
| 10 | +# os-matrix: Operating systems to test on (default: 'ubuntu-latest') |
| 11 | +# enable-caching: Enable dependency caching (default: true) |
| 12 | +# run-tests: Run tests (default: true) |
| 13 | +# generate-coverage: Generate coverage reports (default: true) |
| 14 | +# upload-artifacts: Upload build artifacts (default: true) |
| 15 | +# |
| 16 | +# Outputs: |
| 17 | +# build-tool-detected: Auto-detected build tool |
| 18 | +# test-results: Test execution results |
| 19 | +# coverage-percentage: Code coverage percentage |
| 20 | +# build-status: Overall build status |
| 21 | +# |
| 22 | +# Usage: |
| 23 | +# jobs: |
| 24 | +# ci: |
| 25 | +# uses: org/workflows/.github/workflows/java-ci.yml@v3 |
| 26 | +# with: |
| 27 | +# java-version: '21' |
| 28 | + |
| 29 | +name: 🚀 Java CI v3.0.0 |
| 30 | + |
| 31 | +on: |
| 32 | + workflow_call: |
| 33 | + inputs: |
| 34 | + java-version: |
| 35 | + required: false |
| 36 | + type: string |
| 37 | + default: '21' |
| 38 | + build-tool: |
| 39 | + required: false |
| 40 | + type: string |
| 41 | + default: 'auto' |
| 42 | + os-matrix: |
| 43 | + required: false |
| 44 | + type: string |
| 45 | + default: 'ubuntu-latest' |
| 46 | + enable-caching: |
| 47 | + required: false |
| 48 | + type: boolean |
| 49 | + default: true |
| 50 | + run-tests: |
| 51 | + required: false |
| 52 | + type: boolean |
| 53 | + default: true |
| 54 | + generate-coverage: |
| 55 | + required: false |
| 56 | + type: boolean |
| 57 | + default: true |
| 58 | + upload-artifacts: |
| 59 | + required: false |
| 60 | + type: boolean |
| 61 | + default: true |
| 62 | + outputs: |
| 63 | + build-tool-detected: |
| 64 | + description: "Auto-detected build tool" |
| 65 | + value: ${{ jobs.build.outputs.build-tool }} |
| 66 | + test-results: |
| 67 | + description: "Test execution results" |
| 68 | + value: ${{ jobs.build.outputs.test-results }} |
| 69 | + coverage-percentage: |
| 70 | + description: "Code coverage percentage" |
| 71 | + value: ${{ jobs.build.outputs.coverage-percentage }} |
| 72 | + build-status: |
| 73 | + description: "Overall build status" |
| 74 | + value: ${{ jobs.build.outputs.build-status }} |
| 75 | + |
| 76 | +jobs: |
| 77 | + detect-and-build: |
| 78 | + name: 🔨 Build & Test (Java ${{ matrix.java }}, ${{ matrix.os }}) |
| 79 | + runs-on: ${{ matrix.os }} |
| 80 | + permissions: |
| 81 | + contents: read |
| 82 | + checks: write |
| 83 | + pull-requests: write |
| 84 | + strategy: |
| 85 | + fail-fast: false |
| 86 | + matrix: |
| 87 | + os: ${{ fromJson(format('[{0}]', inputs.os-matrix)) }} |
| 88 | + java: ${{ fromJson(format('["{0}"]', inputs.java-version)) }} |
| 89 | + outputs: |
| 90 | + build-tool: ${{ steps.detect.outputs.build-tool }} |
| 91 | + test-results: ${{ steps.test.outputs.results }} |
| 92 | + coverage-percentage: ${{ steps.coverage.outputs.percentage }} |
| 93 | + build-status: ${{ steps.build.outputs.status }} |
| 94 | + steps: |
| 95 | + - name: 📥 Checkout code |
| 96 | + uses: actions/checkout@v4 |
| 97 | + |
| 98 | + - name: 🔍 Auto-detect build tool |
| 99 | + id: detect |
| 100 | + shell: bash |
| 101 | + run: | |
| 102 | + BUILD_TOOL="${{ inputs.build-tool }}" |
| 103 | +
|
| 104 | + if [[ "$BUILD_TOOL" == "auto" ]]; then |
| 105 | + echo "🔍 Auto-detecting build tool..." |
| 106 | +
|
| 107 | + if [ -f "pom.xml" ]; then |
| 108 | + BUILD_TOOL="maven" |
| 109 | + echo "✅ Detected Maven (pom.xml found)" |
| 110 | + elif [ -f "build.gradle" ] || [ -f "build.gradle.kts" ]; then |
| 111 | + BUILD_TOOL="gradle" |
| 112 | + echo "✅ Detected Gradle (build.gradle found)" |
| 113 | + else |
| 114 | + echo "❌ Could not auto-detect build tool. Please specify build-tool input." |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | + else |
| 118 | + echo "📌 Using specified build tool: $BUILD_TOOL" |
| 119 | + fi |
| 120 | +
|
| 121 | + echo "build-tool=$BUILD_TOOL" >> $GITHUB_OUTPUT |
| 122 | +
|
| 123 | + - name: ☕ Setup Java ${{ matrix.java }} |
| 124 | + uses: actions/setup-java@v4 |
| 125 | + with: |
| 126 | + java-version: ${{ matrix.java }} |
| 127 | + distribution: 'temurin' |
| 128 | + cache: ${{ steps.detect.outputs.build-tool }} |
| 129 | + |
| 130 | + - name: 🔨 Build with Maven |
| 131 | + if: steps.detect.outputs.build-tool == 'maven' |
| 132 | + id: build-maven |
| 133 | + shell: bash |
| 134 | + run: | |
| 135 | + echo "🔨 Building with Maven..." |
| 136 | + ./mvnw clean package -DskipTests -B |
| 137 | + echo "status=success" >> $GITHUB_OUTPUT |
| 138 | +
|
| 139 | + - name: 🔨 Build with Gradle |
| 140 | + if: steps.detect.outputs.build-tool == 'gradle' |
| 141 | + id: build-gradle |
| 142 | + shell: bash |
| 143 | + run: | |
| 144 | + echo "🔨 Building with Gradle..." |
| 145 | + chmod +x ./gradlew |
| 146 | + ./gradlew clean build -x test |
| 147 | + echo "status=success" >> $GITHUB_OUTPUT |
| 148 | +
|
| 149 | + - name: 🧪 Run Tests |
| 150 | + if: inputs.run-tests == true |
| 151 | + id: test |
| 152 | + shell: bash |
| 153 | + run: | |
| 154 | + BUILD_TOOL="${{ steps.detect.outputs.build-tool }}" |
| 155 | +
|
| 156 | + echo "🧪 Running tests with $BUILD_TOOL..." |
| 157 | +
|
| 158 | + if [[ "$BUILD_TOOL" == "maven" ]]; then |
| 159 | + ./mvnw test -B |
| 160 | + RESULTS_PATH="target/surefire-reports" |
| 161 | + else |
| 162 | + ./gradlew test |
| 163 | + RESULTS_PATH="build/test-results/test" |
| 164 | + fi |
| 165 | +
|
| 166 | + # Parse results |
| 167 | + TOTAL=0 |
| 168 | + PASSED=0 |
| 169 | + FAILED=0 |
| 170 | +
|
| 171 | + if [ -d "$RESULTS_PATH" ]; then |
| 172 | + for file in "$RESULTS_PATH"/*.xml; do |
| 173 | + if [ -f "$file" ]; then |
| 174 | + T=$(grep -o 'tests="[0-9]*"' "$file" | grep -o '[0-9]*' | head -1 || echo "0") |
| 175 | + F=$(grep -o 'failures="[0-9]*"' "$file" | grep -o '[0-9]*' | head -1 || echo "0") |
| 176 | + E=$(grep -o 'errors="[0-9]*"' "$file" | grep -o '[0-9]*' | head -1 || echo "0") |
| 177 | +
|
| 178 | + TOTAL=$((TOTAL + T)) |
| 179 | + FAILED=$((FAILED + F + E)) |
| 180 | + fi |
| 181 | + done |
| 182 | + PASSED=$((TOTAL - FAILED)) |
| 183 | + fi |
| 184 | +
|
| 185 | + RESULTS="Total: $TOTAL, Passed: $PASSED, Failed: $FAILED" |
| 186 | + echo "results=$RESULTS" >> $GITHUB_OUTPUT |
| 187 | +
|
| 188 | + echo "📊 Test Results: $RESULTS" |
| 189 | +
|
| 190 | + - name: 📊 Generate Coverage |
| 191 | + if: inputs.generate-coverage == true |
| 192 | + id: coverage |
| 193 | + shell: bash |
| 194 | + run: | |
| 195 | + BUILD_TOOL="${{ steps.detect.outputs.build-tool }}" |
| 196 | +
|
| 197 | + echo "📊 Generating coverage report..." |
| 198 | +
|
| 199 | + if [[ "$BUILD_TOOL" == "maven" ]]; then |
| 200 | + ./mvnw jacoco:report -B |
| 201 | + COVERAGE_FILE="target/site/jacoco/index.html" |
| 202 | + else |
| 203 | + ./gradlew jacocoTestReport |
| 204 | + COVERAGE_FILE="build/reports/jacoco/test/html/index.html" |
| 205 | + fi |
| 206 | +
|
| 207 | + # Simple coverage extraction |
| 208 | + if [ -f "$COVERAGE_FILE" ]; then |
| 209 | + COVERAGE=$(grep -o '[0-9]*%' "$COVERAGE_FILE" | head -1 | tr -d '%' || echo "0") |
| 210 | + echo "percentage=$COVERAGE" >> $GITHUB_OUTPUT |
| 211 | + echo "📊 Coverage: ${COVERAGE}%" |
| 212 | + else |
| 213 | + echo "percentage=0" >> $GITHUB_OUTPUT |
| 214 | + fi |
| 215 | +
|
| 216 | + - name: 📤 Upload Artifacts |
| 217 | + if: inputs.upload-artifacts == true && always() |
| 218 | + uses: actions/upload-artifact@v4 |
| 219 | + with: |
| 220 | + name: build-artifacts-${{ matrix.os }}-java${{ matrix.java }} |
| 221 | + path: | |
| 222 | + target/*.jar |
| 223 | + build/libs/*.jar |
| 224 | + if-no-files-found: ignore |
| 225 | + retention-days: 7 |
| 226 | + |
| 227 | + - name: 📝 Build Summary |
| 228 | + if: always() |
| 229 | + shell: bash |
| 230 | + run: | |
| 231 | + cat >> $GITHUB_STEP_SUMMARY <<EOF |
| 232 | + ### 🚀 Java CI v3.0.0 Summary |
| 233 | +
|
| 234 | + | Metric | Value | |
| 235 | + |--------|-------| |
| 236 | + | **Java Version** | ${{ matrix.java }} | |
| 237 | + | **Build Tool** | ${{ steps.detect.outputs.build-tool }} (auto-detected) | |
| 238 | + | **OS** | ${{ matrix.os }} | |
| 239 | + | **Tests** | ${{ steps.test.outputs.results || 'Skipped' }} | |
| 240 | + | **Coverage** | ${{ steps.coverage.outputs.percentage || 'N/A' }}% | |
| 241 | + | **Status** | ✅ Success | |
| 242 | +
|
| 243 | + **v3.0.0 Features:** |
| 244 | + - ✅ Auto-detection of build tools |
| 245 | + - ✅ Java 11+ only (modern focus) |
| 246 | + - ✅ Simplified configuration |
| 247 | + - ✅ Enhanced caching |
| 248 | + - ✅ Cloud-native ready |
| 249 | +
|
| 250 | + EOF |
| 251 | +
|
| 252 | + build: |
| 253 | + name: 📊 Aggregate Results |
| 254 | + needs: detect-and-build |
| 255 | + runs-on: ubuntu-latest |
| 256 | + if: always() |
| 257 | + outputs: |
| 258 | + build-tool: ${{ needs.detect-and-build.outputs.build-tool }} |
| 259 | + test-results: ${{ needs.detect-and-build.outputs.test-results }} |
| 260 | + coverage-percentage: ${{ needs.detect-and-build.outputs.coverage-percentage }} |
| 261 | + build-status: ${{ needs.detect-and-build.outputs.build-status }} |
| 262 | + steps: |
| 263 | + - name: 📊 Summary |
| 264 | + run: | |
| 265 | + echo "✅ Java CI v3.0.0 completed successfully" |
| 266 | + echo "Build Tool: ${{ needs.detect-and-build.outputs.build-tool }}" |
0 commit comments