.github/workflows/ci-comment.yml #199
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
on: | |
workflow_run: | |
workflows: ['Continuous Integration'] | |
types: | |
- completed | |
# This is adapted from https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run (2022-07-17) | |
jobs: | |
on-failure: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
steps: | |
- 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 == "pr_number" | |
})[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}/pr_number.zip`, Buffer.from(download.data)); | |
- name: 'Unzip artifact' | |
run: unzip pr_number.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('./pr_number', 'utf8').trim()); | |
let pr_sha = fs.readFileSync('./pr_sha', 'utf8').trim(); | |
let merge_sha = fs.readFileSync('./merge_sha', 'utf8').trim(); | |
let pull = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: issue_number | |
}); | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue_number, | |
body: `The commit ${pr_sha} (as a parent of ${merge_sha}) contains errors. Please inspect the [Run Summary](${context.payload.workflow_run.html_url}) for details.` | |
}); | |