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

132 lines
3.5 KiB
TypeScript
Raw Normal View History

import * as core from '@actions/core'
import {UploadOptions, UploadResponse} from '../shared/interfaces'
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'
import {uploadZipToBlobStorage} from './blob-upload'
import {createZipUploadStream} from './zip'
import {
CreateArtifactRequest,
FinalizeArtifactRequest,
StringValue
} from '../../generated'
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 the necessary backend ids which are required to create the artifact`
)
2023-08-09 11:26:33 -07:00
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,
2023-08-22 10:06:40 -07:00
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
const finalizeArtifactReq: FinalizeArtifactRequest = {
2023-08-09 12:09:17 -07:00
workflowRunBackendId: backendIds.workflowRunBackendId,
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
2023-08-22 10:06:40 -07:00
name,
size: uploadResult.uploadSize!.toString()
}
if (uploadResult.md5Hash) {
finalizeArtifactReq.hash = StringValue.create({
2023-08-22 10:06:40 -07:00
value: `md5:${uploadResult.md5Hash}`
})
}
core.info(`Finalizing artifact upload`)
const finalizeArtifactResp = await artifactClient.FinalizeArtifact(
finalizeArtifactReq
)
if (!finalizeArtifactResp.ok) {
2023-08-09 11:26:33 -07:00
core.warning(`Failed to finalize artifact`)
return {
success: false
}
}
const artifactId = BigInt(finalizeArtifactResp.artifactId)
core.info(
`Artifact ${name}.zip successfully finalized. Artifact ID ${artifactId}`
)
return {
success: true,
size: uploadResult.uploadSize,
id: Number(artifactId)
}
}