Skip to content

Commit

Permalink
Add regexp filter for artifact name (#237)
Browse files Browse the repository at this point in the history

Co-authored-by: Dawid Dziurla <dawidd0811@gmail.com>
  • Loading branch information
EnricoMi and dawidd6 committed Apr 14, 2023
1 parent 021af15 commit 246dbf4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/download.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ jobs:
cat artifact/sha | grep $GITHUB_SHA
cat artifact1/sha1 | grep $GITHUB_SHA
cat artifact2/sha2 | grep $GITHUB_SHA
download-regexp:
runs-on: ubuntu-latest
needs: wait
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Download
uses: ./
with:
workflow: upload.yml
name: artifact.
name_is_regexp: true
- name: Test
run: |
cat artifact1/sha1 | grep $GITHUB_SHA
cat artifact2/sha2 | grep $GITHUB_SHA
! test -d artifact/artifact
! test -f artifact.zip
download-empty-conclusion:
runs-on: ubuntu-latest
needs: wait
Expand Down Expand Up @@ -185,3 +203,23 @@ jobs:
search_artifacts: true
- name: Test
run: cat artifact/sha | grep $GITHUB_SHA
download-regexp-with-search-artifacts:
runs-on: ubuntu-latest
needs: wait
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Download
uses: ./
with:
workflow: upload.yml
name: artifact.
name_is_regexp: true
path: artifact
search_artifacts: true
- name: Test
run: |
cat artifact/artifact1/sha1 | grep $GITHUB_SHA
cat artifact/artifact2/sha2 | grep $GITHUB_SHA
! test -d artifact/artifact/artifact
! test -f artifact/artifact.zip
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar
# will download all artifacts if not specified
# and extract them into respective subdirectories
# https://github.com/actions/download-artifact#download-all-artifacts
# is treated as a regular expression if input name_is_regexp is true
# will download only those artifacts with a name that matches this regular expression
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
name: artifact_name
# Optional, name is treated as a regular expression if set true
name_is_regexp: true
# Optional, a directory where to extract artifact(s), defaults to the current directory
path: extract_here
# Optional, defaults to current repo
repo: ${{ github.repository }}
# Optional, check the workflow run to whether it has an artifact
# then will get the last available artifact from the previous workflow
# default false, just try to download from the last one
check_artifacts: false
check_artifacts: false
# Optional, search for the last workflow run whose stored an artifact named as in `name` input
# default false
search_artifacts: false
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ inputs:
name:
description: Artifact name (download all artifacts if not specified)
required: false
name_is_regexp:
description: Treat artifact name as a regular expression and download only artifacts with matching names
required: false
default: false
path:
description: Where to unpack the artifact
required: false
Expand Down
11 changes: 9 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async function main() {
const [owner, repo] = core.getInput("repo", { required: true }).split("/")
const path = core.getInput("path", { required: true })
const name = core.getInput("name")
const nameIsRegExp = core.getBooleanInput("name_is_regexp")
const skipUnpack = core.getBooleanInput("skip_unpack")
const ifNoArtifactFound = core.getInput("if_no_artifact_found")
let workflow = core.getInput("workflow")
Expand Down Expand Up @@ -130,6 +131,9 @@ async function main() {
}
if (searchArtifacts) {
const artifact = artifacts.find((artifact) => {
if (nameIsRegExp) {
return artifact.name.match(name) !== null
}
return artifact.name == name
})
if (!artifact) {
Expand Down Expand Up @@ -166,9 +170,12 @@ async function main() {
run_id: runID,
})

// One artifact or all if `name` input is not specified.
// One artifact if 'name' input is specified, one or more if `name` is a regular expression, all otherwise.
if (name) {
filtered = artifacts.filter((artifact) => {
if (nameIsRegExp) {
return artifact.name.match(name) !== null
}
return artifact.name == name
})
if (filtered.length == 0) {
Expand Down Expand Up @@ -238,7 +245,7 @@ async function main() {
continue
}

const dir = name ? path : pathname.join(path, artifact.name)
const dir = name && !nameIsRegExp ? path : pathname.join(path, artifact.name)

fs.mkdirSync(dir, { recursive: true })

Expand Down

0 comments on commit 246dbf4

Please sign in to comment.