Skip to content

Commit 4ad8bc0

Browse files
tstellaryuxuanchen1997
authored andcommitted
[workflows] Avoid usage of access token in issue-write.yml (#94011)
Summary: 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 Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251441
1 parent 57b31d7 commit 4ad8bc0

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

.github/workflows/issue-write.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ jobs:
2424
github.event.workflow_run.conclusion == 'failure'
2525
)
2626
steps:
27+
- name: Fetch Sources
28+
uses: actions/checkout@v4
29+
with:
30+
sparse-checkout: |
31+
.github/workflows/unprivileged-download-artifact/action.yml
32+
sparse-checkout-cone-mode: false
2733
- name: 'Download artifact'
28-
uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935 # v4.1.1
34+
uses: ./.github/workflows/unprivileged-download-artifact
35+
id: download-artifact
2936
with:
30-
github-token: ${{ secrets.ISSUE_WRITE_DOWNLOAD_ARTIFACT }}
3137
run-id: ${{ github.event.workflow_run.id }}
32-
name: workflow-args
38+
artifact-name: workflow-args
3339

3440
- name: 'Comment on PR'
41+
if: steps.download-artifact.outputs.artifact-id != ''
3542
uses: actions/github-script@v3
3643
with:
3744
github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -144,5 +151,7 @@ jobs:
144151
});
145152
146153
- name: Dump comments file
147-
if: always()
154+
if: >-
155+
always() &&
156+
steps.download-artifact.outputs.artifact-id != ''
148157
run: cat comments
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Unprivileged Download Artifact
2+
description: >-
3+
Download artifacts from another workflow run without using an access token.
4+
inputs:
5+
run-id:
6+
description: >-
7+
The run-id for the workflow run that you want to download the artifact
8+
from. If ommitted it will download the most recently created artifact
9+
from the repo with the artifact-name.
10+
required: false
11+
artifact-name:
12+
desciption: The name of the artifact to download.
13+
required: true
14+
15+
16+
outputs:
17+
filename:
18+
description: >-
19+
The filename of the downloaded artifact or the empty string if the
20+
artifact was not found.
21+
value: ${{ steps.download-artifact.outputs.filename }}
22+
artifact-id:
23+
description: "The id of the artifact being downloaded."
24+
value: ${{ steps.artifact-url.outputs.id }}
25+
26+
27+
runs:
28+
using: "composite"
29+
steps:
30+
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea #v7.0.1
31+
id: artifact-url
32+
with:
33+
script: |
34+
var response;
35+
if (!"${{ inputs.run-id }}") {
36+
response = await github.rest.actions.listArtifactsForRepo({
37+
owner: context.repo.owner,
38+
repo: context.repo.repo,
39+
name: "${{ inputs.artifact-name }}"
40+
})
41+
} else {
42+
response = await github.rest.actions.listWorkflowRunArtifacts({
43+
owner: context.repo.owner,
44+
repo: context.repo.repo,
45+
run_id: "${{ inputs.run-id }}",
46+
name: "${{ inputs.artifact-name }}"
47+
})
48+
}
49+
50+
console.log(response)
51+
52+
for (artifact of response.data.artifacts) {
53+
console.log(artifact);
54+
}
55+
56+
if (response.data.artifacts.length == 0) {
57+
console.log("Could not find artifact ${{ inputs.artifact-name }} for workflow run ${{ inputs.run-id }}")
58+
return;
59+
}
60+
61+
const url_response = await github.rest.actions.downloadArtifact({
62+
owner: context.repo.owner,
63+
repo: context.repo.repo,
64+
artifact_id: response.data.artifacts[0].id,
65+
archive_format: "zip"
66+
})
67+
68+
core.setOutput("url", url_response.url);
69+
core.setOutput("id", response.data.artifacts[0].id);
70+
71+
- shell: bash
72+
if: steps.artifact-url.outputs.url != ''
73+
id: download-artifact
74+
run: |
75+
curl -L -o ${{ inputs.artifact-name }}.zip "${{ steps.artifact-url.outputs.url }}"
76+
echo "filename=${{ inputs.artifact-name }}.zip" >> $GITHUB_OUTPUT
77+
78+
- shell: bash
79+
if: steps.download-artifact.outputs.filename != ''
80+
run: |
81+
unzip ${{ steps.download-artifact.outputs.filename }}

0 commit comments

Comments
 (0)