Skip to content

Commit 306b047

Browse files
committed
Optionally override the "strip prefix"
Typically, build artifacts are downloaded in the form of a `.zip` file that contains the files, where the original path is prefixed by the artifact name. For example, an artifact name "heya" will be reflected in the downloaded `.zip` file by having all files inside the `heya/` subdirectory. For that reason, this Action strips that prefix from the extracted files. Under certain circumstances, it may be desirable to strip an even longer prefix, e.g. when downloading Git's `vcpkg` artifact. The corresponding files are all inside the `compat/vcbuild/vcpkg/` subdirectory, and if we want to cache the artifact files, we can only download them to an empty (or non-existing) directory, and `compat/` is definitely not empty once Git's source code is checked out. So let's offer the option to override the strip prefix. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 071c369 commit 306b047

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
lines changed

.github/workflows/test.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,17 @@ jobs:
3838
- name: Verify that the package was downloaded
3939
shell: bash
4040
run: test -f sparse/sparse_*.deb
41+
test-strip-prefix: # make sure the action works on a clean machine without building
42+
runs-on: windows-latest
43+
steps:
44+
- uses: actions/checkout@v2
45+
- name: Run this Action in-place
46+
uses: ./
47+
with:
48+
repository: git/git
49+
definitionId: 9
50+
path: compat/vcbuild/vcpkg
51+
stripPrefix: compat/vcbuild/vcpkg/
52+
- name: Verify that the artifact files were downloaded
53+
shell: bash
54+
run: compat/vcbuild/vcpkg/vcpkg.exe version

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ Every Azure Pipeline has a numerical identifier that is part of the URL. For exa
4646

4747
Pipelines can have an arbitrary number of artifacts, which are identified by a name. The `artifact` parameter specifies which one to download. It can be omitted if the given Pipeline run has only one artifact attached to it.
4848

49+
### Strip prefix
50+
51+
Pipeline artifacts can contain entire directory structures. The `stripPrefix` parameter allows filtering by a given path prefix; Any files matching that prefix will be written (after stripping the prefix), all other files will be skipped.
52+
4953
### Output location
5054

5155
By default, the artifact files will be stored in the current directory. The `path` parameter can be used to override that.

__tests__/downloader.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ test('can obtain build ID', async () => {
4949
mocked(fetch).mockReturnValue(
5050
Promise.resolve(new Response(JSON.stringify(buildIdResponse)))
5151
)
52-
const {cacheId} = await get('git-for-windows/git', '22', 'git-sdk-64-minimal')
52+
const {cacheId} = await get(
53+
'git-for-windows/git',
54+
'22',
55+
'git-sdk-64-minimal',
56+
undefined
57+
)
5358
expect(fetch).toHaveBeenCalledTimes(1)
5459
expect(cacheId).toEqual('git-for-windows/git-22-git-sdk-64-minimal-71000')
5560
})

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ inputs:
1414
artifact:
1515
required: false
1616
description: 'The name of the Azure Pipeline artifact'
17+
stripPrefix:
18+
required: false
19+
description: "Strip this prefix from the artifact files' paths; Defaults to '<artifact>/'"
1720
path:
1821
required: false
1922
description: 'Where to write the artifact file(s)'

main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ async function run(): Promise<void> {
88
const {artifactName, download, cacheId} = await get(
99
core.getInput('repository'),
1010
core.getInput('definitionId'),
11-
core.getInput('artifact')
11+
core.getInput('artifact'),
12+
core.getInput('stripPrefix')
1213
)
1314
const outputDirectory = core.getInput('path') || artifactName
1415
let useCache = core.getInput('cache') === 'true'

src/downloader.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ async function unzip(
9696
export async function get(
9797
repository: string,
9898
definitionId: string,
99-
artifactName: string
99+
artifactName: string,
100+
stripPrefix?: string
100101
): Promise<{
101102
artifactName: string
102103
cacheId: string
@@ -161,7 +162,12 @@ export async function get(
161162
let delayInSeconds = 1
162163
for (;;) {
163164
try {
164-
return await unzip(url, `${artifactName}/`, outputDirectory, verbose)
165+
return await unzip(
166+
url,
167+
stripPrefix || `${artifactName}/`,
168+
outputDirectory,
169+
verbose
170+
)
165171
} catch (e) {
166172
delayInSeconds *= 2
167173
if (delayInSeconds >= 60) {

0 commit comments

Comments
 (0)