Skip to content

Comment on PR with JAR Artifact #55

Comment on PR with JAR Artifact

Comment on PR with JAR Artifact #55

Workflow file for this run

name: Comment on PR with JAR Artifact
on:
workflow_run:
workflows: ["Upload JAR Artifact on PR"]
types:
- completed
permissions:
pull-requests: write
contents: read
jobs:
comment-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Checkout this repo
uses: actions/checkout@v4
- name: Download workflow run event payload
run: |
gh api repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} \
> workflow_run_event.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Extract PR number and head SHA
id: extract
run: |
WORKFLOW_RUN_EVENT_OBJ=$(cat workflow_run_event.json)
# Extract head_sha directly
HEAD_SHA=$(jq -r '.head_sha' <<< "$WORKFLOW_RUN_EVENT_OBJ")
echo "HEAD_SHA=$HEAD_SHA"
# Try getting PR number from embedded pull_requests (sometimes empty)
PR_NUMBER=$(jq -r '.pull_requests[0].number // empty' <<< "$WORKFLOW_RUN_EVENT_OBJ")
# Fallback using gh CLI if PR number not found
if [ -z "$PR_NUMBER" ]; then
echo "PR number not found in event. Falling back to gh CLI..."
PR_NUMBER=$(gh pr list --state open --json number,headRefOid | jq -r --arg sha "$HEAD_SHA" '.[] | select(.headRefOid == $sha) | .number')
fi
echo "PR_NUMBER=$PR_NUMBER"
echo "HEAD_SHA=$HEAD_SHA"
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV"
echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_ENV"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download artifact metadata
id: download-artifact
run: |
ARTIFACTS_JSON=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}/artifacts)
echo "$ARTIFACTS_JSON" > artifacts.json
get_artifact_url() {
local name="$1"
jq -r --arg NAME "$name" '.artifacts[] | select(.name == $NAME) | .id' artifacts.json
}
LINUX_ID=$(get_artifact_url "dinosaur-exploder-linux")
MAC_ID=$(get_artifact_url "dinosaur-exploder-mac")
WIN_ID=$(get_artifact_url "dinosaur-exploder-win")
echo "LINUX_URL=https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}/artifacts/$LINUX_ID" >> "$GITHUB_ENV"
echo "MAC_URL=https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}/artifacts/$MAC_ID" >> "$GITHUB_ENV"
echo "WIN_URL=https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}/artifacts/$WIN_ID" >> "$GITHUB_ENV"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Find previous comment
id: find-comment
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ env.PR_NUMBER }}
comment-author: 'github-actions[bot]'
body-includes: '<!-- build-preview-comment -->'
- name: Create or update PR comment
env:
HEAD_SHA: ${{ env.HEAD_SHA }}
ARTIFACT_HUMAN_URL: ${{ env.ARTIFACT_HUMAN_URL }}
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ env.PR_NUMBER }}
comment-id: ${{ steps.find-comment.outputs.comment-id }}
edit-mode: replace
body: |-
<!-- build-preview-comment -->
## Build preview 🚀
[![badge]](${{ env.JOB_PATH }})
**Thanks** for your **contribution** to this open-source project! ❤️
A **new build** has been generated for this pull request:
- 🎮 **Game Artifacts**: 🐧 [Linux](${{ env.LINUX_URL }}) / 🍎 [macOS](${{ env.MAC_URL }}) / 🪟 [Windows](${{ env.WIN_URL }})
- 🧪 **Workflow Run**: [View Logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }})
- 🔖 **Commit**: `${{ env.HEAD_SHA }}`
> [!TIP]
> This JAR lets you test the latest version of the game from this PR.
⏳ **Note**: Artifacts auto-expire after ~90 days.
Happy testing & enjoy the game! 🎮
_The DinoBot Team_ 🦖
[badge]: https://img.shields.io/badge/Build-Success!-3fb950?logo=github&style=for-the-badge
comment-on-failure:
if: github.event.workflow_run.conclusion == 'failure'
runs-on: ubuntu-latest
steps:
- name: Checkout this repo
uses: actions/checkout@v4
- name: Download workflow run event payload
run: |
gh api repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} \
> workflow_run_event.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Extract PR number and head SHA
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WORKFLOW_RUN_EVENT_OBJ: ${{ toJSON(github.event.workflow_run) }}
run: |
WORKFLOW_RUN_EVENT_OBJ=$(cat workflow_run_event.json)
# Extract head_sha directly
HEAD_SHA=$(jq -r '.head_sha' <<< "$WORKFLOW_RUN_EVENT_OBJ")
echo "HEAD_SHA=$HEAD_SHA"
# Try getting PR number from embedded pull_requests (sometimes empty)
PR_NUMBER=$(jq -r '.pull_requests[0].number // empty' <<< "$WORKFLOW_RUN_EVENT_OBJ")
# Fallback using gh CLI if PR number not found
if [ -z "$PR_NUMBER" ]; then
echo "PR number not found in event. Falling back to gh CLI..."
PR_NUMBER=$(gh pr list --state open --json number,headRefOid | jq -r --arg sha "$HEAD_SHA" '.[] | select(.headRefOid == $sha) | .number')
fi
echo "PR_NUMBER=$PR_NUMBER"
echo "HEAD_SHA=$HEAD_SHA"
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV"
echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_ENV"
- name: Find previous comment
id: find-comment
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ env.PR_NUMBER }}
comment-author: 'github-actions[bot]'
body-includes: '<!-- build-preview-comment -->'
- name: Leave failure comment
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ env.PR_NUMBER }}
comment-id: ${{ steps.find-comment.outputs.comment-id }}
edit-mode: replace
body: |-
<!-- build-preview-comment -->
## ❌ Build Failed
[![badge-fail]](${{
github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }})
**Thanks** for your **contribution** to this open-source project! ❤️
Unfortunately the build for this pull request has **failed**:
- 🚩 **Workflow Run**: [View Logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }})
- 🔖 **Commit**: `${{ env.HEAD_SHA }}`
> [!TIP]
> Please check the logs and fix any issues before re-running the workflow.
Good luck with your debugging! 🐛 (or should I say _dino-bugging_? 🦖)
_The DinoBot Team_ 🦖
[badge-fail]: https://img.shields.io/badge/Build-Failed-red?logo=github&style=for-the-badge