Comment Artifact URL on PRs #2142
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: Comment Artifact URL on PRs | |
on: | |
workflow_run: | |
types: | |
- 'completed' | |
workflows: | |
- 'Artifact Upload' | |
jobs: | |
comment-on-pr: | |
if: github.event.workflow_run.conclusion == 'success' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get Artifact URL & PR Info | |
env: | |
GITHUB_TOKEN: ${{ github.token }} | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
GITHUB_WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }} | |
GITHUB_WORKFLOW_RUN_CHECK_SUITE_ID: ${{ github.event.workflow_run.check_suite_id }} | |
WORKFLOW_RUN_EVENT_OBJ: ${{ toJSON(github.event.workflow_run) }} | |
run: | | |
set -e | |
HEAD_SHA=$(jq -r '.pull_requests[0].head.sha' <<< "$WORKFLOW_RUN_EVENT_OBJ") | |
PR_NUMBER=$(jq -r '.pull_requests[0].number' <<< "$WORKFLOW_RUN_EVENT_OBJ") | |
echo "PR Number: $PR_NUMBER" | |
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV" | |
gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_WORKFLOW_RUN_ID/artifacts" \ | |
--jq "[.artifacts | .[] | {"id": .id, "name": .name}]" \ | |
> artifacts.json | |
PR_COMMENT=$( | |
echo "Builds for commit $HEAD_SHA:" | |
while IFS="§" read -r name url; do | |
echo "* [$name]($url)" | |
done < <(jq -r ".[] | \ | |
.name + \"§\" \ | |
+ ( .id | tostring | \"https://github.com/$GITHUB_REPOSITORY/suites/$GITHUB_WORKFLOW_RUN_CHECK_SUITE_ID/artifacts/\" + . )" \ | |
artifacts.json) | |
) | |
echo -e "PR COMMENT: \n$PR_COMMENT" | |
{ | |
echo 'PR_COMMENT<<EOF' | |
echo -e "$PR_COMMENT" | |
echo EOF | |
} >> "$GITHUB_ENV" | |
- name: Update Comment | |
env: | |
HEAD_SHA: ${{ env.HEAD_SHA }} | |
uses: peter-evans/create-or-update-comment@v2 | |
with: | |
issue-number: ${{ env.PR_NUMBER }} | |
body: ${{ env.PR_COMMENT }} |