Skip to content

Post Build Artifact to PR #1

Post Build Artifact to PR

Post Build Artifact to PR #1

Workflow file for this run

name: Post Build Artifact to PR
# ref: https://medium.com/@vlyskouski/github-actions-auto-reply-in-pull-requests-with-a-link-to-build-artifacts-ae1ef55034d8
on:
workflow_run:
workflows: [Pull Request]
types: [completed]
# necessary to post a comment
permissions:
pull-requests: write
jobs:
build:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Get Artifacts Link
env:
WORKFLOW_RUN_EVENT_OBJ: ${{ toJSON(github.event.workflow_run) }}
GH_TOKEN: ${{ github.token }}
run: |
PREVIOUS_JOB_ID=$(jq -r '.id' <<< "$WORKFLOW_RUN_EVENT_OBJ")
SUITE_ID=$(jq -r '.check_suite_id' <<< "$WORKFLOW_RUN_EVENT_OBJ")
# Sample for a single artifact, can be improved for a multiple artifacts
ARTIFACT_ID=$(gh api "/repos/${{ github.repository }}/actions/artifacts" \
--jq ".artifacts.[] | select(.workflow_run.id==${PREVIOUS_JOB_ID}) | .id")
echo "ARTIFACT_URL=https://github.com/${{ github.repository }}/suites/${SUITE_ID}/artifacts/${ARTIFACT_ID}" >> $GITHUB_ENV
PR_NUMBER=$(jq -r '.pull_requests[0].number' <<< "$WORKFLOW_RUN_EVENT_OBJ")
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV
- uses: actions/github-script@v6
env:
PR_NUMBER: ${{ env.PR_NUMBER }}
PR_NOTES: |
Build artifacts:
| Name | Link |
|------|------|
| Protocol | [protocol (PR #${{ env.PR_NUMBER }})](${{ env.ARTIFACT_URL }}) |
with:
script: |
github.rest.issues.createComment({
issue_number: process.env.PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
body: process.env.PR_NOTES
})