Update test-and-email.yml #3
This file contains hidden or 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: 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.start-end.outputs.duration }} | |
| steps: | |
| - name: Start time | |
| id: start-end | |
| run: echo "start_time=$(date +%s)" >> $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 | |
| - 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 | |
| run: | | |
| total=$(grep -h 'testsuite' target/surefire-reports/*.xml | awk -F'"' '{print $4}' | paste -sd+ - | bc) | |
| failed=$(grep -h 'testsuite' target/surefire-reports/*.xml | awk -F'"' '{print $8}' | paste -sd+ - | bc) | |
| skipped=$(grep -h 'testsuite' target/surefire-reports/*.xml | awk -F'"' '{print $12}' | paste -sd+ - | bc) | |
| passed=$((total - failed - skipped)) | |
| 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 | |
| run: | | |
| end_time=$(date +%s) | |
| start_time=${{ steps.start-end.outputs.start_time }} | |
| duration=$((end_time - start_time)) | |
| min=$((duration / 60)) | |
| sec=$((duration % 60)) | |
| echo "duration=${min}m ${sec}s" >> $GITHUB_OUTPUT | |
| - 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:${{ job.status == 'success' && 'green' || 'red' }};"><b>${{ job.status == 'success' && '✅ Success' || '❌ Failure' }}</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-end.outputs.start_time }}<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;">${{ steps.test-summary.outputs.total }}</td> | |
| <td style="border:1px solid #ccc; padding:8px; color:green;">${{ steps.test-summary.outputs.passed }}</td> | |
| <td style="border:1px solid #ccc; padding:8px; color:${{ steps.test-summary.outputs.failed == '0' && 'green' || 'red' }};">${{ steps.test-summary.outputs.failed }}</td> | |
| <td style="border:1px solid #ccc; padding:8px; color:#555;">${{ steps.test-summary.outputs.skipped }}</td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <hr> | |
| <p style="color:#777;">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 |