2020-04-28 15:45:21 +02:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as artifact from '@actions/artifact'
|
2020-07-30 12:01:38 +02:00
|
|
|
import * as os from 'os'
|
2020-07-15 12:09:31 +02:00
|
|
|
import {resolve} from 'path'
|
|
|
|
import {Inputs, Outputs} from './constants'
|
2020-04-28 15:45:21 +02:00
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
|
|
|
const name = core.getInput(Inputs.Name, {required: false})
|
|
|
|
const path = core.getInput(Inputs.Path, {required: false})
|
|
|
|
|
2020-07-30 12:01:38 +02:00
|
|
|
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}`)
|
|
|
|
|
2020-04-28 15:45:21 +02:00
|
|
|
const artifactClient = artifact.create()
|
|
|
|
if (!name) {
|
|
|
|
// download all artifacts
|
2020-07-15 12:09:31 +02:00
|
|
|
core.info('No artifact name specified, downloading all artifacts')
|
|
|
|
core.info(
|
|
|
|
'Creating an extra directory for each artifact that is being downloaded'
|
|
|
|
)
|
2020-07-30 12:01:38 +02:00
|
|
|
const downloadResponse = await artifactClient.downloadAllArtifacts(
|
|
|
|
resolvedPath
|
|
|
|
)
|
2020-04-28 15:45:21 +02:00
|
|
|
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
|
2020-07-15 12:09:31 +02:00
|
|
|
core.info(`Starting download for ${name}`)
|
2020-04-28 15:45:21 +02:00
|
|
|
const downloadOptions = {
|
|
|
|
createArtifactFolder: false
|
|
|
|
}
|
|
|
|
const downloadResponse = await artifactClient.downloadArtifact(
|
|
|
|
name,
|
2020-07-30 12:01:38 +02:00
|
|
|
resolvedPath,
|
2020-04-28 15:45:21 +02:00
|
|
|
downloadOptions
|
|
|
|
)
|
|
|
|
core.info(
|
|
|
|
`Artifact ${downloadResponse.artifactName} was downloaded to ${downloadResponse.downloadPath}`
|
|
|
|
)
|
|
|
|
}
|
2020-07-15 12:09:31 +02:00
|
|
|
// 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
|
2020-07-30 12:01:38 +02:00
|
|
|
core.setOutput(Outputs.DownloadPath, resolvedPath)
|
2020-04-28 15:45:21 +02:00
|
|
|
core.info('Artifact download has finished successfully')
|
|
|
|
} catch (err) {
|
|
|
|
core.setFailed(err.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run()
|