Skip to content

Update test-and-email.yml #4

Update test-and-email.yml

Update test-and-email.yml #4

name: Test Spring Application and Send Email
on:
push:
branches:
- master
jobs:
test:
runs-on: ubuntu-latest
outputs:
total: ${{ steps.test-summary.outputs.total }}
passed: ${{ steps.test-summary.outputs.passed }}
failed: ${{ steps.test-summary.outputs.failed }}
skipped: ${{ steps.test-summary.outputs.skipped }}
duration: ${{ steps.end-time.outputs.duration }}
status: ${{ steps.set-status.outputs.status }}
steps:
- name: Start time
id: start-time
run: |
echo "start_time=$(date +%s)" >> $GITHUB_OUTPUT
echo "start_time_formatted=$(date '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_OUTPUT
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'
- name: Cache Maven packages
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Build and test with Maven
id: maven
run: mvn clean test
continue-on-error: true
- name: Archive test results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results
path: target/surefire-reports/
- name: Summarize test results
id: test-summary
if: always()
run: |
if [ -d "target/surefire-reports" ] && [ "$(ls -A target/surefire-reports/*.xml 2>/dev/null)" ]; then
total=$(grep -h 'testsuite' target/surefire-reports/*.xml | awk -F'"' '{print $4}' | paste -sd+ - | bc || echo "0")
failed=$(grep -h 'testsuite' target/surefire-reports/*.xml | awk -F'"' '{print $8}' | paste -sd+ - | bc || echo "0")
skipped=$(grep -h 'testsuite' target/surefire-reports/*.xml | awk -F'"' '{print $12}' | paste -sd+ - | bc || echo "0")
passed=$((total - failed - skipped))
else
total=0
failed=0
skipped=0
passed=0
fi
echo "total=$total" >> $GITHUB_OUTPUT
echo "passed=$passed" >> $GITHUB_OUTPUT
echo "failed=$failed" >> $GITHUB_OUTPUT
echo "skipped=$skipped" >> $GITHUB_OUTPUT
- name: End time and duration
id: end-time
if: always()
run: |
end_time=$(date +%s)
start_time=${{ steps.start-time.outputs.start_time }}
duration=$((end_time - start_time))
min=$((duration / 60))
sec=$((duration % 60))
echo "duration=${min}m ${sec}s" >> $GITHUB_OUTPUT
- name: Set job status
id: set-status
if: always()
run: |
if [ "${{ steps.maven.outcome }}" = "success" ]; then
echo "status=✅ Success" >> $GITHUB_OUTPUT
echo "status_color=green" >> $GITHUB_OUTPUT
else
echo "status=❌ Failure" >> $GITHUB_OUTPUT
echo "status_color=red" >> $GITHUB_OUTPUT
fi
- name: Send beautiful email with test summary
if: always()
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.gmail.com
server_port: 465
username: ${{ secrets.MAIL_USERNAME }}
password: ${{ secrets.MAIL_PASSWORD }}
subject: "Spring App Test Results - ${{ github.repository }} #${{ github.run_number }}"
to: ${{ secrets.MAIL_RECIPIENT }}
from: ${{ secrets.MAIL_USERNAME }}
content_type: text/html
body: |
<html>
<body style="font-family: Arial, sans-serif; background:#f5f5f5; padding:20px;">
<div style="max-width:600px; margin:auto; background:white; border-radius:8px; box-shadow:0 2px 8px #ccc; padding:24px;">
<h2 style="color:#336699;">🚀 Test Results for <span style="color:#222;">${{ github.repository }}</span> (Run #${{ github.run_number }})</h2>
<p>
<strong>Workflow:</strong> <a href="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}">View Run Logs</a><br>
<strong>Status:</strong> <span style="color:${{ steps.set-status.outputs.status_color }};"><b>${{ steps.set-status.outputs.status }}</b></span><br>
<strong>Commit:</strong> <a href="${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}">${{ github.sha }}</a><br>
<strong>Branch:</strong> <span style="color:#555;">${{ github.ref_name }}</span><br>
<strong>Author:</strong> ${{ github.actor }}<br>
<strong>Started:</strong> ${{ steps.start-time.outputs.start_time_formatted }}<br>
<strong>Duration:</strong> ${{ steps.end-time.outputs.duration }}
</p>
<hr>
<h3 style="color:#336699;">📊 Test Results</h3>
<table style="width:100%; border-collapse:collapse;">
<thead>
<tr style="background:#eef;">
<th style="border:1px solid #ccc; padding:8px;">Total</th>
<th style="border:1px solid #ccc; padding:8px;">Passed</th>
<th style="border:1px solid #ccc; padding:8px;">Failed</th>
<th style="border:1px solid #ccc; padding:8px;">Skipped</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border:1px solid #ccc; padding:8px; text-align:center;">${{ steps.test-summary.outputs.total }}</td>
<td style="border:1px solid #ccc; padding:8px; color:green; text-align:center; font-weight:bold;">${{ steps.test-summary.outputs.passed }}</td>
<td style="border:1px solid #ccc; padding:8px; color:${{ steps.test-summary.outputs.failed == '0' && 'green' || 'red' }}; text-align:center; font-weight:bold;">${{ steps.test-summary.outputs.failed }}</td>
<td style="border:1px solid #ccc; padding:8px; color:#555; text-align:center;">${{ steps.test-summary.outputs.skipped }}</td>
</tr>
</tbody>
</table>
<hr>
<p style="color:#777; font-size:12px;">You are receiving this email because you requested notifications for workflow runs on <b>${{ github.repository }}</b>.</p>
</div>
</body>
</html>
attachments: target/surefire-reports/*.xml