use new @actions/artifact version & update download logic

This commit is contained in:
Rob Herley 2023-08-24 11:57:52 -04:00
parent e9ef242655
commit 24b1443a07
No known key found for this signature in database
GPG key ID: D1602042C3543B06
6 changed files with 113053 additions and 3428 deletions

View file

@ -1,7 +1,11 @@
export enum Inputs {
Name = 'name',
Path = 'path'
Path = 'path',
GitHubToken = 'github-token',
Repository = 'repository',
RunID = 'run-id'
}
export enum Outputs {
DownloadPath = 'download-path'
}

View file

@ -1,61 +1,96 @@
import * as os from 'os'
import * as path from 'path'
import * as core from '@actions/core'
import * as artifact from '@actions/artifact'
import * as os from 'os'
import {resolve} from 'path'
import {Inputs, Outputs} from './constants'
const PARALLEL_DOWNLOADS = 5
export const chunk = <T>(arr: T[], n: number): T[][] =>
arr.reduce((acc, cur, i) => {
const index = Math.floor(i / n)
acc[index] = [...(acc[index] || []), cur]
return acc
}, [] as T[][])
async function run(): Promise<void> {
try {
const name = core.getInput(Inputs.Name, {required: false})
const path = core.getInput(Inputs.Path, {required: false})
let resolvedPath
// resolve tilde expansions, path.replace only replaces the first occurrence of a pattern
if (path.startsWith(`~`)) {
resolvedPath = resolve(path.replace('~', os.homedir()))
} else {
resolvedPath = resolve(path)
}
core.debug(`Resolved path is ${resolvedPath}`)
const artifactClient = artifact.create()
if (!name) {
// download all artifacts
core.info('No artifact name specified, downloading all artifacts')
core.info(
'Creating an extra directory for each artifact that is being downloaded'
)
const downloadResponse = await artifactClient.downloadAllArtifacts(
resolvedPath
)
core.info(`There were ${downloadResponse.length} artifacts downloaded`)
for (const artifact of downloadResponse) {
core.info(
`Artifact ${artifact.artifactName} was downloaded to ${artifact.downloadPath}`
)
}
} else {
// download a single artifact
core.info(`Starting download for ${name}`)
const downloadOptions = {
createArtifactFolder: false
}
const downloadResponse = await artifactClient.downloadArtifact(
name,
resolvedPath,
downloadOptions
)
core.info(
`Artifact ${downloadResponse.artifactName} was downloaded to ${downloadResponse.downloadPath}`
)
}
// 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')
} catch (err) {
core.setFailed(err.message)
const inputs = {
name: core.getInput(Inputs.Name, {required: false}),
path: core.getInput(Inputs.Path, {required: false}),
token: core.getInput(Inputs.GitHubToken, {required: true}),
repository: core.getInput(Inputs.Repository, {required: true}),
runID: parseInt(core.getInput(Inputs.RunID, {required: true}))
}
if (inputs.path.startsWith(`~`)) {
inputs.path = inputs.path.replace('~', os.homedir())
}
const resolvedPath = path.resolve(inputs.path)
core.debug(`Resolved path is ${resolvedPath}`)
const [owner, repo] = inputs.repository.split('/')
if (!owner || !repo) {
throw new Error(
`Invalid repository: '${inputs.repository}'. Must be in format owner/repo`
)
}
const artifactClient = artifact.create()
let artifacts: artifact.Artifact[] = []
if (inputs.name) {
const {artifact: targetArtifact} = await artifactClient.getArtifact(
inputs.name,
inputs.runID,
owner,
repo,
inputs.token
)
if (!targetArtifact) {
throw new Error(`Artifact '${inputs.name}' not found`)
}
core.debug(
`Found named artifact '${inputs.name}' (ID: ${targetArtifact.id}, Size: ${targetArtifact.size})`
)
artifacts = [targetArtifact]
} else {
const listArtifactResponse = await artifactClient.listArtifacts(
inputs.runID,
owner,
repo,
inputs.token
)
if (listArtifactResponse.artifacts.length === 0) {
throw new Error(
`No artifacts found for run '${inputs.runID}' in '${inputs.repository}'`
)
}
core.debug(`Found ${listArtifactResponse.artifacts.length} artifacts`)
artifacts = listArtifactResponse.artifacts
}
const downloadPromises = artifacts.map(artifact =>
artifactClient.downloadArtifact(artifact.id, owner, repo, inputs.token, {
path: resolvedPath
})
)
const chunkedPromises = chunk(downloadPromises, PARALLEL_DOWNLOADS)
for (const chunk of chunkedPromises) {
await Promise.all(chunk)
}
core.info(`Total of ${artifacts.length} artifact(s) downloaded`)
core.setOutput(Outputs.DownloadPath, resolvedPath)
core.info('Download artifact has finished successfully')
}
run()
run().catch(err =>
core.setFailed(`Unable to download artifact(s): ${err.message}`)
)