Maven Magic Matrix Multi-Job Workflow #85
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Maven Magic Matrix Multi-Job Workflow | |
on: | |
push: | |
branches: [ master ] | |
pull_request: | |
branches: [ master ] | |
schedule: | |
- cron: '0 3 * * *' # Runs daily at 3 AM UTC | |
jobs: | |
# Job 1: Build the project | |
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 | |
# Job 2: Run Unit Tests | |
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 | |
- name: Upload Unit Test Results | |
uses: actions/upload-artifact@v3 | |
with: | |
name: unit-test-results-java-${{ matrix.java-version }} | |
path: target/surefire-reports/ | |
# Job 3: Run Integration Tests | |
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 | |
- name: Upload Integration Test Results | |
uses: actions/upload-artifact@v3 | |
with: | |
name: integration-test-results | |
path: target/failsafe-reports/ | |
# Job 4: Code Analysis (Parallel with Test Jobs) | |
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 | |
continue-on-error: true # Allow this step to fail without failing the job | |
- name: Upload SpotBugs Report | |
if: always() # Ensure this step runs even if the previous step failed | |
uses: actions/upload-artifact@v3 | |
with: | |
name: spotbugs-report | |
path: target/spotbugs/ | |
# Job 5: Package the Application | |
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 | |
- name: Upload Package Artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: packaged-artifact | |
path: target/*.jar | |
# Job 6: Deploy the Application | |
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 | |
- 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 |