Pull Request - Comment Artifact URL #31
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
# https://github.com/orgs/community/discussions/51403 | |
name: Pull Request - Comment Artifact URL | |
on: | |
workflow_run: | |
types: | |
- "completed" | |
workflows: | |
- "Pull Request - 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 }} | |
OWNER: ${{ github.repository_owner }} | |
REPO: ${{ github.event.repository.name }} | |
WORKFLOW_RUN_EVENT_OBJ: ${{ toJSON(github.event.workflow_run) }} | |
run: | | |
PREVIOUS_JOB_ID=$(jq -r '.id' <<< "$WORKFLOW_RUN_EVENT_OBJ") | |
echo "Previous Job ID: $PREVIOUS_JOB_ID" | |
echo "PREVIOUS_JOB_ID=$PREVIOUS_JOB_ID" >> "$GITHUB_ENV" | |
SUITE_ID=$(jq -r '.check_suite_id' <<< "$WORKFLOW_RUN_EVENT_OBJ") | |
echo "Previous Suite ID: $SUITE_ID" | |
echo "SUITE_ID=$SUITE_ID" >> "$GITHUB_ENV" | |
ARTIFACT_ID=$(gh api "/repos/$OWNER/$REPO/actions/artifacts" \ | |
--jq ".artifacts.[] | | |
select(.workflow_run.id==${PREVIOUS_JOB_ID}) | | |
select(.expired==false) | | |
.id") | |
echo "Artifact ID: $ARTIFACT_ID" | |
echo "ARTIFACT_ID=$ARTIFACT_ID" >> "$GITHUB_ENV" | |
PR_NUMBER=$(jq -r '.pull_requests[0].number' \ | |
<<< "$WORKFLOW_RUN_EVENT_OBJ") | |
echo "Pull request Number: $PR_NUMBER" | |
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV" | |
HEAD_SHA=$(jq -r '.pull_requests[0].head.sha' \ | |
<<< "$WORKFLOW_RUN_EVENT_OBJ") | |
echo "Head SHA: $HEAD_SHA" | |
echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_ENV" | |
# On an forked repo, pull_requests are empty above, making not being able to post a comment: https://github.com/orgs/community/discussions/25220#discussioncomment-7551168 | |
- name: Get PR ID` | |
if: ${{ env.PR_NUMBER == '' || env.PR_NUMBER == 'null' }} | |
env: | |
GITHUB_TOKEN: ${{ github.token }} | |
run: | | |
PR_NUMBER=$(gh pr view -R "${{ github.repository }}" "${{ github.event.workflow_run.head_repository.owner.login }}:${{ github.event.workflow_run.head_branch }}" --json "number" --jq ".number") | |
echo "Pull request Number (2nd method): $PR_NUMBER" | |
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV" | |
- name: Find Comment | |
uses: peter-evans/find-comment@v3 | |
id: find-comment | |
with: | |
issue-number: ${{ env.PR_NUMBER }} | |
comment-author: 'github-actions[bot]' | |
- name: Update Comment | |
env: | |
JOB_PATH: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ env.PREVIOUS_JOB_ID }}" | |
ARTIFACT_URL: "https://nightly.link/${{ github.repository }}/suites/${{ env.SUITE_ID }}/artifacts/${{ env.ARTIFACT_ID }}" | |
# HEAD_SHA: "${{ env.HEAD_SHA }}" # empty from a forked repo | |
HEAD_SHA: "${{ github.event.workflow_run.head_sha }}" | |
uses: peter-evans/create-or-update-comment@v4 | |
with: | |
issue-number: ${{ env.PR_NUMBER }} | |
comment-id: ${{ steps.find-comment.outputs.comment-id }} | |
edit-mode: replace | |
body: |- | |
Build Successful! You can find a link to the downloadable artifact below. | |
| Name | Link | | |
| -------- | ----------------------- | | |
| Commit | ${{ env.HEAD_SHA }} | | |
| Logs | ${{ env.JOB_PATH }} | | |
| Download | ${{ env.ARTIFACT_URL }} | | |