Skip to content

Commit 246dbf4

Browse files
EnricoMidawidd6
andauthored
Add regexp filter for artifact name (dawidd6#237)
Co-authored-by: Dawid Dziurla <dawidd0811@gmail.com>
1 parent 021af15 commit 246dbf4

4 files changed

Lines changed: 57 additions & 3 deletions

File tree

.github/workflows/download.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ jobs:
8888
cat artifact/sha | grep $GITHUB_SHA
8989
cat artifact1/sha1 | grep $GITHUB_SHA
9090
cat artifact2/sha2 | grep $GITHUB_SHA
91+
download-regexp:
92+
runs-on: ubuntu-latest
93+
needs: wait
94+
steps:
95+
- name: Checkout
96+
uses: actions/checkout@v3
97+
- name: Download
98+
uses: ./
99+
with:
100+
workflow: upload.yml
101+
name: artifact.
102+
name_is_regexp: true
103+
- name: Test
104+
run: |
105+
cat artifact1/sha1 | grep $GITHUB_SHA
106+
cat artifact2/sha2 | grep $GITHUB_SHA
107+
! test -d artifact/artifact
108+
! test -f artifact.zip
91109
download-empty-conclusion:
92110
runs-on: ubuntu-latest
93111
needs: wait
@@ -185,3 +203,23 @@ jobs:
185203
search_artifacts: true
186204
- name: Test
187205
run: cat artifact/sha | grep $GITHUB_SHA
206+
download-regexp-with-search-artifacts:
207+
runs-on: ubuntu-latest
208+
needs: wait
209+
steps:
210+
- name: Checkout
211+
uses: actions/checkout@v3
212+
- name: Download
213+
uses: ./
214+
with:
215+
workflow: upload.yml
216+
name: artifact.
217+
name_is_regexp: true
218+
path: artifact
219+
search_artifacts: true
220+
- name: Test
221+
run: |
222+
cat artifact/artifact1/sha1 | grep $GITHUB_SHA
223+
cat artifact/artifact2/sha2 | grep $GITHUB_SHA
224+
! test -d artifact/artifact/artifact
225+
! test -f artifact/artifact.zip

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,20 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar
4747
# will download all artifacts if not specified
4848
# and extract them into respective subdirectories
4949
# https://github.com/actions/download-artifact#download-all-artifacts
50+
# is treated as a regular expression if input name_is_regexp is true
51+
# will download only those artifacts with a name that matches this regular expression
52+
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
5053
name: artifact_name
54+
# Optional, name is treated as a regular expression if set true
55+
name_is_regexp: true
5156
# Optional, a directory where to extract artifact(s), defaults to the current directory
5257
path: extract_here
5358
# Optional, defaults to current repo
5459
repo: ${{ github.repository }}
5560
# Optional, check the workflow run to whether it has an artifact
5661
# then will get the last available artifact from the previous workflow
5762
# default false, just try to download from the last one
58-
check_artifacts: false
63+
check_artifacts: false
5964
# Optional, search for the last workflow run whose stored an artifact named as in `name` input
6065
# default false
6166
search_artifacts: false

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ inputs:
4747
name:
4848
description: Artifact name (download all artifacts if not specified)
4949
required: false
50+
name_is_regexp:
51+
description: Treat artifact name as a regular expression and download only artifacts with matching names
52+
required: false
53+
default: false
5054
path:
5155
description: Where to unpack the artifact
5256
required: false

main.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ async function main() {
2525
const [owner, repo] = core.getInput("repo", { required: true }).split("/")
2626
const path = core.getInput("path", { required: true })
2727
const name = core.getInput("name")
28+
const nameIsRegExp = core.getBooleanInput("name_is_regexp")
2829
const skipUnpack = core.getBooleanInput("skip_unpack")
2930
const ifNoArtifactFound = core.getInput("if_no_artifact_found")
3031
let workflow = core.getInput("workflow")
@@ -130,6 +131,9 @@ async function main() {
130131
}
131132
if (searchArtifacts) {
132133
const artifact = artifacts.find((artifact) => {
134+
if (nameIsRegExp) {
135+
return artifact.name.match(name) !== null
136+
}
133137
return artifact.name == name
134138
})
135139
if (!artifact) {
@@ -166,9 +170,12 @@ async function main() {
166170
run_id: runID,
167171
})
168172

169-
// One artifact or all if `name` input is not specified.
173+
// One artifact if 'name' input is specified, one or more if `name` is a regular expression, all otherwise.
170174
if (name) {
171175
filtered = artifacts.filter((artifact) => {
176+
if (nameIsRegExp) {
177+
return artifact.name.match(name) !== null
178+
}
172179
return artifact.name == name
173180
})
174181
if (filtered.length == 0) {
@@ -238,7 +245,7 @@ async function main() {
238245
continue
239246
}
240247

241-
const dir = name ? path : pathname.join(path, artifact.name)
248+
const dir = name && !nameIsRegExp ? path : pathname.join(path, artifact.name)
242249

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

0 commit comments

Comments
 (0)