feat: support downloading multiple artifacts to different paths

There is a few use cases where we want to download a few different artifacts but
not all of them. This commit implements this support, without breaking backward
compatibility. Two build test cases were also added to the pipeline.
This commit is contained in:
Diogo Kiss 2022-10-13 13:54:16 +02:00
parent fbb8edeffa
commit 569e039f2a
4 changed files with 2294 additions and 120 deletions

View file

@ -89,7 +89,56 @@ jobs:
}
shell: pwsh
# Test downloading both artifacts at once
# Test downloading multiple artifacts to the same path
- name: Download artifacts A and B to the same path
uses: ./
with:
name: |
Artifact-A
Artifact-B
path: some/path/for/multiple/files
- name: Verify successful download
run: |
$fileA = "some/path/for/multiple/files/file-A.txt"
$fileB = "some/path/for/multiple/files/file-B.txt"
if(!(Test-Path -path $fileA) -or !(Test-Path -path $fileB))
{
Write-Error "Expected files do not exist"
}
if(!((Get-Content $fileA) -ceq "Lorem ipsum dolor sit amet") -or !((Get-Content $fileB) -ceq "Hello world from file B"))
{
Write-Error "File contents of downloaded artifacts are incorrect"
}
shell: pwsh
# Test downloading multiple artifacts to different paths
- name: Download artifacts A and B to different paths
uses: ./
with:
name: |
Artifact-A
Artifact-B
path: |
some/path/for/a
some/path/for/b
- name: Verify successful download
run: |
$fileA = "some/path/for/a/file-A.txt"
$fileB = "some/path/for/b/file-B.txt"
if(!(Test-Path -path $fileA) -or !(Test-Path -path $fileB))
{
Write-Error "Expected files do not exist"
}
if(!((Get-Content $fileA) -ceq "Lorem ipsum dolor sit amet") -or !((Get-Content $fileB) -ceq "Hello world from file B"))
{
Write-Error "File contents of downloaded artifacts are incorrect"
}
shell: pwsh
# Test downloading all artifacts
- name: Download all Artifacts
uses: ./
with:
@ -108,5 +157,3 @@ jobs:
Write-Error "File contents of downloaded artifacts are incorrect"
}
shell: pwsh

2150
dist/index.js vendored

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,7 @@
export enum Inputs {
Name = 'name',
Path = 'path'
Names = 'name',
Paths = 'path'
}
export enum Outputs {
DownloadPath = 'download-path'
DownloadPaths = 'download-path'
}

View file

@ -4,11 +4,7 @@ import * as os from 'os'
import {resolve} from 'path'
import {Inputs, Outputs} from './constants'
async function run(): Promise<void> {
try {
const name = core.getInput(Inputs.Name, {required: false})
const path = core.getInput(Inputs.Path, {required: false})
async function downloadArtifact(name: string, path: string): Promise<string> {
let resolvedPath
// resolve tilde expansions, path.replace only replaces the first occurrence of a pattern
if (path.startsWith(`~`)) {
@ -51,8 +47,75 @@ async function run(): Promise<void> {
}
// output the directory that the artifact(s) was/were downloaded to
// if no path is provided, an empty string resolves to the current working directory
core.setOutput(Outputs.DownloadPath, resolvedPath)
core.info('Artifact download has finished successfully')
return resolvedPath
}
async function run(): Promise<void> {
try {
const names = core.getMultilineInput(Inputs.Names, {required: false})
const paths = core.getMultilineInput(Inputs.Paths, {required: false})
core.info(`names: '${JSON.stringify(names)}' | length: ${names.length}`)
core.info(`paths: '${JSON.stringify(paths)}' | length: ${paths.length}`)
let downloadPaths: string[] = []
// Names is set and has fewer entries than Paths
if (names.length !== 0 && paths.length > names.length) {
throw Error(
`The input 'path' cannot have more entries than 'name', if 'name' is set.`
)
}
// Names is NOT set and Paths has more than 1 entry
else if (names.length === 0 && paths.length > 1) {
throw Error(
`The input 'path' cannot have more than one entry, if 'name' is not set.`
)
}
// Names is NOT set and path has at max 1 entry: download all artifacts
else if (names.length === 0 && paths.length <= 1) {
const name = names.toString() // ''
const path = paths.toString() // '' or 'some/path'
const downloadPath = await downloadArtifact(name, path)
downloadPaths.push(downloadPath)
}
// Names has one or more entries and Paths has at max 1 entry
else if (names.length >= 1 && paths.length <= 1) {
const path = paths.toString() // '' or 'some/path'
names.forEach(async name => {
const downloadPath = await downloadArtifact(name, path)
downloadPaths.push(downloadPath)
})
}
// Names and Paths have the same numbers of entries (more than 1)
else if (
names.length > 1 &&
paths.length > 1 &&
names.length === paths.length
) {
names.forEach(async (name, index) => {
const path = paths[index]
const downloadPath = await downloadArtifact(name, path)
downloadPaths.push(downloadPath)
})
}
// Unhandled exception
else {
throw Error(
`Unhandled scenario. This shouldn't happen. It's very likely a bug. :-()`
)
}
// Remove duplicates and empty strings
downloadPaths = [...new Set(downloadPaths.filter(path => path !== ''))]
// Returns a newline-separated list of paths
const output = downloadPaths.join('\n')
// output the directory that the artifact(s) was/were downloaded to
core.setOutput(Outputs.DownloadPaths, output)
} catch (err) {
core.setFailed(err.message)
}