1
0
Files
upload-artifact/src/upload-artifact.ts
T

75 lines
2.5 KiB
TypeScript
Raw Normal View History

import * as core from '../node_modules/@actions/core/'
import {UploadOptions, create} from '../node_modules/@actions/artifact/lib/artifact'
2020-04-28 17:18:53 +02:00
import {findFilesToUpload} from './search'
import {getInputs} from './input-helper'
import {NoFileOptions} from './constants'
2020-04-28 17:18:53 +02:00
async function run(): Promise<void> {
try {
const inputs = getInputs()
const searchResult = await findFilesToUpload(inputs.searchPath)
2020-04-28 17:18:53 +02:00
if (searchResult.filesToUpload.length === 0) {
// No files were found, different use cases warrant different types of behavior if nothing is found
switch (inputs.ifNoFilesFound) {
case NoFileOptions.warn: {
core.warning(
`No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
)
break
}
case NoFileOptions.error: {
core.setFailed(
`No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
)
break
}
case NoFileOptions.ignore: {
core.info(
`No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
)
break
}
}
2020-04-28 17:18:53 +02:00
} else {
const s = searchResult.filesToUpload.length === 1 ? '' : 's'
2020-04-28 17:18:53 +02:00
core.info(
`With the provided path, there will be ${searchResult.filesToUpload.length} file${s} uploaded`
2020-04-28 17:18:53 +02:00
)
core.debug(`Root artifact directory is ${searchResult.rootDirectory}`)
if (searchResult.filesToUpload.length > 10000) {
core.warning(
2021-05-23 08:33:18 -04:00
`There are over 10,000 files in this artifact, consider creating an archive before upload to improve the upload performance.`
)
}
2020-04-28 17:18:53 +02:00
const artifactClient = create()
const options: UploadOptions = {}
2020-08-27 13:39:36 -04:00
if (inputs.retentionDays) {
options.retentionDays = inputs.retentionDays
}
const uploadResponse = await artifactClient.uploadArtifact(
inputs.artifactName,
2020-04-28 17:18:53 +02:00
searchResult.filesToUpload,
searchResult.rootDirectory,
options
)
if (uploadResponse.success === false) {
core.setFailed(
`An error was encountered when uploading ${inputs.artifactName}.`
)
} else {
core.info(
`Artifact ${inputs.artifactName} has been successfully uploaded! Final size is ${uploadResponse.size} bytes. Artifact ID is ${uploadResponse.id}}`
)
}
2020-04-28 17:18:53 +02:00
}
} catch (error) {
core.setFailed((error as Error).message)
2020-04-28 17:18:53 +02:00
}
}
run()