|
| 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