Skip to content

Commit

Permalink
Allow providing custom owner and repo for download artifact action (#54)
Browse files Browse the repository at this point in the history
* Updating download artifact action

* Update readme

* Update download-github-workflow-artifact/README.md

* Update download-github-workflow-artifact/README.md

Co-authored-by: Rashid N H M <[email protected]>

* Testing changes to action

* Apply suggestions from code review

Co-authored-by: Rashid N H M <[email protected]>

* Update download-github-workflow-artifact/README.md

Co-authored-by: Rashid N H M <[email protected]>

---------

Co-authored-by: Rashid N H M <[email protected]>
  • Loading branch information
mudit2812 and rashidnhm authored Oct 25, 2023
1 parent 2dca5cd commit 7bfb944
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
11 changes: 10 additions & 1 deletion download-github-workflow-artifact/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ The following code snippet get the most recent successful build of a workflow, t
return workflowRunsSorted[0].id
```
And now you can use the output of this step to call this action:
And now you can use the output of this step to call this action. If the artifact is from another repository,
the `repository_owner` and `repository_name` parameters must be set to point to the repository from which
to download the artifact:
```yaml
- id: repo_information
run: |
echo "owner=${{ github.repository_owner }}" >> $GITHUB_OUTPUT
echo "name=$(basename ${{ github.repository }})" >> $GITHUB_OUTPUT
- uses: XanaduAI/cloud-actions/download-github-workflow-artifact@main
with:
repository_owner: ${{ steps.repo_information.outputs.owner }}
repository_name: ${{ steps.repo_information.outputs.name }}
workflow_run_id: ${{ steps.workflow_run_id.outputs.result }}
github_token: ${{ github.token }}
```
14 changes: 13 additions & 1 deletion download-github-workflow-artifact/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ description: |
Has a built-in retry mechanism with exponential backoff.
inputs:
repository_owner:
description: The owner of the repository containing the workflow
required: false
default: ''
repository_name:
description: The name of the repository containing the workflow without the .git extension
required: false
default: ''
workflow_run_id:
description: The workflow run id from which to download the artifacts
required: true
Expand Down Expand Up @@ -33,8 +41,12 @@ runs:
with:
github-token: ${{ inputs.github_token }}
script: |
const repoOwner = "${{ inputs.repository_owner }}" || context.repo.owner;
const repoName = "${{ inputs.repository_name }}" || context.repo.repo;
const script = require('${{ github.action_path }}/download.js');
await script({github, context},
await script({github},
repoOwner,
repoName,
${{ inputs.workflow_run_id }},
"${{ inputs.artifact_name_regex }}",
"${{ inputs.artifact_download_dir }}",
Expand Down
10 changes: 5 additions & 5 deletions download-github-workflow-artifact/download.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
let fs = require('fs');

module.exports = async ({github, context}, workflow_run_id, artifact_name_regex, artifact_download_dir, max_retry) => {
module.exports = async ({github}, repository_owner, repository_name, workflow_run_id, artifact_name_regex, artifact_download_dir, max_retry) => {
// Exponential backoff with a ceiling at 30 seconds
const backoffDelay = (retryCount) => new Promise(resolve => setTimeout(resolve, 100 * Math.min(300, 1 << retryCount)));

const downloadArtifact = async (artifact) => {
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
owner: repository_owner,
repo: repository_name,
artifact_id: artifact.id,
archive_format: 'zip'
});
fs.writeFileSync(`${artifact_download_dir}/${artifact.name}.zip`, Buffer.from(download.data));
};

let artifactsAllOpts = github.rest.actions.listWorkflowRunArtifacts.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
owner: repository_owner,
repo: repository_name,
run_id: workflow_run_id
});
let artifactsAll = await github.paginate(artifactsAllOpts);
Expand Down

0 comments on commit 7bfb944

Please sign in to comment.