Skip to content

Commit e629526

Browse files
authored
Create maven-magic-matrix.yml
1 parent c0fbe7e commit e629526

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed
+208
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
name: Maven Multi-Job Workflow
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
schedule:
9+
- cron: '0 3 * * *' # Runs daily at 3 AM UTC
10+
11+
jobs:
12+
# Job 1: Build the project
13+
build:
14+
runs-on: ubuntu-latest
15+
name: Build Project
16+
steps:
17+
- name: Checkout Repository
18+
uses: actions/checkout@v3
19+
20+
- name: Set Up JDK 11
21+
uses: actions/setup-java@v3
22+
with:
23+
distribution: 'temurin'
24+
java-version: '11'
25+
cache: maven
26+
27+
- name: Display Java Version
28+
run: java -version
29+
30+
- name: Maven Clean and Compile
31+
run: mvn clean compile
32+
33+
# Job 2: Run Unit Tests
34+
unit-tests:
35+
needs: build
36+
runs-on: ubuntu-latest
37+
strategy:
38+
matrix:
39+
java-version: [8, 11, 17]
40+
name: Run Unit Tests on Java ${{ matrix.java-version }}
41+
steps:
42+
- name: Checkout Repository
43+
uses: actions/checkout@v3
44+
45+
- name: Set Up JDK ${{ matrix.java-version }}
46+
uses: actions/setup-java@v3
47+
with:
48+
distribution: 'temurin'
49+
java-version: ${{ matrix.java-version }}
50+
cache: maven
51+
52+
- name: Maven Test
53+
run: mvn test
54+
55+
- name: Upload Unit Test Results
56+
uses: actions/upload-artifact@v3
57+
with:
58+
name: unit-test-results-java-${{ matrix.java-version }}
59+
path: target/surefire-reports/
60+
61+
# Job 3: Run Integration Tests
62+
integration-tests:
63+
needs: build
64+
runs-on: ubuntu-latest
65+
name: Run Integration Tests
66+
steps:
67+
- name: Checkout Repository
68+
uses: actions/checkout@v3
69+
70+
- name: Set Up JDK 11
71+
uses: actions/setup-java@v3
72+
with:
73+
distribution: 'temurin'
74+
java-version: '11'
75+
cache: maven
76+
77+
- name: Maven Integration Test
78+
run: mvn verify -P integration-tests
79+
80+
- name: Upload Integration Test Results
81+
uses: actions/upload-artifact@v3
82+
with:
83+
name: integration-test-results
84+
path: target/failsafe-reports/
85+
86+
# Job 4: Code Analysis (Parallel with Test Jobs)
87+
code-analysis:
88+
needs: build
89+
runs-on: ubuntu-latest
90+
name: Static Code Analysis
91+
steps:
92+
- name: Checkout Repository
93+
uses: actions/checkout@v3
94+
95+
- name: Set Up JDK 11
96+
uses: actions/setup-java@v3
97+
with:
98+
distribution: 'temurin'
99+
java-version: '11'
100+
101+
- name: Maven Code Analysis with SpotBugs
102+
run: mvn spotbugs:check
103+
continue-on-error: true # Allow this step to fail without failing the job
104+
105+
- name: Upload SpotBugs Report
106+
if: always() # Ensure this step runs even if the previous step failed
107+
uses: actions/upload-artifact@v3
108+
with:
109+
name: spotbugs-report
110+
path: target/spotbugs/
111+
112+
# Job 5: Package the Application
113+
package:
114+
needs: [unit-tests, integration-tests, code-analysis]
115+
runs-on: ubuntu-latest
116+
name: Package Application
117+
steps:
118+
- name: Checkout Repository
119+
uses: actions/checkout@v3
120+
121+
- name: Set Up JDK 11
122+
uses: actions/setup-java@v3
123+
with:
124+
distribution: 'temurin'
125+
java-version: '11'
126+
cache: maven
127+
128+
- name: Maven Package
129+
run: mvn package
130+
131+
- name: Upload Package Artifact
132+
uses: actions/upload-artifact@v3
133+
with:
134+
name: packaged-artifact
135+
path: target/*.jar
136+
137+
# Job 6: Deploy the Application
138+
deploy:
139+
needs: package
140+
runs-on: ubuntu-latest
141+
name: Deploy Application
142+
steps:
143+
- name: Checkout Repository
144+
uses: actions/checkout@v3
145+
146+
- name: Set Up JDK 11
147+
uses: actions/setup-java@v3
148+
with:
149+
distribution: 'temurin'
150+
java-version: '11'
151+
152+
- name: Download Packaged Artifact
153+
uses: actions/download-artifact@v3
154+
with:
155+
name: packaged-artifact
156+
path: deploy/
157+
158+
- name: Deploy to Server
159+
env:
160+
DEPLOYMENT_SERVER: ${{ secrets.DEPLOYMENT_SERVER }}
161+
DEPLOYMENT_USER: ${{ secrets.DEPLOYMENT_USER }}
162+
DEPLOYMENT_KEY: ${{ secrets.DEPLOYMENT_KEY }}
163+
run: |
164+
# Ensure SSH key has proper permissions
165+
mkdir -p ~/.ssh
166+
echo "$DEPLOYMENT_KEY" > ~/.ssh/id_rsa
167+
chmod 600 ~/.ssh/id_rsa
168+
169+
# Add server to known hosts to prevent prompt
170+
ssh-keyscan $DEPLOYMENT_SERVER >> ~/.ssh/known_hosts
171+
172+
# Securely copy the JAR to the deployment server
173+
scp deploy/*.jar $DEPLOYMENT_USER@$DEPLOYMENT_SERVER:/path/to/deploy/
174+
175+
# Execute deployment commands on the server
176+
ssh -i ~/.ssh/id_rsa $DEPLOYMENT_USER@$DEPLOYMENT_SERVER 'bash -s' <<'ENDSSH'
177+
cd /path/to/deploy/
178+
pkill -f your-application.jar || true
179+
nohup java -jar your-application.jar > application.log 2>&1 &
180+
ENDSSH
181+
182+
- name: Notify Deployment Success
183+
if: success()
184+
uses: dawidd6/action-send-mail@v3
185+
with:
186+
server_address: smtp.example.com
187+
server_port: 587
188+
username: ${{ secrets.SMTP_USERNAME }}
189+
password: ${{ secrets.SMTP_PASSWORD }}
190+
subject: 'Deployment Successful'
191+
body: |
192+
The application has been successfully deployed to ${{ secrets.DEPLOYMENT_SERVER }}.
193+
194+
195+
196+
- name: Notify Deployment Failure
197+
if: failure()
198+
uses: dawidd6/action-send-mail@v3
199+
with:
200+
server_address: smtp.example.com
201+
server_port: 587
202+
username: ${{ secrets.SMTP_USERNAME }}
203+
password: ${{ secrets.SMTP_PASSWORD }}
204+
subject: 'Deployment Failed'
205+
body: |
206+
The deployment to ${{ secrets.DEPLOYMENT_SERVER }} has failed. Please check the workflow logs for details.
207+
208+

0 commit comments

Comments
 (0)