Unit-Test pipeline and publish results #2
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: Unit-Test pipeline and publish results | |
| on: | |
| workflow_dispatch: | |
| jobs: | |
| trigger-unit-tests-job: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Code | |
| uses: actions/checkout@v4 | |
| - name: Setup Java and Maven | |
| uses: actions/setup-java@v3 | |
| with: | |
| distribution: 'temurin' | |
| java-version: '17' # or 11 based on your project | |
| - name: Unit-Test execution started | |
| run: mvn clean surefire-report:report | |
| - name: Test Run Completed | |
| if: always() | |
| run: | | |
| echo "✅ Job completed for ${{ inputs.userName }} on branch ${{ github.ref_name }}" | |
| cat target/surefire-reports/*Test.txt | |
| - name: Upload Surefire HTML Report | |
| if: always() | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: surefire-report | |
| path: target/site/surefire-report.html | |
| - name: Extract Test Summary | |
| id: test_summary | |
| if: always() | |
| run: | | |
| # use bash features | |
| shopt -s nullglob | |
| # Look for text reports first (common) | |
| files=(target/surefire-reports/*Test.txt target/surefire-reports/*.txt) | |
| if [ ${#files[@]} -eq 0 ]; then | |
| # fallback to XML reports if txt not present | |
| xml_files=(target/surefire-reports/TEST-*.xml) | |
| if [ ${#xml_files[@]} -eq 0 ]; then | |
| echo "summary=⚠️ No test result files found." >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| sums=$(sed -n 's/.*tests="\([0-9]\+\)".*failures="\([0-9]\+\)".*errors="\([0-9]\+\)".*skipped="\([0-9]\+\)".*/\1 \2 \3 \4/p' "${xml_files[@]}" \ | |
| | awk '{t+=$1;f+=$2;e+=$3;s+=$4} END {p=t-f-e-s; printf "%d|%d|%d|%d|%d", t, f, e, s, p}') | |
| if [ -z "$sums" ]; then | |
| echo "summary=⚠️ Could not parse XML test results." >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| IFS='|' read total failures errors skipped passed <<< "$sums" | |
| fails=$((failures + errors)) | |
| echo "summary=🧪 Total: $total | ✅ Passed: $passed | ❌ Failed: $fails" >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| # Parse human-readable .txt surefire reports | |
| sums=$(sed -n 's/.*Tests run:[[:space:]]*\([0-9]\+\)[[:space:]]*,[[:space:]]*Failures:[[:space:]]*\([0-9]\+\)[[:space:]]*,[[:space:]]*Errors:[[:space:]]*\([0-9]\+\)[[:space:]]*,[[:space:]]*Skipped:[[:space:]]*\([0-9]\+\).*/\1 \2 \3 \4/p' "${files[@]}" \ | |
| | awk '{t+=$1;f+=$2;e+=$3;s+=$4} END {p=t-f-e-s; printf "%d|%d|%d|%d|%d", t, f, e, s, p}') | |
| if [ -z "$sums" ]; then | |
| echo "summary=⚠️ Could not parse text test results." >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| IFS='|' read total failures errors skipped passed <<< "$sums" | |
| fails=$((failures + errors)) | |
| echo "summary=🧪 Total: $total | ✅ Passed: $passed | ❌ Failed: $fails" >> $GITHUB_OUTPUT | |
| - name: Set Slack Color | |
| id: slack_color | |
| if: always() | |
| run: | | |
| if [ "${{ job.status }}" == "success" ]; then | |
| echo "color=#36a64f" >> $GITHUB_OUTPUT | |
| else | |
| echo "color=#ff0000" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Publish Test Results on Slack | |
| if: always() | |
| uses: docker://technosophos/slack-notify | |
| env: | |
| SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} | |
| SLACK_TITLE: > | |
| Unit-Test Results - ${{ job.status == 'success' && '✅ Success' || '❌ Failed' }} | |
| SLACK_MESSAGE: | | |
| *👤 Triggered by:* `${{ github.actor }}` | |
| *🌿 Branch:* `${{ github.ref_name }}` | |
| *🧪 Test Summary:* `${{ steps.test_summary.outputs.summary }}` | |
| *📊 Full Report:* <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|Open Report in Artifacts> | |
| SLACK_COLOR: ${{ steps.slack_color.outputs.color }} |