|
8 | 8 | artifacts-url-comments: |
9 | 9 | name: Add artifact links to PR and issues |
10 | 10 | runs-on: ubuntu-latest |
11 | | - # Only run if the CI workflow was triggered by a pull_request |
12 | | - if: ${{ github.event.workflow_run.event == 'pull_request' }} |
| 11 | + # Only run if the CI workflow was triggered by a pull_request and succeeded |
| 12 | + if: > |
| 13 | + github.event.workflow_run.event == 'pull_request' && |
| 14 | + github.event.workflow_run.conclusion == 'success' |
13 | 15 | permissions: |
14 | 16 | pull-requests: write |
15 | 17 | actions: read |
16 | 18 | steps: |
17 | | - - name: Add artifact links to PR and issues |
18 | | - uses: tonyhallett/[email protected] |
19 | | - env: |
20 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 19 | + - name: 'Download artifact metadata' |
| 20 | + uses: actions/github-script@v7 |
| 21 | + id: artifacts |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + run_id: context.payload.workflow_run.id, |
| 28 | + }); |
| 29 | + |
| 30 | + let links = []; |
| 31 | + for (const artifact of artifacts.data.artifacts) { |
| 32 | + const url = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.payload.workflow_run.id}/artifacts/${artifact.id}`; |
| 33 | + links.push(`- [${artifact.name}](${url}) (${(artifact.size_in_bytes / 1024 / 1024).toFixed(2)} MB)`); |
| 34 | + } |
| 35 | + |
| 36 | + core.setOutput('artifact_links', links.join('\n')); |
| 37 | + core.setOutput('has_artifacts', links.length > 0); |
| 38 | + |
| 39 | + - name: 'Comment on PR' |
| 40 | + if: steps.artifacts.outputs.has_artifacts == 'true' |
| 41 | + uses: actions/github-script@v7 |
21 | 42 | with: |
22 | | - prefix: "Do you want to test this code? Here you have an automated build:" |
23 | | - suffix: "WARNING: It may be unstable and result in corrupted configurations or data loss. Use only for testing!" |
24 | | - format: name |
25 | | - addTo: pull |
| 43 | + script: | |
| 44 | + const prNumber = context.payload.workflow_run.pull_requests[0].number; |
| 45 | + const artifactLinks = process.env.ARTIFACT_LINKS; |
| 46 | + |
| 47 | + const body = `## 🚀 Build Artifacts Ready! |
| 48 | + |
| 49 | + Do you want to test this code? Here you have automated builds: |
| 50 | + |
| 51 | + ${artifactLinks} |
| 52 | + |
| 53 | + ⚠️ **WARNING:** These are preview builds and may be unstable. They could result in corrupted configurations or data loss. Use only for testing!`; |
| 54 | +
|
| 55 | + await github.rest.issues.createComment({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: context.repo.repo, |
| 58 | + issue_number: prNumber, |
| 59 | + body: body |
| 60 | + }); |
| 61 | + env: |
| 62 | + ARTIFACT_LINKS: ${{ steps.artifacts.outputs.artifact_links }} |
0 commit comments