mirror of
https://github.com/actions/upload-artifact.git
synced 2025-06-29 04:14:15 +00:00
feat: add upload one artifact per file
Signed-off-by: initdc <initd@outlook.com>
This commit is contained in:
parent
3cea537223
commit
35e561c49c
6 changed files with 365 additions and 48 deletions
|
@ -3,10 +3,12 @@ import {create, UploadOptions} from '@actions/artifact'
|
|||
import {findFilesToUpload} from './search'
|
||||
import {getInputs} from './input-helper'
|
||||
import {NoFileOptions} from './constants'
|
||||
import {UploadInputs, UploadPerFile} from './upload-inputs'
|
||||
import path from 'path'
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
const inputs = getInputs()
|
||||
const inputs: UploadInputs | UploadPerFile = getInputs()
|
||||
const searchResult = await findFilesToUpload(inputs.searchPath)
|
||||
if (searchResult.filesToUpload.length === 0) {
|
||||
// No files were found, different use cases warrant different types of behavior if nothing is found
|
||||
|
@ -51,21 +53,85 @@ async function run(): Promise<void> {
|
|||
options.retentionDays = inputs.retentionDays
|
||||
}
|
||||
|
||||
const uploadResponse = await artifactClient.uploadArtifact(
|
||||
inputs.artifactName,
|
||||
searchResult.filesToUpload,
|
||||
searchResult.rootDirectory,
|
||||
options
|
||||
)
|
||||
const artifactsName = inputs['artifactsName'] || 'artifacts'
|
||||
const artifactPerFile = inputs['artifactPerFile'] || false
|
||||
if (!artifactPerFile) {
|
||||
const uploadResponse = await artifactClient.uploadArtifact(
|
||||
artifactsName,
|
||||
searchResult.filesToUpload,
|
||||
searchResult.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.`
|
||||
)
|
||||
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!`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
core.info(
|
||||
`Artifact ${uploadResponse.artifactName} has been successfully uploaded!`
|
||||
)
|
||||
const filesToUpload = searchResult.filesToUpload
|
||||
const SuccessedItems: string[] = []
|
||||
const FailedItems: string[] = []
|
||||
|
||||
const artifactNameRule = inputs['artifactNameRule']
|
||||
for (let i = 0; i < filesToUpload.length; i++) {
|
||||
const file = filesToUpload[i]
|
||||
core.info(file)
|
||||
|
||||
const pathObject = path.parse(file)
|
||||
let artifactName = artifactNameRule
|
||||
for (const key of Object.keys(pathObject)) {
|
||||
const re = `$\{${key}}`
|
||||
if (artifactNameRule.includes(re)) {
|
||||
const value = pathObject[key] || ''
|
||||
artifactName = artifactName.replace(re, value)
|
||||
}
|
||||
}
|
||||
if (artifactName.includes(path.sep)) {
|
||||
core.warning(`${artifactName} includes ${path.sep}`)
|
||||
artifactName = artifactName.split(path.sep).join('_')
|
||||
}
|
||||
if (artifactName.includes(':')) {
|
||||
core.warning(`${artifactName} includes :`)
|
||||
artifactName = artifactName.split(':').join('-')
|
||||
}
|
||||
core.info(artifactName)
|
||||
|
||||
const artifactItemExist = SuccessedItems.includes(artifactName)
|
||||
if (artifactItemExist) {
|
||||
const oldArtifactName = artifactName
|
||||
core.warning(`${artifactName} artifact alreay exist`)
|
||||
artifactName = `${i}__${artifactName}`
|
||||
core.warning(`${oldArtifactName} => ${artifactName}`)
|
||||
}
|
||||
|
||||
const uploadResponse = await artifactClient.uploadArtifact(
|
||||
artifactName,
|
||||
[file],
|
||||
searchResult.rootDirectory,
|
||||
options
|
||||
)
|
||||
if (uploadResponse.failedItems.length > 0) {
|
||||
FailedItems.push(artifactName)
|
||||
} else {
|
||||
SuccessedItems.push(artifactName)
|
||||
}
|
||||
}
|
||||
|
||||
if (FailedItems.length > 0) {
|
||||
let errMsg = `${FailedItems.length} artifacts failed to upload, they were:\n`
|
||||
errMsg += FailedItems.join('\n')
|
||||
core.setFailed(errMsg)
|
||||
}
|
||||
if (SuccessedItems.length > 0) {
|
||||
let infoMsg = `${SuccessedItems.length} artifacts has been successfully uploaded! They were:\n`
|
||||
infoMsg += SuccessedItems.join('\n')
|
||||
core.info(infoMsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue