Files
toolkit/packages/artifact/src/internal/upload/upload-artifact.ts
T

107 lines
3.1 KiB
TypeScript
Raw Normal View History

import * as core from '@actions/core'
import {UploadOptions} from './upload-options'
import {UploadResponse} from './upload-response'
2023-08-09 17:50:46 -07:00
import {getExpiration} from './retention'
import {validateArtifactName} from './path-and-artifact-name-validation'
2023-08-09 11:26:33 -07:00
import {createArtifactTwirpClient} from '../shared/artifact-twirp-client'
import {
UploadZipSpecification,
getUploadZipSpecification,
validateRootDirectory
} from './upload-zip-specification'
2023-08-09 13:12:30 -07:00
import {getBackendIdsFromToken} from '../shared/util'
2023-08-09 17:42:14 -07:00
import {CreateArtifactRequest} from 'src/generated'
import {uploadZipToBlobStorage} from './blob-upload'
import {createZipUploadStream} from './zip'
export async function uploadArtifact(
name: string,
files: string[],
rootDirectory: string,
options?: UploadOptions | undefined
): Promise<UploadResponse> {
validateArtifactName(name)
validateRootDirectory(rootDirectory)
const zipSpecification: UploadZipSpecification[] = getUploadZipSpecification(
files,
rootDirectory
)
if (zipSpecification.length === 0) {
core.warning(`No files were found to upload`)
return {
success: false
}
}
const zipUploadStream = await createZipUploadStream(zipSpecification)
2023-08-09 11:26:33 -07:00
// get the IDs needed for the artifact creation
const backendIds = getBackendIdsFromToken()
2023-08-09 11:26:33 -07:00
if (!backendIds.workflowRunBackendId || !backendIds.workflowJobRunBackendId) {
core.warning(`Failed to get backend ids`)
return {
success: false
}
}
2023-08-09 17:42:14 -07:00
core.debug(`Workflow Run Backend ID: ${backendIds.workflowRunBackendId}`)
core.debug(
`Workflow Job Run Backend ID: ${backendIds.workflowJobRunBackendId}`
)
2023-08-09 11:26:33 -07:00
// create the artifact client
const artifactClient = createArtifactTwirpClient('upload')
// create the artifact
const createArtifactReq: CreateArtifactRequest = {
workflowRunBackendId: backendIds.workflowRunBackendId,
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
name: name,
2023-08-09 11:26:33 -07:00
version: 4
}
// if there is a retention period, add it to the request
const expiresAt = getExpiration(options?.retentionDays)
if (expiresAt) {
createArtifactReq.expiresAt = expiresAt
}
2023-08-09 12:09:17 -07:00
const createArtifactResp = await artifactClient.CreateArtifact(
createArtifactReq
)
if (!createArtifactResp.ok) {
2023-08-09 11:26:33 -07:00
core.warning(`Failed to create artifact`)
return {
success: false
}
}
// Upload zip to blob storage
const uploadResult = await uploadZipToBlobStorage(createArtifactResp.signedUploadUrl, zipUploadStream)
if (uploadResult.isSuccess === false) {
return {
success: false
}
}
2023-08-09 11:26:33 -07:00
// finalize the artifact
2023-08-09 12:09:17 -07:00
const finalizeArtifactResp = await artifactClient.FinalizeArtifact({
workflowRunBackendId: backendIds.workflowRunBackendId,
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
name: name,
size: uploadResult.uploadSize!.toString()
2023-08-09 12:09:17 -07:00
})
if (!finalizeArtifactResp.ok) {
2023-08-09 11:26:33 -07:00
core.warning(`Failed to finalize artifact`)
return {
success: false
}
}
return {
success: true,
size: uploadResult.uploadSize,
2023-08-09 11:26:33 -07:00
id: parseInt(finalizeArtifactResp.artifactId) // TODO - will this be a problem due to the id being a bigint?
}
}