Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github(artifact): upload build record as zip archive #614

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
55 changes: 15 additions & 40 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,20 @@ export class GitHub {
throw new InvalidResponseError('cannot create artifact client');
}

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

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;
}

core.info('Finished uploading artifact content to blob storage!');
const zipUploadStream = await createZipUploadStream(zipSpecification);

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

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) {
finalizeArtifactReq.hash = StringValue.create({
value: `sha256:${sha256Hash}`
value: `sha256:${uploadResult.sha256Hash}`
});
}

Expand All @@ -221,7 +196,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
Loading