Comment Artifacts #7
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: Comment Artifacts | ||
on: | ||
workflow_run: | ||
types: | ||
- "completed" | ||
workflows: | ||
- "Build And Test" | ||
permissions: | ||
actions: read | ||
attestations: read | ||
contents: read | ||
pull-requests: write | ||
jobs: | ||
parse-metadata: | ||
runs-on: ubuntu-latest | ||
outputs: | ||
PREVIOUS_JOB_ID: ${{ steps.metadata.outputs.PREVIOUS_JOB_ID }} | ||
PR_NUMBER: ${{ steps.metadata.outputs.PR_NUMBER }} | ||
HEAD_SHA: ${{ steps.metadata.outputs.HEAD_SHA }} | ||
ARTIFACT_URL: ${{ steps.metadata.outputs.ARTIFACT_URL }} | ||
steps: | ||
- name: Get Artifact URL & PR Info | ||
id: metadata | ||
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=${{ github.event.workflow_run.id }} | ||
echo "Previous Job ID: $PREVIOUS_JOB_ID" | ||
echo "PREVIOUS_JOB_ID=$PREVIOUS_JOB_ID" >> "$GITHUB_OUTPUT" | ||
gh api "/repos/$OWNER/$REPO/actions/runs/$PREVIOUS_JOB_ID/logs" >logs.zip | ||
unzip -p logs.zip 'Build/*.txt' >build.txt | ||
function parse_var() { | ||
name=$1 | ||
line=$(cat build.txt | grep "output:${name}=") | ||
export parsed=${line#*"output:${name}="} | ||
} | ||
parse_var PR_NUMBER | ||
PR_NUMBER=$parsed | ||
parse_var HEAD_SHA | ||
HEAD_SHA=$parsed | ||
parse_var ARTIFACT_URL | ||
ARTIFACT_URL=$parsed | ||
echo "ARTIFACT URL: $ARTIFACT_URL" | ||
echo "ARTIFACT_URL=$ARTIFACT_URL" >> "$GITHUB_OUTPUT" | ||
# PR_NUMBER=$(jq -r '.pull_requests[0].number' <<< "$WORKFLOW_RUN_EVENT_OBJ") | ||
echo "PR Number: $PR_NUMBER" | ||
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_OUTPUT" | ||
# HEAD_SHA=$(jq -r '.pull_requests[0].head.sha' <<< "$WORKFLOW_RUN_EVENT_OBJ") | ||
echo "Head sha: $HEAD_SHA" | ||
echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_OUTPUT" | ||
comment-success: | ||
if: ${{ jobs.parse-metadata.outputs.PR_NUMBER != '' }} | ||
Check failure on line 68 in .github/workflows/pr-comment-artifacts.yml
|
||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Find Comment | ||
uses: peter-evans/find-comment@v3 | ||
id: fc | ||
with: | ||
issue-number: ${{ jobs.parse-metadata.outputs.PR_NUMBER }} | ||
comment-author: 'github-actions[bot]' | ||
body-includes: '## Build Preview' | ||
- name: Update Comment | ||
env: | ||
JOB_PATH: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ jobs.parse-metadata.outputs.PREVIOUS_JOB_ID }}" | ||
HEAD_SHA: ${{ jobs.parse-metadata.outputs.HEAD_SHA }} | ||
ARTIFACT_URL: ${{ jobs.parse-metadata.outputs.ARTIFACT_URL }} | ||
uses: peter-evans/create-or-update-comment@v4 | ||
with: | ||
issue-number: ${{ jobs.parse-metadata.outputs.PR_NUMBER }} | ||
comment-id: ${{ steps.fc.outputs.comment-id }} | ||
edit-mode: replace | ||
body: |- | ||
## Build Preview | ||
[![badge]]($JOB_PATH) | ||
You can find files attached to the below linked Workflow Run URL (Logs). | ||
Please note that files only stay for around 90 days! | ||
| Name | Link | | ||
|-----------|-------------------------| | ||
| Commit | ${{ env.HEAD_SHA }} | | ||
| Logs | ${{ env.JOB_PATH }} | | ||
${{ env.ARTIFACT_URL && format('| Jar Files | {0} |', env.ARTIFACT_URL) || '' }} | ||
[badge]: https://img.shields.io/badge/${{ format('{0}-{1}', github.event.workflow_run.conclusion, github.event.workflow_run.conclusion == 'success' && '3fb950' || 'f85149') }}?style=for-the-badge&logo=github&label=build |