Skip to content

Comment Artifacts

Comment Artifacts #93

# 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:
PR_NUMBER: ${{ steps.metadata.outputs.PR_NUMBER }}
HEAD_SHA: ${{ steps.metadata.outputs.HEAD_SHA }}
ARTIFACT_ID: ${{ steps.metadata.outputs.ARTIFACT_ID }}
ARTIFACT_URL: ${{ steps.metadata.outputs.ARTIFACT_URL }}
ARTIFACT_EXP: ${{ steps.metadata.outputs.ARTIFACT_EXP }}
ARTIFACT_NAME: ${{ steps.metadata.outputs.ARTIFACT_NAME }}
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) }}
PREVIOUS_JOB_ID: ${{ github.event.workflow_run.id }}
run: |
echo "Previous Job ID: $PREVIOUS_JOB_ID"
LOG_URL="/repos/$OWNER/$REPO/actions/runs/$PREVIOUS_JOB_ID/logs"
echo "Getting previous logs: $LOG_URL"
gh api "$LOG_URL" >_logs.zip
echo "Unzipping logs"
unzip -p _logs.zip >_build.txt
echo "Parsing logs"
function parse_var {
name=$1
echo "Parsing output $name"
line=$(cat _build.txt | grep -m 1 "output:${name}=" | cat)
export parsed=${line#*"output:${name}="}
}
parse_var PR_NUMBER
PR_NUMBER=$parsed
echo "PR Number: $PR_NUMBER"
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_OUTPUT"
parse_var HEAD_SHA
HEAD_SHA=$parsed
echo "Head sha: $HEAD_SHA"
echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_OUTPUT"
parse_var ARTIFACT_ID
ARTIFACT_ID=$parsed
echo "ARTIFACT ID: $ARTIFACT_ID"
echo "ARTIFACT_ID=$ARTIFACT_ID" >> "$GITHUB_OUTPUT"
parse_var ARTIFACT_URL
ARTIFACT_URL=$parsed
echo "ARTIFACT URL: $ARTIFACT_URL"
echo "ARTIFACT_URL=$ARTIFACT_URL" >> "$GITHUB_OUTPUT"
if [[ "$ARTIFACT_ID" != "" ]]; then
ARTIFACT_INFO="$(gh api "/repos/$OWNER/$REPO/actions/artifacts/$ARTIFACT_ID")"
ARTIFACT_EXP=$(echo "$ARTIFACT_INFO" | jq -r ".expires_at")
echo "ARTIFACT EXP: $ARTIFACT_EXP"
echo "ARTIFACT_EXP=$ARTIFACT_EXP" >> "$GITHUB_OUTPUT"
ARTIFACT_NAME=$(echo "$ARTIFACT_INFO" | jq -r ".name")
echo "ARTIFACT NAME: $ARTIFACT_NAME"
echo "ARTIFACT_NAME=$ARTIFACT_NAME" >> "$GITHUB_OUTPUT"
fi
exit 0
comment-success:
if: ${{ needs.parse-metadata.outputs.PR_NUMBER != '' }}
runs-on: ubuntu-latest
needs:
- parse-metadata
steps:
- name: Find Comment
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ needs.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/${{ github.event.workflow_run.id }}"
HEAD_SHA: ${{ needs.parse-metadata.outputs.HEAD_SHA }}
ARTIFACT_URL: ${{ needs.parse-metadata.outputs.ARTIFACT_URL }}
ARTIFACT_EXP: ${{ needs.parse-metadata.outputs.ARTIFACT_EXP }}
ARTIFACT_NAME: ${{ needs.parse-metadata.outputs.ARTIFACT_NAME }}
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ needs.parse-metadata.outputs.PR_NUMBER }}
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
body: |-
## Build Preview
[![badge]](${{ env.JOB_PATH }})
You can find files attached to the below linked Workflow Run URL (Logs).
| Name | Link |
|-----------|-------------------------|
| Commit | ${{ env.HEAD_SHA }} |
| Logs | ${{ env.JOB_PATH }} |
${{ env.ARTIFACT_URL && format('| Jar Files | [{0}]({1}) |', env.ARTIFACT_NAME, env.ARTIFACT_URL) || '' }}
${{ env.ARTIFACT_EXP && format('| Expires At | {0} |', env.ARTIFACT_EXP) || '' }}
[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