Skip to content

Commit e7ca417

Browse files
Update test-and-email.yml
1 parent 0d5014a commit e7ca417

File tree

1 file changed

+72
-10
lines changed

1 file changed

+72
-10
lines changed

.github/workflows/test-and-email.yml

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ on:
88
jobs:
99
test:
1010
runs-on: ubuntu-latest
11+
outputs:
12+
total: ${{ steps.test-summary.outputs.total }}
13+
passed: ${{ steps.test-summary.outputs.passed }}
14+
failed: ${{ steps.test-summary.outputs.failed }}
15+
skipped: ${{ steps.test-summary.outputs.skipped }}
16+
duration: ${{ steps.start-end.outputs.duration }}
1117

1218
steps:
19+
- name: Start time
20+
id: start-end
21+
run: echo "start_time=$(date +%s)" >> $GITHUB_OUTPUT
22+
1323
- name: Checkout code
1424
uses: actions/checkout@v4
1525

@@ -28,6 +38,7 @@ jobs:
2838
${{ runner.os }}-maven-
2939
3040
- name: Build and test with Maven
41+
id: maven
3142
run: mvn clean test
3243

3344
- name: Archive test results
@@ -37,7 +48,29 @@ jobs:
3748
name: test-results
3849
path: target/surefire-reports/
3950

40-
- name: Send email with test summary
51+
- name: Summarize test results
52+
id: test-summary
53+
run: |
54+
total=$(grep -h 'testsuite' target/surefire-reports/*.xml | awk -F'"' '{print $4}' | paste -sd+ - | bc)
55+
failed=$(grep -h 'testsuite' target/surefire-reports/*.xml | awk -F'"' '{print $8}' | paste -sd+ - | bc)
56+
skipped=$(grep -h 'testsuite' target/surefire-reports/*.xml | awk -F'"' '{print $12}' | paste -sd+ - | bc)
57+
passed=$((total - failed - skipped))
58+
echo "total=$total" >> $GITHUB_OUTPUT
59+
echo "passed=$passed" >> $GITHUB_OUTPUT
60+
echo "failed=$failed" >> $GITHUB_OUTPUT
61+
echo "skipped=$skipped" >> $GITHUB_OUTPUT
62+
63+
- name: End time and duration
64+
id: end-time
65+
run: |
66+
end_time=$(date +%s)
67+
start_time=${{ steps.start-end.outputs.start_time }}
68+
duration=$((end_time - start_time))
69+
min=$((duration / 60))
70+
sec=$((duration % 60))
71+
echo "duration=${min}m ${sec}s" >> $GITHUB_OUTPUT
72+
73+
- name: Send beautiful email with test summary
4174
if: always()
4275
uses: dawidd6/action-send-mail@v3
4376
with:
@@ -50,13 +83,42 @@ jobs:
5083
from: ${{ secrets.MAIL_USERNAME }}
5184
content_type: text/html
5285
body: |
53-
<h2>Test Results for ${{ github.repository }} (Run #${{ github.run_number }})</h2>
54-
<p>Workflow: <a href="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}">View Run Logs</a></p>
55-
<p>Status: <b>${{ job.status }}</b></p>
56-
<p>Commit: ${{ github.sha }}</p>
57-
<p>Branch: ${{ github.ref_name }}</p>
58-
<p>Summary of test results:</p>
59-
<pre>
60-
${{ steps.test.outputs }}
61-
</pre>
86+
<html>
87+
<body style="font-family: Arial, sans-serif; background:#f5f5f5; padding:20px;">
88+
<div style="max-width:600px; margin:auto; background:white; border-radius:8px; box-shadow:0 2px 8px #ccc; padding:24px;">
89+
<h2 style="color:#336699;">🚀 Test Results for <span style="color:#222;">${{ github.repository }}</span> (Run #${{ github.run_number }})</h2>
90+
<p>
91+
<strong>Workflow:</strong> <a href="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}">View Run Logs</a><br>
92+
<strong>Status:</strong> <span style="color:${{ job.status == 'success' && 'green' || 'red' }};"><b>${{ job.status == 'success' && '✅ Success' || '❌ Failure' }}</b></span><br>
93+
<strong>Commit:</strong> <a href="${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}">${{ github.sha }}</a><br>
94+
<strong>Branch:</strong> <span style="color:#555;">${{ github.ref_name }}</span><br>
95+
<strong>Author:</strong> ${{ github.actor }}<br>
96+
<strong>Started:</strong> ${{ steps.start-end.outputs.start_time }}<br>
97+
<strong>Duration:</strong> ${{ steps.end-time.outputs.duration }}
98+
</p>
99+
<hr>
100+
<h3 style="color:#336699;">Test Results</h3>
101+
<table style="width:100%; border-collapse:collapse;">
102+
<thead>
103+
<tr style="background:#eef;">
104+
<th style="border:1px solid #ccc; padding:8px;">Total</th>
105+
<th style="border:1px solid #ccc; padding:8px;">Passed</th>
106+
<th style="border:1px solid #ccc; padding:8px;">Failed</th>
107+
<th style="border:1px solid #ccc; padding:8px;">Skipped</th>
108+
</tr>
109+
</thead>
110+
<tbody>
111+
<tr>
112+
<td style="border:1px solid #ccc; padding:8px;">${{ steps.test-summary.outputs.total }}</td>
113+
<td style="border:1px solid #ccc; padding:8px; color:green;">${{ steps.test-summary.outputs.passed }}</td>
114+
<td style="border:1px solid #ccc; padding:8px; color:${{ steps.test-summary.outputs.failed == '0' && 'green' || 'red' }};">${{ steps.test-summary.outputs.failed }}</td>
115+
<td style="border:1px solid #ccc; padding:8px; color:#555;">${{ steps.test-summary.outputs.skipped }}</td>
116+
</tr>
117+
</tbody>
118+
</table>
119+
<hr>
120+
<p style="color:#777;">You are receiving this email because you requested notifications for workflow runs on <b>${{ github.repository }}</b>.</p>
121+
</div>
122+
</body>
123+
</html>
62124
attachments: target/surefire-reports/*.xml

0 commit comments

Comments
 (0)