Set up an action to share links to uploaded artifacts in PR comments #51403
-
Select Topic AreaQuestion BodyI would like to make a workflow which runs on
The first three parts are relatively easy for me to. But the last one gives me a minor headache as I'm not sure what the best way would be to access the URL(s) of the uploaded artifacts. Here's my current setup: name: 'Build PR changes'
on:
pull_request_target:
types:
- opened
- reopened
- synchronize
paths:
- 'bukkit/src/**'
- 'bungeecord/src/**'
- 'core/src/**'
- 'velocity/src/**'
concurrency: 'pr-preview'
jobs:
buildJars:
name: 'Build Jars from Pull request'
runs-on: ubuntu-latest
outputs:
comment_id: ${{ steps.find-comment.outputs.comment-id }}
steps:
- name: 'Checkout Repository'
uses: actions/checkout@v3
- name: 'Setup Java 16'
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: '16'
cache: 'maven'
- name: 'Build Jar files'
run: mvn clean install
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: asl-files
path: '*/target/AdvancedServerList-*.jar'
if-no-files-found: error
- name: Find Comment
uses: peter-evans/find-comment@v2
id: find-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
updateCommentSuccess:
name: 'Create or update Comment (On Success).'
needs: [buildJars]
if: success()
runs-on: ubuntu-latest
steps:
- name: Update Comment
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ needs.buildJars.outputs.comment_id }}
body: |-
## Build Preview
[![badge]](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
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 | ${{ github.event.pull_request.head.sha }} |
| Logs | ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| Jar Files | <!-- What can I put here? --> |
[badge]: https://img.shields.io/badge/Build-Success!-3fb950?logo=github&style=for-the-badge
- name: Update Comment
if: ${{ failure() }}
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ needs.buildJars.outputs.comment_id }}
body: |-
## Build Preview
[![badge]](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
There was an issue generating the Jar files from your Pull request.
Please review the logs linked below for further information.
| Name | Link |
|--------|-------------------------------------------------------------------------------------|
| Commit | ${{ github.event.pull_request.head.sha }} |
| Logs | ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
[badge]: https://img.shields.io/badge/Build-Failure!-f85149?logo=github&style=for-the-badge |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 24 replies
-
Hey, jumping over from the other conversation. You can get the artifact url in a separate job as you stated using the api "/repos/{owner}/{repo}/actions/artifacts". You can narrow it down to the workflow_run.id or the SHA if you so choose. |
Beta Was this translation helpful? Give feedback.
-
As mentioned in this other thread, I think this workflow template might be useful for you:
To do so, you'd just need to reuse the template and modify the 2 lines marked ↓ below name: Pin artifacts
on:
workflow_run:
workflows:
# todo: Update this list if need be ↓
- "Build PR changes"
types: ["completed"]
jobs:
# Make artifacts links more visible for the upstream project
pin-artifacts:
permissions:
statuses: write
name: Add artifacts links to commit statuses # todo: replace TODO-orga and TODO-repo ↓
if: ${{ github.event.workflow_run.conclusion == 'success' && github.repository == 'TODO-orga/TODO-repo' }}
runs-on: ubuntu-latest
steps:
- name: Add artifacts links to commit status
# …
# more omitted, cf. Link in bold on top
# … |
Beta Was this translation helpful? Give feedback.
-
Per the docs, I was under the impression that the download URL is from the API is only good for one minute.
I needed to do a similar thing (post a comment to the PR with a link to the artifact). What I ended up doing was getting a link to the workflow run and then add a url fragment to where the artifacts are displayed on the workflow run. |
Beta Was this translation helpful? Give feedback.
-
@erikmd Is giving you the framework of how this would work. The flow would look like so... Workflow 1: build/test/publish artifact I want to clarify something from my experience. This statement here: I mocked up an example you can using most of what you posted above. I admit this took longer than it should have. Got caught up on a letter in my json query. Workflow 1: # artifact-upload.yml
---
name: Artifact Upload
on:
pull_request:
types:
- opened
- reopened
- synchronize
jobs:
create-artifact:
env:
ARTIFACT_NAME: artifact.txt
ARTIFACT_PATH: ./
runs-on: ubuntu-latest
steps:
- name: Create something to artifact
run: |
# Here we just create something for the example
echo "stuff I want to artifact" > "$ARTIFACT_PATH/$ARTIFACT_NAME"
- name: Upload Artifact
id: upload
uses: actions/upload-artifact@v3.1.2
with:
name: ${{ env.ARTIFACT_NAME }}
path: ${{ env.ARTIFACT_PATH }}/${{ env.ARTIFACT_NAME }}
if-no-files-found: error Workflow 2: # pr-comment-artifact-url.yml
---
name: Comment Artifact URL on PR
on:
workflow_run:
types:
- "completed"
workflows:
- "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"
ARTIFACT_URL=$(gh api "/repos/$OWNER/$REPO/actions/artifacts" \
--jq ".artifacts.[] |
select(.workflow_run.id==${PREVIOUS_JOB_ID}) |
select(.expired==false) |
.archive_download_url")
echo "ARTIFACT URL: $ARTIFACT_URL"
echo "ARTIFACT_URL=$ARTIFACT_URL" >> "$GITHUB_ENV"
PR_NUMBER=$(jq -r '.pull_requests[0].number' \
<<< "$WORKFLOW_RUN_EVENT_OBJ")
echo "PR 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"
- name: Update Comment
env:
JOB_PATH: "${{ github.server_url }}/${{ github.repository }}/actions/\
runs/${{ env.PREVIOUS_JOB_ID }}"
HEAD_SHA: ${{ env.HEAD_SHA }}
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ env.PR_NUMBER }}
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 }}
| Jar Files | ${{ env. ARTIFACT_URL }}
[badge]: https://img.shields.io/badge/Build-Success!-3fb950?logo=github&style=for-the-badge
You can see the jobs here. You can see the PR here. |
Beta Was this translation helpful? Give feedback.
-
Coming back with a small question. Can I change the second workflow (The one that triggers on successful PR action) like this to avoid a second - ARTIFACT_ID=$(gh api "/repos/$OWNER/$REPO/actions/artifacts" \
+ ARTIFACT_OBJ=$(gh api "/repos/$OWNER/$REPO/actions/artifacts" \
+ --jq ".artifacts.[] |
- select(.workflow_run.id==${PREVIOUS_JOB_ID}) |
- select(.expired==false) |
- .id")
+ select(.workflow_run_id==${PREVIOUS_JOB_ID}) |
+ select(.expired==false)")
+ ARTIFACT_ID=$(jq -r '.id' <<< "$ARTIFACT_OBJ")
- ARTIFACT_EXPIRE_DATE=$(gh api "/repos/$OWNER/$REPO/actions/artifacts" \
- --jq ".artifacts.[] |
- select(.workflow_run.id==${PREVIOUS_JOB_ID}) |
- select(.expired==false) |
- .expires_at")
+ ARTIFACT_EXPIRE_DATE=$(jq -r '.expires_at' <<< "$ARTIFACT_OBJ") I haven't worked with gh api and jq at all in the past, so I'm not sure if I can just do a jq on an object retrieved from another jq here... |
Beta Was this translation helpful? Give feedback.
-
The output is now "actions/upload-artifact@v4". So we can simply reference it as ${{ steps.upload-artifacts.outputs.artifact-url }} to reference them. https://github.com/actions/upload-artifact?tab=readme-ov-file#outputs |
Beta Was this translation helpful? Give feedback.
@Andre601
@erikmd Is giving you the framework of how this would work. The flow would look like so...
Workflow 1: build/test/publish artifact
Workflow 2: At the completion of workflow 1 if completed this job will run and retrieve the url and you can code to post it to the PR.
I want to clarify something from my experience. This statement here:
I'm aware that the URL (from what I know) isn't provided/created by GitHub until the job has completed, which is why I would do the comment stuff on a separate job depending on the prior one.
isn't exactly correct. What's really happening is the URL is available until the workflow if complete, not the job. Trust me I and many others have tried using …