|
| 1 | +name: Aggressive Maven CI/CD Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ master ] |
| 6 | + pull_request: |
| 7 | + branches: [ master ] |
| 8 | + schedule: |
| 9 | + - cron: '0 3 * * *' # Runs daily at 3 AM UTC |
| 10 | + workflow_dispatch: # Allows manual trigger |
| 11 | + |
| 12 | +jobs: |
| 13 | + # Job 1: Build the Project |
| 14 | + build: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + name: Build Project |
| 17 | + steps: |
| 18 | + - name: Checkout Repository |
| 19 | + uses: actions/checkout@v3 |
| 20 | + |
| 21 | + - name: Set Up JDK 11 |
| 22 | + uses: actions/setup-java@v3 |
| 23 | + with: |
| 24 | + distribution: 'temurin' |
| 25 | + java-version: '11' |
| 26 | + cache: maven |
| 27 | + |
| 28 | + - name: Display Java Version |
| 29 | + run: java -version |
| 30 | + |
| 31 | + - name: Maven Clean and Compile |
| 32 | + run: mvn clean compile -fn |
| 33 | + |
| 34 | + - name: Upload Build Logs |
| 35 | + if: always() |
| 36 | + uses: actions/upload-artifact@v3 |
| 37 | + with: |
| 38 | + name: build-logs |
| 39 | + path: target/build.log |
| 40 | + |
| 41 | + - name: Notify Build Issues |
| 42 | + if: failure() |
| 43 | + uses: dawidd6/action-send-mail@v3 |
| 44 | + with: |
| 45 | + server_address: smtp.example.com |
| 46 | + server_port: 587 |
| 47 | + username: ${{ secrets.SMTP_USERNAME }} |
| 48 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 49 | + subject: 'Build Job Encountered Issues' |
| 50 | + body: | |
| 51 | + The build job has completed with issues. Please check the workflow logs for details. |
| 52 | + to: your-email@example.com |
| 53 | + from: github-actions@example.com |
| 54 | + |
| 55 | + # Job 2: Manage Maven Dependencies |
| 56 | + manage-dependencies: |
| 57 | + needs: build |
| 58 | + runs-on: ubuntu-latest |
| 59 | + name: Manage Maven Dependencies |
| 60 | + steps: |
| 61 | + - name: Checkout Repository |
| 62 | + uses: actions/checkout@v3 |
| 63 | + |
| 64 | + - name: Set Up JDK 11 |
| 65 | + uses: actions/setup-java@v3 |
| 66 | + with: |
| 67 | + distribution: 'temurin' |
| 68 | + java-version: '11' |
| 69 | + cache: maven |
| 70 | + |
| 71 | + - name: Generate Dependency Tree |
| 72 | + run: mvn dependency:tree -fn -DoutputType=dot -DoutputFile=dependency-tree.dot |
| 73 | + |
| 74 | + - name: Upload Dependency Tree |
| 75 | + if: always() |
| 76 | + uses: actions/upload-artifact@v3 |
| 77 | + with: |
| 78 | + name: dependency-tree |
| 79 | + path: dependency-tree.dot |
| 80 | + |
| 81 | + - name: Analyze Dependencies |
| 82 | + run: | |
| 83 | + mvn dependency:analyze -fn > dependency-analysis.txt || echo "Dependency analysis failed, but continuing" |
| 84 | +
|
| 85 | + - name: Upload Dependency Analysis Report |
| 86 | + if: always() |
| 87 | + uses: actions/upload-artifact@v3 |
| 88 | + with: |
| 89 | + name: dependency-analysis |
| 90 | + path: dependency-analysis.txt |
| 91 | + |
| 92 | + - name: Attempt to Auto-Fix Missing Dependencies |
| 93 | + if: failure() |
| 94 | + run: | |
| 95 | + # Example: Automatically add missing dependencies (Requires custom scripting) |
| 96 | + # This is a placeholder for actual implementation |
| 97 | + echo "Attempting to auto-fix missing dependencies..." |
| 98 | + # Implement logic to parse dependency-analysis.txt and update pom.xml |
| 99 | + # For example, using sed, awk, or a custom script to insert dependencies |
| 100 | + # WARNING: Automated POM modifications can introduce instability |
| 101 | +
|
| 102 | + - name: Commit and Push Auto-Fixes |
| 103 | + if: failure() |
| 104 | + uses: stefanzweifel/git-auto-commit-action@v4 |
| 105 | + with: |
| 106 | + commit_message: "Auto-fix missing dependencies via GitHub Actions" |
| 107 | + branch: main |
| 108 | + file_pattern: pom.xml |
| 109 | + |
| 110 | + - name: Notify Dependency Issues |
| 111 | + if: failure() |
| 112 | + uses: dawidd6/action-send-mail@v3 |
| 113 | + with: |
| 114 | + server_address: smtp.example.com |
| 115 | + server_port: 587 |
| 116 | + username: ${{ secrets.SMTP_USERNAME }} |
| 117 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 118 | + subject: 'Maven Dependency Issues Detected' |
| 119 | + body: | |
| 120 | + The Maven dependency management has encountered issues. Auto-fix attempts have been made. |
| 121 | + Please review the dependency-analysis.txt and the updated pom.xml for details. |
| 122 | + to: your-email@example.com |
| 123 | + from: github-actions@example.com |
| 124 | + |
| 125 | + - name: Create GitHub Issue for Dependency Issues |
| 126 | + if: failure() |
| 127 | + uses: peter-evans/create-issue-from-file@v3 |
| 128 | + with: |
| 129 | + title: 'Maven Dependency Issues Detected' |
| 130 | + content-file: dependency-analysis.txt |
| 131 | + labels: dependency, issue |
| 132 | + |
| 133 | + # Job 3: Run Unit Tests |
| 134 | + unit-tests: |
| 135 | + needs: manage-dependencies |
| 136 | + runs-on: ubuntu-latest |
| 137 | + strategy: |
| 138 | + matrix: |
| 139 | + java-version: [8, 11, 17] |
| 140 | + name: Run Unit Tests on Java ${{ matrix.java-version }} |
| 141 | + steps: |
| 142 | + - name: Checkout Repository |
| 143 | + uses: actions/checkout@v3 |
| 144 | + |
| 145 | + - name: Set Up JDK ${{ matrix.java-version }} |
| 146 | + uses: actions/setup-java@v3 |
| 147 | + with: |
| 148 | + distribution: 'temurin' |
| 149 | + java-version: ${{ matrix.java-version }} |
| 150 | + cache: maven |
| 151 | + |
| 152 | + - name: Maven Test with Retries |
| 153 | + run: | |
| 154 | + for i in {1..3}; do |
| 155 | + mvn test -fn && break || sleep 15 |
| 156 | + done |
| 157 | +
|
| 158 | + - name: Upload Unit Test Results |
| 159 | + if: always() |
| 160 | + uses: actions/upload-artifact@v3 |
| 161 | + with: |
| 162 | + name: unit-test-results-java-${{ matrix.java-version }} |
| 163 | + path: target/surefire-reports/ |
| 164 | + |
| 165 | + - name: Notify Unit Test Issues |
| 166 | + if: failure() |
| 167 | + uses: dawidd6/action-send-mail@v3 |
| 168 | + with: |
| 169 | + server_address: smtp.example.com |
| 170 | + server_port: 587 |
| 171 | + username: ${{ secrets.SMTP_USERNAME }} |
| 172 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 173 | + subject: 'Unit Tests Encountered Issues' |
| 174 | + body: | |
| 175 | + The unit tests on Java ${{ matrix.java-version }} have completed with issues. Please check the workflow logs for details. |
| 176 | + to: your-email@example.com |
| 177 | + from: github-actions@example.com |
| 178 | + |
| 179 | + # Job 4: Run Integration Tests |
| 180 | + integration-tests: |
| 181 | + needs: manage-dependencies |
| 182 | + runs-on: ubuntu-latest |
| 183 | + name: Run Integration Tests |
| 184 | + steps: |
| 185 | + - name: Checkout Repository |
| 186 | + uses: actions/checkout@v3 |
| 187 | + |
| 188 | + - name: Set Up JDK 11 |
| 189 | + uses: actions/setup-java@v3 |
| 190 | + with: |
| 191 | + distribution: 'temurin' |
| 192 | + java-version: '11' |
| 193 | + cache: maven |
| 194 | + |
| 195 | + - name: Maven Integration Test with Retries |
| 196 | + run: | |
| 197 | + for i in {1..3}; do |
| 198 | + mvn verify -P integration-tests -fn && break || sleep 15 |
| 199 | + done |
| 200 | +
|
| 201 | + - name: Upload Integration Test Results |
| 202 | + if: always() |
| 203 | + uses: actions/upload-artifact@v3 |
| 204 | + with: |
| 205 | + name: integration-test-results |
| 206 | + path: target/failsafe-reports/ |
| 207 | + |
| 208 | + - name: Notify Integration Test Issues |
| 209 | + if: failure() |
| 210 | + uses: dawidd6/action-send-mail@v3 |
| 211 | + with: |
| 212 | + server_address: smtp.example.com |
| 213 | + server_port: 587 |
| 214 | + username: ${{ secrets.SMTP_USERNAME }} |
| 215 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 216 | + subject: 'Integration Tests Encountered Issues' |
| 217 | + body: | |
| 218 | + The integration tests have completed with issues. Please check the workflow logs for details. |
| 219 | + to: your-email@example.com |
| 220 | + from: github-actions@example.com |
| 221 | + |
| 222 | + # Job 5: Code Analysis |
| 223 | + code-analysis: |
| 224 | + needs: manage-dependencies |
| 225 | + runs-on: ubuntu-latest |
| 226 | + name: Static Code Analysis |
| 227 | + steps: |
| 228 | + - name: Checkout Repository |
| 229 | + uses: actions/checkout@v3 |
| 230 | + |
| 231 | + - name: Set Up JDK 11 |
| 232 | + uses: actions/setup-java@v3 |
| 233 | + with: |
| 234 | + distribution: 'temurin' |
| 235 | + java-version: '11' |
| 236 | + |
| 237 | + - name: Maven Code Analysis with SpotBugs |
| 238 | + run: mvn spotbugs:check -fn |
| 239 | + continue-on-error: true # Allows this step to fail without failing the job |
| 240 | + |
| 241 | + - name: Upload SpotBugs Report |
| 242 | + if: always() |
| 243 | + uses: actions/upload-artifact@v3 |
| 244 | + with: |
| 245 | + name: spotbugs-report |
| 246 | + path: target/spotbugs/ |
| 247 | + |
| 248 | + - name: Notify Code Analysis Issues |
| 249 | + if: failure() |
| 250 | + uses: dawidd6/action-send-mail@v3 |
| 251 | + with: |
| 252 | + server_address: smtp.example.com |
| 253 | + server_port: 587 |
| 254 | + username: ${{ secrets.SMTP_USERNAME }} |
| 255 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 256 | + subject: 'Code Analysis Encountered Issues' |
| 257 | + body: | |
| 258 | + The static code analysis has completed with issues. Please check the workflow logs for details. |
| 259 | + to: your-email@example.com |
| 260 | + from: github-actions@example.com |
| 261 | + |
| 262 | + # Job 6: Package the Application |
| 263 | + package: |
| 264 | + needs: [unit-tests, integration-tests, code-analysis] |
| 265 | + runs-on: ubuntu-latest |
| 266 | + name: Package Application |
| 267 | + steps: |
| 268 | + - name: Checkout Repository |
| 269 | + uses: actions/checkout@v3 |
| 270 | + |
| 271 | + - name: Set Up JDK 11 |
| 272 | + uses: actions/setup-java@v3 |
| 273 | + with: |
| 274 | + distribution: 'temurin' |
| 275 | + java-version: '11' |
| 276 | + cache: maven |
| 277 | + |
| 278 | + - name: Maven Package |
| 279 | + run: mvn package -fn |
| 280 | + |
| 281 | + - name: Upload Package Artifact |
| 282 | + if: always() |
| 283 | + uses: actions/upload-artifact@v3 |
| 284 | + with: |
| 285 | + name: packaged-artifact |
| 286 | + path: target/*.jar |
| 287 | + |
| 288 | + - name: Notify Packaging Issues |
| 289 | + if: failure() |
| 290 | + uses: dawidd6/action-send-mail@v3 |
| 291 | + with: |
| 292 | + server_address: smtp.example.com |
| 293 | + server_port: 587 |
| 294 | + username: ${{ secrets.SMTP_USERNAME }} |
| 295 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 296 | + subject: 'Packaging Encountered Issues' |
| 297 | + body: | |
| 298 | + The packaging step has completed with issues. Please check the workflow logs for details. |
| 299 | + to: your-email@example.com |
| 300 | + from: github-actions@example.com |
| 301 | + |
| 302 | + # Job 7: Deploy the Application |
| 303 | + deploy: |
| 304 | + needs: package |
| 305 | + runs-on: ubuntu-latest |
| 306 | + name: Deploy Application |
| 307 | + steps: |
| 308 | + - name: Checkout Repository |
| 309 | + uses: actions/checkout@v3 |
| 310 | + |
| 311 | + - name: Set Up JDK 11 |
| 312 | + uses: actions/setup-java@v3 |
| 313 | + with: |
| 314 | + distribution: 'temurin' |
| 315 | + java-version: '11' |
| 316 | + |
| 317 | + - name: Download Packaged Artifact |
| 318 | + uses: actions/download-artifact@v3 |
| 319 | + with: |
| 320 | + name: packaged-artifact |
| 321 | + path: deploy/ |
| 322 | + |
| 323 | + - name: Deploy to Server |
| 324 | + env: |
| 325 | + DEPLOYMENT_SERVER: ${{ secrets.DEPLOYMENT_SERVER }} |
| 326 | + DEPLOYMENT_USER: ${{ secrets.DEPLOYMENT_USER }} |
| 327 | + DEPLOYMENT_KEY: ${{ secrets.DEPLOYMENT_KEY }} |
| 328 | + run: | |
| 329 | + # Ensure SSH key has proper permissions |
| 330 | + mkdir -p ~/.ssh |
| 331 | + echo "$DEPLOYMENT_KEY" > ~/.ssh/id_rsa |
| 332 | + chmod 600 ~/.ssh/id_rsa |
| 333 | +
|
| 334 | + # Add server to known hosts to prevent prompt |
| 335 | + ssh-keyscan $DEPLOYMENT_SERVER >> ~/.ssh/known_hosts |
| 336 | +
|
| 337 | + # Securely copy the JAR to the deployment server |
| 338 | + scp deploy/*.jar $DEPLOYMENT_USER@$DEPLOYMENT_SERVER:/path/to/deploy/ |
| 339 | +
|
| 340 | + # Execute deployment commands on the server |
| 341 | + ssh -i ~/.ssh/id_rsa $DEPLOYMENT_USER@$DEPLOYMENT_SERVER 'bash -s' <<'ENDSSH' |
| 342 | + cd /path/to/deploy/ |
| 343 | + pkill -f your-application.jar || true |
| 344 | + nohup java -jar your-application.jar > application.log 2>&1 & |
| 345 | + ENDSSH || echo "Deployment commands failed, but continuing" |
| 346 | +
|
| 347 | + - name: Notify Deployment Success |
| 348 | + if: success() |
| 349 | + uses: dawidd6/action-send-mail@v3 |
| 350 | + with: |
| 351 | + server_address: smtp.example.com |
| 352 | + server_port: 587 |
| 353 | + username: ${{ secrets.SMTP_USERNAME }} |
| 354 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 355 | + subject: 'Deployment Successful' |
| 356 | + body: | |
| 357 | + The application has been successfully deployed to ${{ secrets.DEPLOYMENT_SERVER }}. |
| 358 | + to: your-email@example.com |
| 359 | + from: github-actions@example.com |
| 360 | + |
| 361 | + - name: Notify Deployment Failure |
| 362 | + if: failure() |
| 363 | + uses: dawidd6/action-send-mail@v3 |
| 364 | + with: |
| 365 | + server_address: smtp.example.com |
| 366 | + server_port: 587 |
| 367 | + username: ${{ secrets.SMTP_USERNAME }} |
| 368 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 369 | + subject: 'Deployment Failed' |
| 370 | + body: | |
| 371 | + The deployment to ${{ secrets.DEPLOYMENT_SERVER }} has failed. Please check the workflow logs for details. |
| 372 | + to: your-email@example.com |
| 373 | + from: github-actions@example.com |
0 commit comments