Attach build #2271
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Attach build | |
# We attach build artifacts in a separate workflow, | |
# since they are not available in the same workflow | |
on: | |
workflow_run: | |
workflows: | |
- Build pull request | |
types: | |
- completed | |
jobs: | |
attach_build_artifacts: | |
runs-on: ubuntu-latest | |
permissions: | |
pull-requests: write | |
contents: write | |
actions: read | |
steps: | |
- name: 'Download PR number' | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: context.payload.workflow_run.id, | |
}); | |
const matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == 'pr_number'; | |
})[0]; | |
const download = await github.rest.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
const fs = require('fs'); | |
fs.writeFileSync( | |
`${process.env.GITHUB_WORKSPACE}/pr_number.zip`, | |
Buffer.from(download.data) | |
); | |
- name: 'Unzip PR number' | |
run: unzip pr_number.zip | |
- name: Get build artifacts | |
id: get-build-artifacts | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
run: | | |
set -x | |
run_number=${{ github.event.workflow_run.run_number }} | |
check_suite_id=${{ github.event.workflow_run.check_suite_id }} | |
repository_url=${{ github.server_url }}/${{ github.repository }} | |
pr_number=$(cat ./pr_number) | |
build_artifact_id=$(curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}/artifacts" | jq ".artifacts[1].id") | |
# We can not use "url" or "archive_download_url" here, | |
# hence we need to figure out the artifact's URL "manually" | |
build_download_url=${repository_url}/suites/${check_suite_id}/artifacts/${build_artifact_id} | |
set +x | |
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT | |
echo "build_download_url=$build_download_url" >> $GITHUB_OUTPUT | |
- name: Attach build artifacts to the PR thread | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
uses: actions/github-script@v3 | |
with: | |
script: | | |
const pr_number = "${{ steps.get-build-artifacts.outputs.pr_number }}"; | |
const build_download_url = "${{ steps.get-build-artifacts.outputs.build_download_url }}"; | |
const message = `:package: <a href='${build_download_url}'>build.zip</a>`; | |
const { data: comments } = await github.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: pr_number, | |
}); | |
const comment = comments.find(c => c.body.startsWith(":package:")); | |
const repo = { owner: context.repo.owner, repo: context.repo.repo }; | |
const formatDate = date => | |
new Intl.DateTimeFormat('en', { | |
month: 'short', | |
day: 'numeric', | |
hour: 'numeric', | |
minute: 'numeric', | |
second: 'numeric', | |
}).format(date); | |
const updateComment = ({ comment_id, body }) => | |
github.issues.updateComment({ ...repo, comment_id, body }); | |
const createComment = ({ issue_number, body }) => | |
github.issues.createComment({ ...repo, issue_number, body }); | |
if (comment) { | |
const body = `${message} [updated at ${formatDate(new Date())} UTC]`; | |
updateComment({ comment_id: comment.id, body }); | |
} else { | |
createComment({ issue_number: pr_number, body: message }); | |
} |