Github Actions - Link Artifacts in PR comments #1
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: Link PR Artifacts | ||
on: | ||
workflow_run: | ||
workflows: [Upload PR Artifacts] | ||
types: | ||
- completed | ||
jobs: | ||
download: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: set artifact name | ||
id: set_artifactname | ||
run: | | ||
if [[ "${{ github.REPOSITORY }}" == "emuflight/EmuFlight" ]] ; then | ||
ARTIFACT_NAME="EmuFlight-${{ env.VERSION }}-${{ github.run_number }}" | ||
else | ||
ARTIFACT_NAME="EmuFlight-${{ env.VERSION }}-${{ github.ACTOR }}-${{ github.run_number }}" | ||
fi | ||
echo "${ARTIFACT_NAME}" | ||
echo "ARTIFACT_NAME=${ARTIFACT_NAME}" >> $GITHUB_ENV | ||
- name: 'Download artifact' | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
run_id: context.payload.workflow_run.id, | ||
}); | ||
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { | ||
return artifact.name == `${ARTIFACT_NAME}` | ||
})[0]; | ||
let download = await github.rest.actions.downloadArtifact({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
artifact_id: matchArtifact.id, | ||
archive_format: 'zip', | ||
}); | ||
let fs = require('fs'); | ||
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/${ARTIFACT_NAME}.zip`, Buffer.from(download.data)); | ||
- name: 'Unzip artifact' | ||
run: unzip `${ARTIFACT_NAME}.zip` | ||
- name: 'Comment on PR' | ||
uses: actions/github-script@v6 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
let fs = require('fs'); | ||
let issue_number = Number(fs.readFileSync(`./${ARTIFACT_NAME}`)); | ||
await github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: issue_number, | ||
body: "Compiled HEX from this Pull-Request. WARNING: No warranty or guarantee of any kind. Test/Fly at your own risk!" | ||
}); |