From 66d5434f23e442936b4e9ad151f6d02cd4ca7437 Mon Sep 17 00:00:00 2001 From: Bassem Dghaidi <568794+Link-@users.noreply.github.com> Date: Mon, 10 Jun 2024 10:56:20 -0700 Subject: [PATCH] Add v2 cache upload --- packages/cache/src/cache.ts | 21 +++++++++++++----- .../src/internal/v2/upload/upload-cache.ts | 22 +++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 packages/cache/src/internal/v2/upload/upload-cache.ts diff --git a/packages/cache/src/cache.ts b/packages/cache/src/cache.ts index 5722c9eb..ac704209 100644 --- a/packages/cache/src/cache.ts +++ b/packages/cache/src/cache.ts @@ -6,7 +6,8 @@ import * as cacheHttpClient from './internal/cacheHttpClient' import * as cacheTwirpClient from './internal/cacheTwirpClient' import {createTar, extractTar, listTar} from './internal/tar' import {DownloadOptions, UploadOptions} from './options' -import {GetCachedBlobRequest} from './generated/results/api/v1/blobcache' +import {GetCacheBlobUploadURLRequest, GetCacheBlobUploadURLResponse} from './generated/results/api/v1/blobcache' +import {UploadCache} from './internal/v2/upload/upload-cache' export class ValidationError extends Error { constructor(message: string) { @@ -177,12 +178,12 @@ export async function saveCache( // TODO: REMOVE ME // Making a call to the service const twirpClient = cacheTwirpClient.internalBlobCacheTwirpClient() - const getBlobRequest: GetCachedBlobRequest = { - owner: "link-/test", - keys: ['test-123412631236126'], + const getSignedUploadURL: GetCacheBlobUploadURLRequest = { + organization: "github", + keys: [key], } - const getBlobResponse = await twirpClient.GetCachedBlob(getBlobRequest) - core.info(`GetCachedBlobResponse: ${JSON.stringify(getBlobResponse)}`) + const signedUploadURL: GetCacheBlobUploadURLResponse = await twirpClient.GetCacheBlobUploadURL(getSignedUploadURL) + core.info(`GetCacheBlobUploadURLResponse: ${JSON.stringify(signedUploadURL)}`) const compressionMethod = await utils.getCompressionMethod() let cacheId = -1 @@ -251,6 +252,14 @@ export async function saveCache( core.debug(`Saving Cache (ID: ${cacheId})`) await cacheHttpClient.saveCache(cacheId, archivePath, options) + + // Cache v2 upload + // inputs: + // - getSignedUploadURL + // - archivePath + core.debug(`Saving Cache v2: ${archivePath}`) + await UploadCache(signedUploadURL, archivePath) + } catch (error) { const typedError = error as Error if (typedError.name === ValidationError.name) { diff --git a/packages/cache/src/internal/v2/upload/upload-cache.ts b/packages/cache/src/internal/v2/upload/upload-cache.ts new file mode 100644 index 00000000..d709671f --- /dev/null +++ b/packages/cache/src/internal/v2/upload/upload-cache.ts @@ -0,0 +1,22 @@ +import * as core from '@actions/core' +import {GetCacheBlobUploadURLResponse} from '../../../generated/results/api/v1/blobcache' +import {BlobClient, BlockBlobParallelUploadOptions} from '@azure/storage-blob' + +export async function UploadCache( + uploadURL: GetCacheBlobUploadURLResponse, + archivePath: string, +): Promise<{}> { + core.debug(`Uploading cache to: ${uploadURL}`) + + // Specify data transfer options + const uploadOptions: BlockBlobParallelUploadOptions = { + blockSize: 4 * 1024 * 1024, // 4 MiB max block size + concurrency: 2, // maximum number of parallel transfer workers + maxSingleShotSize: 8 * 1024 * 1024, // 8 MiB initial transfer size + }; + + // Create blob client from container client + const blobClient: BlobClient = new BlobClient(uploadURL.urls[0]) + + return blobClient.uploadFile(archivePath, uploadOptions); +} \ No newline at end of file