|
6 | 6 | pull_request: |
7 | 7 | branches: [ master ] |
8 | 8 |
|
| 9 | +permissions: |
| 10 | + actions: read |
| 11 | + contents: read |
| 12 | + pull-requests: write |
| 13 | + |
9 | 14 | jobs: |
10 | 15 | build: |
11 | 16 | strategy: |
|
75 | 80 | name: ${{ matrix.config.displayTargetName }} |
76 | 81 | path: ${{ matrix.config.artifact }} |
77 | 82 | if-no-files-found: error |
| 83 | + |
| 84 | + post-artifacts: |
| 85 | + if: github.event_name == 'pull_request' |
| 86 | + needs: build |
| 87 | + runs-on: ubuntu-latest |
| 88 | + steps: |
| 89 | + - name: Comment artifact links on PR |
| 90 | + continue-on-error: true |
| 91 | + uses: actions/github-script@v7 |
| 92 | + with: |
| 93 | + script: | |
| 94 | + const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 95 | +
|
| 96 | + // Fetch actual artifact list from this workflow run |
| 97 | + const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({ |
| 98 | + owner: context.repo.owner, |
| 99 | + repo: context.repo.repo, |
| 100 | + run_id: context.runId, |
| 101 | + }); |
| 102 | +
|
| 103 | + let rows = ''; |
| 104 | + for (const a of artifacts) { |
| 105 | + const dlUrl = `${runUrl}/artifacts/${a.id}`; |
| 106 | + rows += `| ${a.name} | [${a.name}](${dlUrl}) | ${(a.size_in_bytes / 1024).toFixed(0)} KB |\n`; |
| 107 | + } |
| 108 | +
|
| 109 | + if (!rows) { |
| 110 | + rows = `| - | No artifacts found | - |\n`; |
| 111 | + } |
| 112 | +
|
| 113 | + const comments = await github.paginate( |
| 114 | + github.rest.issues.listComments, |
| 115 | + { |
| 116 | + owner: context.repo.owner, |
| 117 | + repo: context.repo.repo, |
| 118 | + issue_number: context.issue.number, |
| 119 | + per_page: 100, |
| 120 | + } |
| 121 | + ); |
| 122 | + const marker = '<!-- build-artifacts -->'; |
| 123 | + const existing = comments.find(c => c.body.includes(marker)); |
| 124 | +
|
| 125 | + const body = [ |
| 126 | + marker, |
| 127 | + '### Build Artifacts', |
| 128 | + '', |
| 129 | + '| Platform | Download | Size |', |
| 130 | + '|----------|----------|------|', |
| 131 | + rows.trim(), |
| 132 | + '', |
| 133 | + `> [Full workflow run](${runUrl})`, |
| 134 | + '', |
| 135 | + `<sub>Updated by CI at ${new Date().toISOString()}</sub>`, |
| 136 | + ].join('\n'); |
| 137 | +
|
| 138 | + if (existing) { |
| 139 | + await github.rest.issues.updateComment({ |
| 140 | + owner: context.repo.owner, |
| 141 | + repo: context.repo.repo, |
| 142 | + comment_id: existing.id, |
| 143 | + body, |
| 144 | + }); |
| 145 | + } else { |
| 146 | + await github.rest.issues.createComment({ |
| 147 | + owner: context.repo.owner, |
| 148 | + repo: context.repo.repo, |
| 149 | + issue_number: context.issue.number, |
| 150 | + body, |
| 151 | + }); |
| 152 | + } |
0 commit comments