Skip to content

[workflows] Avoid usage of access token in issue-write.yml #94011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions .github/workflows/issue-write.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ jobs:
if: >
github.event.workflow_run.event == 'pull_request'
steps:
- name: Fetch Sources
uses: actions/checkout@v4
with:
sparse-checkout: |
.github/workflows/unprivileged-download-artifact/action.yml
sparse-checkout-cone-mode: false
- name: 'Download artifact'
uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935 # v4.1.1
uses: ./.github/workflows/unprivileged-download-artifact
id: download-artifact
with:
github-token: ${{ secrets.ISSUE_WRITE_DOWNLOAD_ARTIFACT }}
run-id: ${{ github.event.workflow_run.id }}
name: workflow-args
artifact-name: workflow-args

- name: 'Comment on PR'
if: steps.download-artifact.outputs.artifact-id != ''
uses: actions/github-script@v3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -135,5 +142,7 @@ jobs:
});

- name: Dump comments file
if: always()
if: >-
always() &&
steps.download-artifact.outputs.artifact-id != ''
run: cat comments
81 changes: 81 additions & 0 deletions .github/workflows/unprivileged-download-artifact/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Unprivileged Download Artifact
description: >-
Download artifacts from another workflow run without using an access token.
inputs:
run-id:
description: >-
The run-id for the workflow run that you want to download the artifact
from. If ommitted it will download the most recently created artifact
from the repo with the artifact-name.
required: false
artifact-name:
desciption: The name of the artifact to download.
required: true


outputs:
filename:
description: >-
The filename of the downloaded artifact or the empty string if the
artifact was not found.
value: ${{ steps.download-artifact.outputs.filename }}
artifact-id:
description: "The id of the artifact being downloaded."
value: ${{ steps.artifact-url.outputs.id }}


runs:
using: "composite"
steps:
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea #v7.0.1
id: artifact-url
with:
script: |
var response;
if (!"${{ inputs.run-id }}") {
response = await github.rest.actions.listArtifactsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
name: "${{ inputs.artifact-name }}"
})
} else {
response = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: "${{ inputs.run-id }}",
name: "${{ inputs.artifact-name }}"
})
}

console.log(response)

for (artifact of response.data.artifacts) {
console.log(artifact);
}

if (response.data.artifacts.length == 0) {
console.log("Could not find artifact ${{ inputs.artifact-name }} for workflow run ${{ inputs.run-id }}")
return;
}

const url_response = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: response.data.artifacts[0].id,
archive_format: "zip"
})

core.setOutput("url", url_response.url);
core.setOutput("id", response.data.artifacts[0].id);

- shell: bash
if: steps.artifact-url.outputs.url != ''
id: download-artifact
run: |
curl -L -o ${{ inputs.artifact-name }}.zip "${{ steps.artifact-url.outputs.url }}"
echo "filename=${{ inputs.artifact-name }}.zip" >> $GITHUB_OUTPUT

- shell: bash
if: steps.download-artifact.outputs.filename != ''
run: |
unzip ${{ steps.download-artifact.outputs.filename }}
Loading