|
| 1 | +name: Resilient Maven Workflow |
| 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 | + |
| 11 | +jobs: |
| 12 | + build: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + name: Build Project |
| 15 | + steps: |
| 16 | + - name: Checkout Repository |
| 17 | + uses: actions/checkout@v3 |
| 18 | + |
| 19 | + - name: Set Up JDK 11 |
| 20 | + uses: actions/setup-java@v3 |
| 21 | + with: |
| 22 | + distribution: 'temurin' |
| 23 | + java-version: '11' |
| 24 | + cache: maven |
| 25 | + |
| 26 | + - name: Display Java Version |
| 27 | + run: java -version |
| 28 | + |
| 29 | + - name: Maven Clean and Compile |
| 30 | + run: mvn clean compile -fn || echo "Maven compile failed, but continuing" |
| 31 | + |
| 32 | + - name: Upload Build Logs |
| 33 | + if: always() |
| 34 | + uses: actions/upload-artifact@v3 |
| 35 | + with: |
| 36 | + name: build-logs |
| 37 | + path: target/build.log |
| 38 | + |
| 39 | + - name: Notify Build Issues |
| 40 | + if: failure() |
| 41 | + uses: dawidd6/action-send-mail@v3 |
| 42 | + with: |
| 43 | + server_address: smtp.example.com |
| 44 | + server_port: 587 |
| 45 | + username: ${{ secrets.SMTP_USERNAME }} |
| 46 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 47 | + subject: 'Build Job Encountered Issues' |
| 48 | + body: | |
| 49 | + The build job has completed with issues. Please check the workflow logs for details. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + unit-tests: |
| 54 | + needs: build |
| 55 | + runs-on: ubuntu-latest |
| 56 | + strategy: |
| 57 | + matrix: |
| 58 | + java-version: [8, 11, 17] |
| 59 | + name: Run Unit Tests on Java ${{ matrix.java-version }} |
| 60 | + steps: |
| 61 | + - name: Checkout Repository |
| 62 | + uses: actions/checkout@v3 |
| 63 | + |
| 64 | + - name: Set Up JDK ${{ matrix.java-version }} |
| 65 | + uses: actions/setup-java@v3 |
| 66 | + with: |
| 67 | + distribution: 'temurin' |
| 68 | + java-version: ${{ matrix.java-version }} |
| 69 | + cache: maven |
| 70 | + |
| 71 | + - name: Maven Test |
| 72 | + run: mvn test -fn || echo "Maven tests failed, but continuing" |
| 73 | + |
| 74 | + - name: Upload Unit Test Results |
| 75 | + if: always() |
| 76 | + uses: actions/upload-artifact@v3 |
| 77 | + with: |
| 78 | + name: unit-test-results-java-${{ matrix.java-version }} |
| 79 | + path: target/surefire-reports/ |
| 80 | + |
| 81 | + - name: Notify Unit Test Issues |
| 82 | + if: failure() |
| 83 | + uses: dawidd6/action-send-mail@v3 |
| 84 | + with: |
| 85 | + server_address: smtp.example.com |
| 86 | + server_port: 587 |
| 87 | + username: ${{ secrets.SMTP_USERNAME }} |
| 88 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 89 | + subject: 'Unit Tests Encountered Issues' |
| 90 | + body: | |
| 91 | + The unit tests on Java ${{ matrix.java-version }} have completed with issues. Please check the workflow logs for details. |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + integration-tests: |
| 96 | + needs: build |
| 97 | + runs-on: ubuntu-latest |
| 98 | + name: Run Integration Tests |
| 99 | + steps: |
| 100 | + - name: Checkout Repository |
| 101 | + uses: actions/checkout@v3 |
| 102 | + |
| 103 | + - name: Set Up JDK 11 |
| 104 | + uses: actions/setup-java@v3 |
| 105 | + with: |
| 106 | + distribution: 'temurin' |
| 107 | + java-version: '11' |
| 108 | + cache: maven |
| 109 | + |
| 110 | + - name: Maven Integration Test |
| 111 | + run: mvn verify -P integration-tests -fn || echo "Integration tests failed, but continuing" |
| 112 | + |
| 113 | + - name: Upload Integration Test Results |
| 114 | + if: always() |
| 115 | + uses: actions/upload-artifact@v3 |
| 116 | + with: |
| 117 | + name: integration-test-results |
| 118 | + path: target/failsafe-reports/ |
| 119 | + |
| 120 | + - name: Notify Integration Test Issues |
| 121 | + if: failure() |
| 122 | + uses: dawidd6/action-send-mail@v3 |
| 123 | + with: |
| 124 | + server_address: smtp.example.com |
| 125 | + server_port: 587 |
| 126 | + username: ${{ secrets.SMTP_USERNAME }} |
| 127 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 128 | + subject: 'Integration Tests Encountered Issues' |
| 129 | + body: | |
| 130 | + The integration tests have completed with issues. Please check the workflow logs for details. |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + code-analysis: |
| 135 | + needs: build |
| 136 | + runs-on: ubuntu-latest |
| 137 | + name: Static Code Analysis |
| 138 | + steps: |
| 139 | + - name: Checkout Repository |
| 140 | + uses: actions/checkout@v3 |
| 141 | + |
| 142 | + - name: Set Up JDK 11 |
| 143 | + uses: actions/setup-java@v3 |
| 144 | + with: |
| 145 | + distribution: 'temurin' |
| 146 | + java-version: '11' |
| 147 | + |
| 148 | + - name: Maven Code Analysis with SpotBugs |
| 149 | + run: mvn spotbugs:check -fn || echo "Code analysis failed, but continuing" |
| 150 | + continue-on-error: true # Allows this step to fail without failing the job |
| 151 | + |
| 152 | + - name: Upload SpotBugs Report |
| 153 | + if: always() |
| 154 | + uses: actions/upload-artifact@v3 |
| 155 | + with: |
| 156 | + name: spotbugs-report |
| 157 | + path: target/spotbugs/ |
| 158 | + |
| 159 | + - name: Notify Code Analysis Issues |
| 160 | + if: failure() |
| 161 | + uses: dawidd6/action-send-mail@v3 |
| 162 | + with: |
| 163 | + server_address: smtp.example.com |
| 164 | + server_port: 587 |
| 165 | + username: ${{ secrets.SMTP_USERNAME }} |
| 166 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 167 | + subject: 'Code Analysis Encountered Issues' |
| 168 | + body: | |
| 169 | + The static code analysis has completed with issues. Please check the workflow logs for details. |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + package: |
| 174 | + needs: [unit-tests, integration-tests, code-analysis] |
| 175 | + runs-on: ubuntu-latest |
| 176 | + name: Package Application |
| 177 | + steps: |
| 178 | + - name: Checkout Repository |
| 179 | + uses: actions/checkout@v3 |
| 180 | + |
| 181 | + - name: Set Up JDK 11 |
| 182 | + uses: actions/setup-java@v3 |
| 183 | + with: |
| 184 | + distribution: 'temurin' |
| 185 | + java-version: '11' |
| 186 | + cache: maven |
| 187 | + |
| 188 | + - name: Maven Package |
| 189 | + run: mvn package -fn || echo "Packaging failed, but continuing" |
| 190 | + |
| 191 | + - name: Upload Package Artifact |
| 192 | + if: always() |
| 193 | + uses: actions/upload-artifact@v3 |
| 194 | + with: |
| 195 | + name: packaged-artifact |
| 196 | + path: target/*.jar |
| 197 | + |
| 198 | + - name: Notify Packaging Issues |
| 199 | + if: failure() |
| 200 | + uses: dawidd6/action-send-mail@v3 |
| 201 | + with: |
| 202 | + server_address: smtp.example.com |
| 203 | + server_port: 587 |
| 204 | + username: ${{ secrets.SMTP_USERNAME }} |
| 205 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 206 | + subject: 'Packaging Encountered Issues' |
| 207 | + body: | |
| 208 | + The packaging step has completed with issues. Please check the workflow logs for details. |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + deploy: |
| 213 | + needs: package |
| 214 | + runs-on: ubuntu-latest |
| 215 | + name: Deploy Application |
| 216 | + steps: |
| 217 | + - name: Checkout Repository |
| 218 | + uses: actions/checkout@v3 |
| 219 | + |
| 220 | + - name: Set Up JDK 11 |
| 221 | + uses: actions/setup-java@v3 |
| 222 | + with: |
| 223 | + distribution: 'temurin' |
| 224 | + java-version: '11' |
| 225 | + |
| 226 | + - name: Download Packaged Artifact |
| 227 | + uses: actions/download-artifact@v3 |
| 228 | + with: |
| 229 | + name: packaged-artifact |
| 230 | + path: deploy/ |
| 231 | + |
| 232 | + - name: Deploy to Server |
| 233 | + env: |
| 234 | + DEPLOYMENT_SERVER: ${{ secrets.DEPLOYMENT_SERVER }} |
| 235 | + DEPLOYMENT_USER: ${{ secrets.DEPLOYMENT_USER }} |
| 236 | + DEPLOYMENT_KEY: ${{ secrets.DEPLOYMENT_KEY }} |
| 237 | + run: | |
| 238 | + # Ensure SSH key has proper permissions |
| 239 | + mkdir -p ~/.ssh |
| 240 | + echo "$DEPLOYMENT_KEY" > ~/.ssh/id_rsa |
| 241 | + chmod 600 ~/.ssh/id_rsa |
| 242 | +
|
| 243 | + # Add server to known hosts to prevent prompt |
| 244 | + ssh-keyscan $DEPLOYMENT_SERVER >> ~/.ssh/known_hosts |
| 245 | +
|
| 246 | + # Securely copy the JAR to the deployment server |
| 247 | + scp deploy/*.jar $DEPLOYMENT_USER@$DEPLOYMENT_SERVER:/path/to/deploy/ |
| 248 | +
|
| 249 | + # Execute deployment commands on the server |
| 250 | + ssh -i ~/.ssh/id_rsa $DEPLOYMENT_USER@$DEPLOYMENT_SERVER 'bash -s' <<'ENDSSH' |
| 251 | + cd /path/to/deploy/ |
| 252 | + pkill -f your-application.jar || true |
| 253 | + nohup java -jar your-application.jar > application.log 2>&1 & |
| 254 | + ENDSSH || echo "Deployment commands failed, but continuing" |
| 255 | +
|
| 256 | + - name: Notify Deployment Success |
| 257 | + if: success() |
| 258 | + uses: dawidd6/action-send-mail@v3 |
| 259 | + with: |
| 260 | + server_address: smtp.example.com |
| 261 | + server_port: 587 |
| 262 | + username: ${{ secrets.SMTP_USERNAME }} |
| 263 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 264 | + subject: 'Deployment Successful' |
| 265 | + body: | |
| 266 | + The application has been successfully deployed to ${{ secrets.DEPLOYMENT_SERVER }}. |
| 267 | + |
| 268 | + |
| 269 | + |
| 270 | + - name: Notify Deployment Failure |
| 271 | + if: failure() |
| 272 | + uses: dawidd6/action-send-mail@v3 |
| 273 | + with: |
| 274 | + server_address: smtp.example.com |
| 275 | + server_port: 587 |
| 276 | + username: ${{ secrets.SMTP_USERNAME }} |
| 277 | + password: ${{ secrets.SMTP_PASSWORD }} |
| 278 | + subject: 'Deployment Failed' |
| 279 | + body: | |
| 280 | + The deployment to ${{ secrets.DEPLOYMENT_SERVER }} has failed. Please check the workflow logs for details. |
| 281 | + |
| 282 | + |
0 commit comments