Skip to content

Resilient Maven Workflow #83

Resilient Maven Workflow

Resilient Maven Workflow #83

name: Resilient Maven Workflow
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '0 3 * * *' # Runs daily at 3 AM UTC
jobs:
build:
runs-on: ubuntu-latest
name: Build Project
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set Up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
cache: maven
- name: Display Java Version
run: java -version
- name: Maven Clean and Compile
run: mvn clean compile -fn || echo "Maven compile failed, but continuing"
- name: Upload Build Logs
if: always()
uses: actions/upload-artifact@v3
with:
name: build-logs
path: target/build.log
- name: Notify Build Issues
if: failure()
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.example.com
server_port: 587
username: ${{ secrets.SMTP_USERNAME }}
password: ${{ secrets.SMTP_PASSWORD }}
subject: 'Build Job Encountered Issues'
body: |
The build job has completed with issues. Please check the workflow logs for details.
to: your-email@example.com
from: github-actions@example.com
unit-tests:
needs: build
runs-on: ubuntu-latest
strategy:
matrix:
java-version: [8, 11, 17]
name: Run Unit Tests on Java ${{ matrix.java-version }}
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set Up JDK ${{ matrix.java-version }}
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: ${{ matrix.java-version }}
cache: maven
- name: Maven Test
run: mvn test -fn || echo "Maven tests failed, but continuing"
- name: Upload Unit Test Results
if: always()
uses: actions/upload-artifact@v3
with:
name: unit-test-results-java-${{ matrix.java-version }}
path: target/surefire-reports/
- name: Notify Unit Test Issues
if: failure()
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.example.com
server_port: 587
username: ${{ secrets.SMTP_USERNAME }}
password: ${{ secrets.SMTP_PASSWORD }}
subject: 'Unit Tests Encountered Issues'
body: |
The unit tests on Java ${{ matrix.java-version }} have completed with issues. Please check the workflow logs for details.
to: your-email@example.com
from: github-actions@example.com
integration-tests:
needs: build
runs-on: ubuntu-latest
name: Run Integration Tests
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set Up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
cache: maven
- name: Maven Integration Test
run: mvn verify -P integration-tests -fn || echo "Integration tests failed, but continuing"
- name: Upload Integration Test Results
if: always()
uses: actions/upload-artifact@v3
with:
name: integration-test-results
path: target/failsafe-reports/
- name: Notify Integration Test Issues
if: failure()
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.example.com
server_port: 587
username: ${{ secrets.SMTP_USERNAME }}
password: ${{ secrets.SMTP_PASSWORD }}
subject: 'Integration Tests Encountered Issues'
body: |
The integration tests have completed with issues. Please check the workflow logs for details.
to: your-email@example.com
from: github-actions@example.com
code-analysis:
needs: build
runs-on: ubuntu-latest
name: Static Code Analysis
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set Up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
- name: Maven Code Analysis with SpotBugs
run: mvn spotbugs:check -fn || echo "Code analysis failed, but continuing"
continue-on-error: true # Allows this step to fail without failing the job
- name: Upload SpotBugs Report
if: always()
uses: actions/upload-artifact@v3
with:
name: spotbugs-report
path: target/spotbugs/
- name: Notify Code Analysis Issues
if: failure()
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.example.com
server_port: 587
username: ${{ secrets.SMTP_USERNAME }}
password: ${{ secrets.SMTP_PASSWORD }}
subject: 'Code Analysis Encountered Issues'
body: |
The static code analysis has completed with issues. Please check the workflow logs for details.
to: your-email@example.com
from: github-actions@example.com
package:
needs: [unit-tests, integration-tests, code-analysis]
runs-on: ubuntu-latest
name: Package Application
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set Up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
cache: maven
- name: Maven Package
run: mvn package -fn || echo "Packaging failed, but continuing"
- name: Upload Package Artifact
if: always()
uses: actions/upload-artifact@v3
with:
name: packaged-artifact
path: target/*.jar
- name: Notify Packaging Issues
if: failure()
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.example.com
server_port: 587
username: ${{ secrets.SMTP_USERNAME }}
password: ${{ secrets.SMTP_PASSWORD }}
subject: 'Packaging Encountered Issues'
body: |
The packaging step has completed with issues. Please check the workflow logs for details.
to: your-email@example.com
from: github-actions@example.com
deploy:
needs: package
runs-on: ubuntu-latest
name: Deploy Application
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set Up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
- name: Download Packaged Artifact
uses: actions/download-artifact@v3
with:
name: packaged-artifact
path: deploy/
- name: Deploy to Server
env:
DEPLOYMENT_SERVER: ${{ secrets.DEPLOYMENT_SERVER }}
DEPLOYMENT_USER: ${{ secrets.DEPLOYMENT_USER }}
DEPLOYMENT_KEY: ${{ secrets.DEPLOYMENT_KEY }}
run: |
# Ensure SSH key has proper permissions
mkdir -p ~/.ssh
echo "$DEPLOYMENT_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
# Add server to known hosts to prevent prompt
ssh-keyscan $DEPLOYMENT_SERVER >> ~/.ssh/known_hosts
# Securely copy the JAR to the deployment server
scp deploy/*.jar $DEPLOYMENT_USER@$DEPLOYMENT_SERVER:/path/to/deploy/
# Execute deployment commands on the server
ssh -i ~/.ssh/id_rsa $DEPLOYMENT_USER@$DEPLOYMENT_SERVER 'bash -s' <<'ENDSSH'
cd /path/to/deploy/
pkill -f your-application.jar || true
nohup java -jar your-application.jar > application.log 2>&1 &
ENDSSH || echo "Deployment commands failed, but continuing"
- name: Notify Deployment Success
if: success()
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.example.com
server_port: 587
username: ${{ secrets.SMTP_USERNAME }}
password: ${{ secrets.SMTP_PASSWORD }}
subject: 'Deployment Successful'
body: |
The application has been successfully deployed to ${{ secrets.DEPLOYMENT_SERVER }}.
to: your-email@example.com
from: github-actions@example.com
- name: Notify Deployment Failure
if: failure()
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.example.com
server_port: 587
username: ${{ secrets.SMTP_USERNAME }}
password: ${{ secrets.SMTP_PASSWORD }}
subject: 'Deployment Failed'
body: |
The deployment to ${{ secrets.DEPLOYMENT_SERVER }} has failed. Please check the workflow logs for details.
to: your-email@example.com
from: github-actions@example.com