From f00165ddf65e7f4eca502b22a0a91ef78e6a4061 Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Fri, 21 Apr 2023 13:06:43 +0200
Subject: [PATCH] CI: adding test result upload

---
 .github/workflows/linux.yml               | 65 ++++++++++++++++++++---
 .github/workflows/test-result-comment.yml | 46 ++++++++++++++++
 2 files changed, 105 insertions(+), 6 deletions(-)
 create mode 100644 .github/workflows/test-result-comment.yml

diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml
index 3186567c9..f564cbd06 100644
--- a/.github/workflows/linux.yml
+++ b/.github/workflows/linux.yml
@@ -45,7 +45,7 @@ jobs:
           ninja install
           . ../bin/thisdd4hep.sh
           echo "::group::Test"
-          ctest --output-on-failure -j4
+          ctest --output-on-failure -j4 --output-junit TestResults_1.xml
           echo "::group::CMakeExamples"
           cd ../examples/
           mkdir build
@@ -57,7 +57,21 @@ jobs:
           echo "::group::CompileExamples"
           ninja install
           echo "::group::TestExamples"
-          ctest --output-on-failure -j2
+          ctest --output-on-failure -j2 --output-junit TestResults_2.xml
+    - name: Prepare Artifact Name
+      if: success() || failure()
+      run: |
+          artifact_name="${{ matrix.LCG }}"
+          artifact_name=$(echo -n "${artifact_name}" | sed -e 's@/@@g')
+          echo "ARTIFACT_NAME=${artifact_name}" >> $GITHUB_ENV
+    - name: Upload test results
+      if: success() || failure()
+      uses: actions/upload-artifact@v3
+      with:
+        name: Test Results ${{ env.ARTIFACT_NAME }}
+        path: |
+           /home/runner/work/DD4hep/DD4hep/build/TestResults_1.xml
+           /home/runner/work/DD4hep/DD4hep/examples/build/TestResults_2.xml
 
 
   key4hep:
@@ -97,7 +111,7 @@ jobs:
           ninja install
           . ../bin/thisdd4hep.sh
           echo "::group::Test"
-          ctest --output-on-failure -j4
+          ctest --output-on-failure -j4 --output-junit TestResults_1.xml
           echo "::group::CMakeExamples"
           cd ../examples/
           mkdir build
@@ -109,7 +123,21 @@ jobs:
           echo "::group::CompileExamples"
           ninja install
           echo "::group::TestExamples"
-          ctest --output-on-failure -j2
+          ctest --output-on-failure -j2 --output-junit TestResults_2.xml
+    - name: Prepare Artifact Name
+      if: success() || failure()
+      run: |
+          artifact_name="${{ matrix.LCG }}"
+          artifact_name=$(echo -n "${artifact_name}" | sed -e 's@/@@g')
+          echo "ARTIFACT_NAME=${artifact_name}" >> $GITHUB_ENV
+    - name: Upload test results
+      if: success() || failure()
+      uses: actions/upload-artifact@v3
+      with:
+        name: Test Results ${{ env.ARTIFACT_NAME }}
+        path: |
+           /home/runner/work/DD4hep/DD4hep/build/TestResults_1.xml
+           /home/runner/work/DD4hep/DD4hep/examples/build/TestResults_2.xml
 
   non-shared:
     runs-on: ubuntu-latest
@@ -182,7 +210,7 @@ jobs:
           ninja install
           . ../bin/thisdd4hep.sh
           echo "::group::Test"
-          ctest --output-on-failure -j4
+          ctest --output-on-failure -j4 --output-junit TestResults_1.xml
           echo "::group::CMakeExamples"
           cd ../examples/
           mkdir build
@@ -194,4 +222,29 @@ jobs:
           echo "::group::CompileExamples"
           ninja install
           echo "::group::TestExamples"
-          ctest --output-on-failure -j2
+          ctest --output-on-failure -j2 --output-junit TestResults_2.xml
+    - name: Prepare Artifact Name
+      if: success() || failure()
+      run: |
+          artifact_name="${{ matrix.LCG }}"
+          artifact_name=$(echo -n "${artifact_name}" | sed -e 's@/@@g')
+          echo "ARTIFACT_NAME=${artifact_name}" >> $GITHUB_ENV
+    - name: Upload test results
+      if: success() || failure()
+      uses: actions/upload-artifact@v3
+      with:
+        name: Test Results ${{ env.ARTIFACT_NAME }}
+        path: |
+           /home/runner/work/DD4hep/DD4hep/build/TestResults_1.xml
+           /home/runner/work/DD4hep/DD4hep/examples/build/TestResults_2.xml
+
+  event_file:
+    if: github.repository_owner == 'aidasoft' || github.event_name == 'pull_request'
+    name: "Upload Event Payload"
+    runs-on: ubuntu-latest
+    steps:
+    - name: Upload
+      uses: actions/upload-artifact@v3
+      with:
+        name: Event File
+        path: ${{ github.event_path }}
diff --git a/.github/workflows/test-result-comment.yml b/.github/workflows/test-result-comment.yml
new file mode 100644
index 000000000..3d7cbf5f6
--- /dev/null
+++ b/.github/workflows/test-result-comment.yml
@@ -0,0 +1,46 @@
+name: Test Summary PR comment
+
+on:
+  workflow_run:
+    # do NOT use quotes: https://stackoverflow.com/a/72551795/17876693
+    workflows: [linux]
+    types:
+      - completed
+permissions: {}
+
+jobs:
+  comment-test-results:
+    name: Publish Test Results
+
+    if: github.event.workflow_run.event == 'pull_request'
+
+    runs-on: ubuntu-latest
+
+    permissions:
+      checks: write
+      pull-requests: write
+      actions: read
+
+    steps:
+      - name: Download and Extract Artifacts
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+        run: |
+           mkdir -p artifacts && cd artifacts
+
+           artifacts_url=${{ github.event.workflow_run.artifacts_url }}
+
+           gh api --paginate "$artifacts_url" -q '.artifacts[] | [.name, .archive_download_url] | @tsv' | while read artifact
+           do
+             IFS=$'\t' read name url <<< "$artifact"
+             gh api $url > "$name.zip"
+             unzip -d "$name" "$name.zip"
+           done
+
+      - name: Publish Test Results
+        uses: EnricoMi/publish-unit-test-result-action@v2
+        with:
+          commit: ${{ github.event.workflow_run.head_sha }}
+          event_file: artifacts/Event File/event.json
+          event_name: ${{ github.event.workflow_run.event }}
+          files: "artifacts/**/*.xml"
-- 
GitLab