upload-artifact/src/upload-artifact.ts

69 lines
2.3 KiB
TypeScript
Raw Normal View History

import * as core from '@actions/core'
2020-07-27 12:26:41 +03:00
import {create, UploadOptions, ArtifactClient} from '@actions/artifact'
import {Inputs, getDefaultArtifactName} from './constants'
import {findFilesToUpload} from './search'
2020-07-27 12:26:41 +03:00
import { basename } from 'path';
async function run(): Promise<void> {
try {
const name = core.getInput(Inputs.Name, {required: false})
const path = core.getInput(Inputs.Path, {required: true})
2020-07-27 12:26:41 +03:00
const skipArchive = core.getInput(Inputs.SkipArchive, {required: false})
const searchResult = await findFilesToUpload(path)
if (searchResult.filesToUpload.length === 0) {
core.warning(
`No files were found for the provided path: ${path}. No artifacts will be uploaded.`
)
} else {
core.info(
`With the provided path, there will be ${searchResult.filesToUpload.length} files uploaded`
)
core.debug(`Root artifact directory is ${searchResult.rootDirectory}`)
const artifactClient = create()
const options: UploadOptions = {
continueOnError: false
}
2020-07-27 12:26:41 +03:00
const uploadedArtifacts: string[] = [];
if (skipArchive) {
for (const file of searchResult.filesToUpload) {
const resultName = await uploadArtifacts(artifactClient, [file], searchResult.rootDirectory, options, basename(file));
resultName && uploadedArtifacts.push(resultName);
}
} else {
2020-07-27 12:26:41 +03:00
const resultName = await uploadArtifacts(artifactClient, searchResult.filesToUpload, searchResult.rootDirectory, options, name);
resultName && uploadedArtifacts.push(resultName);
}
2020-07-27 12:26:41 +03:00
}
} catch (err) {
core.setFailed(err.message)
}
}
2020-07-27 12:26:41 +03:00
async function uploadArtifacts(artifactClient: ArtifactClient, files: string[], rootDirectory: string, options: UploadOptions, name = getDefaultArtifactName()): Promise<string | undefined> {
const uploadResponse = await artifactClient.uploadArtifact(
name,
files,
rootDirectory,
options
)
if (uploadResponse.failedItems.length > 0) {
core.setFailed(
`An error was encountered when uploading ${uploadResponse.artifactName}. There were ${uploadResponse.failedItems.length} items that failed to upload.`
)
} else {
core.info(
`Artifact ${uploadResponse.artifactName} has been successfully uploaded!`
)
return uploadResponse.artifactName;
}
}
run()