Skip to content

Commit

Permalink
github(artifact): upload build record as zip archive
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Mar 3, 2025
1 parent b5f9106 commit 6f4493a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 43 deletions.
4 changes: 0 additions & 4 deletions __tests__/github.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ maybe('uploadArtifact', () => {
fs.copyFileSync(path.join(fixturesDir, `github-repo.json`), filename);
const res = await GitHub.uploadArtifact({
filename: filename,
mimeType: 'application/json',
retentionDays: 1
});
expect(res).toBeDefined();
Expand Down Expand Up @@ -100,7 +99,6 @@ maybe('writeBuildSummary', () => {

const uploadRes = await GitHub.uploadArtifact({
filename: exportRes?.dockerbuildFilename,
mimeType: 'application/gzip',
retentionDays: 1
});
expect(uploadRes).toBeDefined();
Expand Down Expand Up @@ -180,7 +178,6 @@ maybe('writeBuildSummary', () => {

const uploadRes = await GitHub.uploadArtifact({
filename: exportRes?.dockerbuildFilename,
mimeType: 'application/gzip',
retentionDays: 1
});
expect(uploadRes).toBeDefined();
Expand Down Expand Up @@ -235,7 +232,6 @@ maybe('writeBuildSummary', () => {

const uploadRes = await GitHub.uploadArtifact({
filename: exportRes?.dockerbuildFilename,
mimeType: 'application/gzip',
retentionDays: 1
});
expect(uploadRes).toBeDefined();
Expand Down
54 changes: 16 additions & 38 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

import crypto from 'crypto';
import fs from 'fs';
import he from 'he';
import jsyaml from 'js-yaml';
import os from 'os';
Expand All @@ -24,15 +22,16 @@ import {CreateArtifactRequest, FinalizeArtifactRequest, StringValue} from '@acti
import {internalArtifactTwirpClient} from '@actions/artifact/lib/internal/shared/artifact-twirp-client';
import {isGhes} from '@actions/artifact/lib/internal/shared/config';
import {getBackendIdsFromToken} from '@actions/artifact/lib/internal/shared/util';
import {createZipUploadStream} from '@actions/artifact/lib/internal/upload/zip';
import {uploadZipToBlobStorage} from '@actions/artifact/lib/internal/upload/blob-upload';
import {UploadZipSpecification, getUploadZipSpecification} from '@actions/artifact/lib/internal/upload/upload-zip-specification';
import {getExpiration} from '@actions/artifact/lib/internal/upload/retention';
import {InvalidResponseError, NetworkError} from '@actions/artifact';
import {FilesNotFoundError, InvalidResponseError} from '@actions/artifact';
import * as core from '@actions/core';
import {SummaryTableCell} from '@actions/core/lib/summary';
import * as github from '@actions/github';
import {GitHub as Octokit} from '@actions/github/lib/utils';
import {Context} from '@actions/github/lib/context';
import {TransferProgressEvent} from '@azure/core-http';
import {BlobClient, BlobHTTPHeaders} from '@azure/storage-blob';
import {jwtDecode, JwtPayload} from 'jwt-decode';

import {Util} from './util';
Expand Down Expand Up @@ -143,6 +142,11 @@ export class GitHub {

core.info(`Uploading ${artifactName} to blob storage`);

const zipSpecification: UploadZipSpecification[] = getUploadZipSpecification([opts.filename], path.dirname(opts.filename));
if (zipSpecification.length === 0) {
throw new FilesNotFoundError(zipSpecification.flatMap(s => (s.sourcePath ? [s.sourcePath] : [])));
}

const createArtifactReq: CreateArtifactRequest = {
workflowRunBackendId: backendIds.workflowRunBackendId,
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
Expand All @@ -160,49 +164,23 @@ export class GitHub {
throw new InvalidResponseError('cannot create artifact client');
}

let uploadByteCount = 0;
const blobClient = new BlobClient(createArtifactResp.signedUploadUrl);
const blockBlobClient = blobClient.getBlockBlobClient();
const zipUploadStream = await createZipUploadStream(zipSpecification);

const headers: BlobHTTPHeaders = {
blobContentDisposition: `attachment; filename="${artifactName}"`
};
if (opts.mimeType) {
headers.blobContentType = opts.mimeType;
}
core.debug(`Upload headers: ${JSON.stringify(headers)}`);

try {
core.info('Beginning upload of artifact content to blob storage');
await blockBlobClient.uploadFile(opts.filename, {
blobHTTPHeaders: headers,
onProgress: (progress: TransferProgressEvent): void => {
core.info(`Uploaded bytes ${progress.loadedBytes}`);
uploadByteCount = progress.loadedBytes;
}
});
} catch (error) {
if (NetworkError.isNetworkErrorCode(error?.code)) {
throw new NetworkError(error?.code);
}
throw error;
}
const uploadResult = await uploadZipToBlobStorage(createArtifactResp.signedUploadUrl, zipUploadStream);

core.info('Finished uploading artifact content to blob storage!');

const sha256Hash = crypto.createHash('sha256').update(fs.readFileSync(opts.filename)).digest('hex');
core.info(`SHA256 hash of uploaded artifact is ${sha256Hash}`);

const finalizeArtifactReq: FinalizeArtifactRequest = {
workflowRunBackendId: backendIds.workflowRunBackendId,
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
name: artifactName,
size: uploadByteCount ? uploadByteCount.toString() : '0'
size: uploadResult.uploadSize ? uploadResult.uploadSize.toString() : '0'
};

if (sha256Hash) {
if (uploadResult.sha256Hash) {
core.info(`SHA256 hash of uploaded artifact is ${uploadResult.sha256Hash}`);
finalizeArtifactReq.hash = StringValue.create({
value: `sha256:${sha256Hash}`
value: `sha256:${uploadResult.sha256Hash}`
});
}

Expand All @@ -221,7 +199,7 @@ export class GitHub {
return {
id: Number(artifactId),
filename: artifactName,
size: uploadByteCount,
size: uploadResult.uploadSize || 0,
url: artifactURL
};
}
Expand Down
1 change: 0 additions & 1 deletion src/types/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export interface GitHubAnnotation extends AnnotationProperties {

export interface UploadArtifactOpts {
filename: string;
mimeType?: string;
retentionDays?: number;
}

Expand Down

0 comments on commit 6f4493a

Please sign in to comment.