|
| 1 | +name: Post build artifact links |
| 2 | + |
| 3 | +# Runs in the context of the base (upstream) repo after the build workflow |
| 4 | +# completes — this gives the GITHUB_TOKEN write access to PRs even when |
| 5 | +# the build was triggered by a fork PR. |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: ["C/C++ CI"] |
| 9 | + types: [completed] |
| 10 | + |
| 11 | +permissions: |
| 12 | + actions: read |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + post-artifacts: |
| 17 | + if: > |
| 18 | + github.event.workflow_run.event == 'pull_request' && |
| 19 | + github.event.workflow_run.conclusion == 'success' |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Comment artifact links on PR |
| 23 | + uses: actions/github-script@v7 |
| 24 | + with: |
| 25 | + script: | |
| 26 | + const run = context.payload.workflow_run; |
| 27 | + const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${run.id}`; |
| 28 | +
|
| 29 | + // Find associated PR number from the triggering workflow run |
| 30 | + const prs = run.pull_requests; |
| 31 | + if (!prs || prs.length === 0) { |
| 32 | + // Fallback: search for open PR matching the head branch/SHA |
| 33 | + const { data: pulls } = await github.rest.pulls.list({ |
| 34 | + owner: context.repo.owner, |
| 35 | + repo: context.repo.repo, |
| 36 | + state: 'open', |
| 37 | + head: `${run.head_repository.owner.login}:${run.head_branch}`, |
| 38 | + }); |
| 39 | + if (pulls.length === 0) { |
| 40 | + console.log('No associated PR found, skipping.'); |
| 41 | + return; |
| 42 | + } |
| 43 | + prs.push(pulls[0]); |
| 44 | + } |
| 45 | + const prNumber = prs[0].number; |
| 46 | +
|
| 47 | + // Fetch artifacts from the completed workflow run |
| 48 | + const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({ |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo, |
| 51 | + run_id: run.id, |
| 52 | + }); |
| 53 | +
|
| 54 | + let rows = ''; |
| 55 | + for (const a of artifacts) { |
| 56 | + const dlUrl = `${runUrl}/artifacts/${a.id}`; |
| 57 | + rows += `| ${a.name} | [${a.name}](${dlUrl}) | ${(a.size_in_bytes / 1024).toFixed(0)} KB |\n`; |
| 58 | + } |
| 59 | +
|
| 60 | + if (!rows) { |
| 61 | + rows = `| - | No artifacts found | - |\n`; |
| 62 | + } |
| 63 | +
|
| 64 | + // Check for existing comment to update (upsert pattern) |
| 65 | + const comments = await github.paginate( |
| 66 | + github.rest.issues.listComments, |
| 67 | + { |
| 68 | + owner: context.repo.owner, |
| 69 | + repo: context.repo.repo, |
| 70 | + issue_number: prNumber, |
| 71 | + per_page: 100, |
| 72 | + } |
| 73 | + ); |
| 74 | + const marker = '<!-- build-artifacts -->'; |
| 75 | + const existing = comments.find(c => c.body.includes(marker)); |
| 76 | +
|
| 77 | + const body = [ |
| 78 | + marker, |
| 79 | + '### Build Artifacts', |
| 80 | + '', |
| 81 | + '| Platform | Download | Size |', |
| 82 | + '|----------|----------|------|', |
| 83 | + rows.trim(), |
| 84 | + '', |
| 85 | + `> [Full workflow run](${runUrl})`, |
| 86 | + '', |
| 87 | + `<sub>Updated by CI at ${new Date().toISOString()}</sub>`, |
| 88 | + ].join('\n'); |
| 89 | +
|
| 90 | + if (existing) { |
| 91 | + await github.rest.issues.updateComment({ |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + comment_id: existing.id, |
| 95 | + body, |
| 96 | + }); |
| 97 | + console.log(`Updated comment ${existing.id} on PR #${prNumber}`); |
| 98 | + } else { |
| 99 | + await github.rest.issues.createComment({ |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + issue_number: prNumber, |
| 103 | + body, |
| 104 | + }); |
| 105 | + console.log(`Created new comment on PR #${prNumber}`); |
| 106 | + } |
0 commit comments