|
| 1 | +name: Test PR build |
| 2 | + |
| 3 | +run-name: "Tests triggered by PR: ${{ github.event.workflow_run.display_title }}" |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_run: |
| 7 | + workflows: ["Build on PR"] |
| 8 | + types: |
| 9 | + - completed |
| 10 | + |
| 11 | +permissions: |
| 12 | + checks: write # required by test reporting action |
| 13 | + pull-requests: write # required by test reporting action |
| 14 | + contents: read # github default |
| 15 | + packages: read # github default |
| 16 | + |
| 17 | +jobs: |
| 18 | + retrieve-build-url: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 21 | + outputs: |
| 22 | + url: ${{ steps.set-build-url.outputs.url }} |
| 23 | + steps: |
| 24 | + - name: 'Download build URL' |
| 25 | + uses: actions/github-script@v6 |
| 26 | + with: |
| 27 | + script: | |
| 28 | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 29 | + owner: context.repo.owner, |
| 30 | + repo: context.repo.repo, |
| 31 | + run_id: context.payload.workflow_run.id, |
| 32 | + }); |
| 33 | + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { |
| 34 | + return artifact.name == "build_url" |
| 35 | + })[0]; |
| 36 | + let download = await github.rest.actions.downloadArtifact({ |
| 37 | + owner: context.repo.owner, |
| 38 | + repo: context.repo.repo, |
| 39 | + artifact_id: matchArtifact.id, |
| 40 | + archive_format: 'zip', |
| 41 | + }); |
| 42 | + const fs = require('fs'); |
| 43 | + const path = require('path'); |
| 44 | + const temp = '${{ runner.temp }}/artifacts'; |
| 45 | + if (!fs.existsSync(temp)){ |
| 46 | + fs.mkdirSync(temp); |
| 47 | + } |
| 48 | + fs.writeFileSync(path.join(temp, 'build_url.zip'), Buffer.from(download.data)); |
| 49 | +
|
| 50 | + - name: 'Setup build URL' |
| 51 | + id: set-build-url |
| 52 | + run: | |
| 53 | + unzip "${{ runner.temp }}/artifacts/build_url.zip" |
| 54 | + BUILD_URL=$(cat build_url) |
| 55 | + echo "Build URL: ${BUILD_URL}" |
| 56 | + echo "url=${BUILD_URL}" >> $GITHUB_OUTPUT |
| 57 | +
|
| 58 | + test: |
| 59 | + uses: ./.github/workflows/test.yml |
| 60 | + secrets: inherit |
| 61 | + needs: retrieve-build-url |
| 62 | + with: |
| 63 | + url: ${{ needs.retrieve-build-url.outputs.url }} |
| 64 | + |
| 65 | + publish-test-results: |
| 66 | + name: "Publish Tests Results" |
| 67 | + needs: test |
| 68 | + runs-on: ubuntu-latest |
| 69 | + steps: |
| 70 | + - name: Download Artifacts |
| 71 | + uses: actions/download-artifact@v4 |
| 72 | + with: |
| 73 | + path: artifacts |
| 74 | + |
| 75 | + - name: Download event file |
| 76 | + uses: actions/download-artifact@v4 |
| 77 | + with: |
| 78 | + run-id: ${{ github.event.workflow_run.id }} |
| 79 | + path: artifacts |
| 80 | + github-token: ${{ github.token }} |
| 81 | + |
| 82 | + - name: "List files" |
| 83 | + run: | |
| 84 | + echo $GITHUB_WORKSPACE |
| 85 | + ls -R $GITHUB_WORKSPACE |
| 86 | +
|
| 87 | + - name: Publish Test Results |
| 88 | + uses: EnricoMi/publish-unit-test-result-action@v2 |
| 89 | + with: |
| 90 | + commit: ${{ github.event.workflow_run.head_sha }} |
| 91 | + event_file: artifacts/Event File/event.json |
| 92 | + event_name: ${{ github.event.workflow_run.event }} |
| 93 | + files: "${{ github.workspace }}/artifacts/**/*.xml" |
| 94 | + |
| 95 | + - name: Prepare PR comment |
| 96 | + id: pr_comment_prep |
| 97 | + run: | |
| 98 | + echo "## Test jobs for commit ${{ github.event.workflow_run.head_sha }}" > pr-comment.txt |
| 99 | + for json_file in $(find ${{ github.workspace }} -name "test-job-*.json") |
| 100 | + do |
| 101 | + DEVICE_TYPE=$(cat "$json_file" | jq -r ".requested_device_type") |
| 102 | + URL=$(cat "$json_file" | jq -r ".url") |
| 103 | + JOB_ID=$(cat "$json_file" | jq -r ".id") |
| 104 | + echo " * [Job $JOB_ID on $DEVICE_TYPE]($URL)" |
| 105 | + echo " * [Job $JOB_ID on $DEVICE_TYPE]($URL)" >> pr-comment.txt |
| 106 | + done |
| 107 | + PR_NUMBER=$(cat "artifacts/Event File/event.json" | jq -r ".number") |
| 108 | + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 109 | +
|
| 110 | + - name: Comment on PR |
| 111 | + uses: thollander/actions-comment-pull-request@v3 |
| 112 | + with: |
| 113 | + file-path: pr-comment.txt |
| 114 | + pr-number: ${{ steps.pr_comment_prep.outputs.pr_number }} |
0 commit comments