Skip to content

Commit f6779ff

Browse files
committed
[workflows] Avoid usage of access token in issue-write.yml
This adds a new composite workflow that allows you to download artifacts from other workflows without using an access token. actions/download-artifact from GitHub requires an access token in order to download artifacts from a different workflow, which is why we can't use it here if we want to avoid using a token. See https://github.com/actions/download-artifact?tab=readme-ov-file#download-artifacts-from-other-workflow-runs-or-repositories
1 parent 973821c commit f6779ff

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

.github/workflows/issue-write.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,22 @@ jobs:
1919
if: >
2020
github.event.workflow_run.event == 'pull_request'
2121
steps:
22+
- name: Fetch Sources
23+
uses: actions/checkout@v4
24+
with:
25+
sparse-checkout: |
26+
.github/workflows/unprivileged-download-artifact/action.yml
27+
sparse-checkout-cone-mode: false
2228
- name: 'Download artifact'
23-
uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935 # v4.1.1
29+
uses: ./.github/workflows/unprivileged-download-artifact
30+
id: download-artifact
2431
with:
25-
github-token: ${{ secrets.ISSUE_WRITE_DOWNLOAD_ARTIFACT }}
2632
run-id: ${{ github.event.workflow_run.id }}
27-
name: workflow-args
33+
artifact-name: workflow-args
34+
35+
- name: Unpack Artifact
36+
run: |
37+
unzip ${{ steps.download-artifact.outputs.filename }}
2838
2939
- name: 'Comment on PR'
3040
uses: actions/github-script@v3
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Unprivileged Download Artifact
2+
description: Download artifacts from another workflow run without using an access token.
3+
inputs:
4+
run-id:
5+
description: The run-id for the workflow run that you want to download the artifact from. If ommited it will download the most recently created artifact from the repo with the artifact-name.
6+
required: false
7+
artifact-name:
8+
desciption: The name of the artifact to download.
9+
required: true
10+
11+
12+
outputs:
13+
filename:
14+
description: "The filename of the downloaded artifact or the empty string if the artifact was not found."
15+
value: ${{ steps.download-artifact.outputs.filename }}
16+
artifact-id:
17+
description: "The id of the artifact being downloaded."
18+
value: ${{ steps.artifact-url.outputs.id }}
19+
20+
21+
runs:
22+
using: "composite"
23+
steps:
24+
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea #v7.0.1
25+
id: artifact-url
26+
with:
27+
script: |
28+
var response;
29+
if (!"${{ inputs.run-id }}") {
30+
response = await github.rest.actions.listArtifactsForRepo({
31+
owner: context.repo.owner,
32+
repo: context.repo.repo,
33+
name: "${{ inputs.artifact-name }}"
34+
})
35+
} else {
36+
response = await github.rest.actions.listWorkflowRunArtifacts({
37+
owner: context.repo.owner,
38+
repo: context.repo.repo,
39+
run_id: "${{ inputs.run-id }}",
40+
name: "${{ inputs.artifact-name }}"
41+
})
42+
}
43+
44+
console.log(response)
45+
46+
for (artifact of response.data.artifacts) {
47+
console.log(artifact);
48+
}
49+
50+
if (response.data.artifacts.length == 0) {
51+
console.log("Could not find artifact ${{ inputs.artifact-name }} for workflow run ${{ inputs.run-id }}")
52+
return;
53+
}
54+
55+
const url_response = await github.rest.actions.downloadArtifact({
56+
owner: context.repo.owner,
57+
repo: context.repo.repo,
58+
artifact_id: response.data.artifacts[0].id,
59+
archive_format: "zip"
60+
})
61+
62+
core.setOutput("url", url_response.url);
63+
core.setOutput("id", response.data.artifacts[0].id);
64+
65+
- shell: bash
66+
if: steps.artifact-url.outputs.url != ''
67+
id: download-artifact
68+
run: |
69+
curl -L -o ${{ inputs.artifact-name }}.zip "${{ steps.artifact-url.outputs.url }}"
70+
echo "filename=${{ inputs.artifact-name }}.zip" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)