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

124 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-03-15 14:53:33 +00:00
import whyIsNodeRunning from 'why-is-node-running'
import * as core from '@actions/core'
2023-12-03 21:13:55 +00:00
import {
UploadArtifactOptions,
UploadArtifactResponse
} from '../shared/interfaces'
2023-08-09 17:50:46 -07:00
import {getExpiration} from './retention'
import {validateArtifactName} from './path-and-artifact-name-validation'
import {internalArtifactTwirpClient} 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'
import {FilesNotFoundError, InvalidResponseError} from '../shared/errors'
export async function uploadArtifact(
name: string,
files: string[],
rootDirectory: string,
2023-12-03 21:13:55 +00:00
options?: UploadArtifactOptions | undefined
): Promise<UploadArtifactResponse> {
validateArtifactName(name)
validateRootDirectory(rootDirectory)
const zipSpecification: UploadZipSpecification[] = getUploadZipSpecification(
files,
rootDirectory
)
if (zipSpecification.length === 0) {
throw new FilesNotFoundError(
zipSpecification.flatMap(s => (s.sourcePath ? [s.sourcePath] : []))
)
}
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
// create the artifact client
const artifactClient = internalArtifactTwirpClient()
2023-08-09 11:26:33 -07:00
// 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-09-08 14:29:27 +00:00
const createArtifactResp =
await artifactClient.CreateArtifact(createArtifactReq)
if (!createArtifactResp.ok) {
throw new InvalidResponseError(
'CreateArtifact: response from backend was not ok'
)
2023-08-09 11:26:33 -07:00
}
const zipUploadStream = await createZipUploadStream(
zipSpecification,
options?.compressionLevel
)
// Upload zip to blob storage
const uploadResult = await uploadZipToBlobStorage(
createArtifactResp.signedUploadUrl,
zipUploadStream
)
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,
2023-08-23 07:28:17 -07:00
size: uploadResult.uploadSize ? uploadResult.uploadSize.toString() : '0'
}
if (uploadResult.sha256Hash) {
finalizeArtifactReq.hash = StringValue.create({
value: `sha256:${uploadResult.sha256Hash}`
})
}
core.info(`Finalizing artifact upload`)
2023-09-08 14:29:27 +00:00
const finalizeArtifactResp =
await artifactClient.FinalizeArtifact(finalizeArtifactReq)
if (!finalizeArtifactResp.ok) {
throw new InvalidResponseError(
'FinalizeArtifact: response from backend was not ok'
)
2023-08-09 11:26:33 -07:00
}
const artifactId = BigInt(finalizeArtifactResp.artifactId)
core.info(
`Artifact ${name}.zip successfully finalized. Artifact ID ${artifactId}`
)
2024-03-15 14:53:33 +00:00
if (core.isDebug()) {
setTimeout(function () {
core.debug('Processes keeping upload stream running:')
whyIsNodeRunning()
}, 500)
}
//
return {
size: uploadResult.uploadSize,
id: Number(artifactId)
}
}