github: use default client and skip archive when uploading artifact
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
4
.github/dependabot.yml
vendored
4
.github/dependabot.yml
vendored
@@ -16,10 +16,6 @@ updates:
|
|||||||
versioning-strategy: "increase"
|
versioning-strategy: "increase"
|
||||||
allow:
|
allow:
|
||||||
- dependency-type: "production"
|
- dependency-type: "production"
|
||||||
ignore:
|
|
||||||
- # we want to match the same version as the one used by @actions/artifact
|
|
||||||
# https://github.com/actions/toolkit/blob/ae38557bb0dba824cdda26ce787bd6b66cf07a83/packages/artifact/package.json#L46
|
|
||||||
dependency-name: "@azure/storage-blob"
|
|
||||||
labels:
|
labels:
|
||||||
- "dependencies"
|
- "dependencies"
|
||||||
- "bot"
|
- "bot"
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ maybe('upload', () => {
|
|||||||
fs.copyFileSync(path.join(fixturesDir, `github-repo.json`), filename);
|
fs.copyFileSync(path.join(fixturesDir, `github-repo.json`), filename);
|
||||||
const res = await GitHubArtifact.upload({
|
const res = await GitHubArtifact.upload({
|
||||||
filename: filename,
|
filename: filename,
|
||||||
mimeType: 'application/json',
|
|
||||||
retentionDays: 1
|
retentionDays: 1
|
||||||
});
|
});
|
||||||
expect(res).toBeDefined();
|
expect(res).toBeDefined();
|
||||||
|
|||||||
@@ -85,7 +85,6 @@ maybe('writeBuildSummary', () => {
|
|||||||
|
|
||||||
const uploadRes = await GitHubArtifact.upload({
|
const uploadRes = await GitHubArtifact.upload({
|
||||||
filename: exportRes?.dockerbuildFilename,
|
filename: exportRes?.dockerbuildFilename,
|
||||||
mimeType: 'application/gzip',
|
|
||||||
retentionDays: 1
|
retentionDays: 1
|
||||||
});
|
});
|
||||||
expect(uploadRes).toBeDefined();
|
expect(uploadRes).toBeDefined();
|
||||||
@@ -165,7 +164,6 @@ maybe('writeBuildSummary', () => {
|
|||||||
|
|
||||||
const uploadRes = await GitHubArtifact.upload({
|
const uploadRes = await GitHubArtifact.upload({
|
||||||
filename: exportRes?.dockerbuildFilename,
|
filename: exportRes?.dockerbuildFilename,
|
||||||
mimeType: 'application/gzip',
|
|
||||||
retentionDays: 1
|
retentionDays: 1
|
||||||
});
|
});
|
||||||
expect(uploadRes).toBeDefined();
|
expect(uploadRes).toBeDefined();
|
||||||
@@ -220,7 +218,6 @@ maybe('writeBuildSummary', () => {
|
|||||||
|
|
||||||
const uploadRes = await GitHubArtifact.upload({
|
const uploadRes = await GitHubArtifact.upload({
|
||||||
filename: exportRes?.dockerbuildFilename,
|
filename: exportRes?.dockerbuildFilename,
|
||||||
mimeType: 'application/gzip',
|
|
||||||
retentionDays: 1
|
retentionDays: 1
|
||||||
});
|
});
|
||||||
expect(uploadRes).toBeDefined();
|
expect(uploadRes).toBeDefined();
|
||||||
|
|||||||
@@ -54,7 +54,6 @@
|
|||||||
"@actions/http-client": "^4.0.0",
|
"@actions/http-client": "^4.0.0",
|
||||||
"@actions/io": "^3.0.2",
|
"@actions/io": "^3.0.2",
|
||||||
"@actions/tool-cache": "^4.0.0",
|
"@actions/tool-cache": "^4.0.0",
|
||||||
"@azure/storage-blob": "^12.30.0",
|
|
||||||
"@sigstore/bundle": "^4.0.0",
|
"@sigstore/bundle": "^4.0.0",
|
||||||
"@sigstore/sign": "^4.1.0",
|
"@sigstore/sign": "^4.1.0",
|
||||||
"@sigstore/tuf": "^4.0.1",
|
"@sigstore/tuf": "^4.0.1",
|
||||||
|
|||||||
@@ -14,17 +14,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import crypto from 'crypto';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {CreateArtifactRequest, FinalizeArtifactRequest, StringValue} from '@actions/artifact/lib/generated';
|
import {DefaultArtifactClient, InvalidResponseError} from '@actions/artifact';
|
||||||
import {internalArtifactTwirpClient} from '@actions/artifact/lib/internal/shared/artifact-twirp-client';
|
|
||||||
import {getBackendIdsFromToken} from '@actions/artifact/lib/internal/shared/util';
|
|
||||||
import {getExpiration} from '@actions/artifact/lib/internal/upload/retention';
|
|
||||||
import {InvalidResponseError, NetworkError} from '@actions/artifact';
|
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import {TransferProgressEvent} from '@azure/core-rest-pipeline';
|
|
||||||
import {BlobClient, BlobHTTPHeaders} from '@azure/storage-blob';
|
|
||||||
|
|
||||||
import {UploadOpts, UploadResponse} from '../types/github/artifact.js';
|
import {UploadOpts, UploadResponse} from '../types/github/artifact.js';
|
||||||
import {GitHub} from './github';
|
import {GitHub} from './github';
|
||||||
@@ -36,90 +29,27 @@ export class GitHubArtifact {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const artifactName = path.basename(opts.filename);
|
const artifactName = path.basename(opts.filename);
|
||||||
const backendIds = getBackendIdsFromToken();
|
const artifactClient = new DefaultArtifactClient();
|
||||||
const artifactClient = internalArtifactTwirpClient();
|
|
||||||
|
|
||||||
core.info(`Uploading ${artifactName} to blob storage`);
|
core.info(`Uploading ${artifactName} as an artifact`);
|
||||||
|
const rootDirectory = path.dirname(opts.filename);
|
||||||
const createArtifactReq: CreateArtifactRequest = {
|
const response = await artifactClient.uploadArtifact(artifactName, [opts.filename], rootDirectory, {
|
||||||
workflowRunBackendId: backendIds.workflowRunBackendId,
|
retentionDays: opts.retentionDays,
|
||||||
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
|
skipArchive: true
|
||||||
name: artifactName,
|
});
|
||||||
version: 4
|
if (!response.id) {
|
||||||
};
|
throw new InvalidResponseError('Cannot upload artifact');
|
||||||
|
|
||||||
const expiresAt = getExpiration(opts?.retentionDays);
|
|
||||||
if (expiresAt) {
|
|
||||||
createArtifactReq.expiresAt = expiresAt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const createArtifactResp = await artifactClient.CreateArtifact(createArtifactReq);
|
const size = response.size ?? fs.statSync(opts.filename).size;
|
||||||
if (!createArtifactResp.ok) {
|
const artifactURL = `${GitHub.workflowRunURL()}/artifacts/${response.id}`;
|
||||||
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 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'
|
|
||||||
};
|
|
||||||
|
|
||||||
if (sha256Hash) {
|
|
||||||
finalizeArtifactReq.hash = StringValue.create({
|
|
||||||
value: `sha256:${sha256Hash}`
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
core.info(`Finalizing artifact upload`);
|
|
||||||
const finalizeArtifactResp = await artifactClient.FinalizeArtifact(finalizeArtifactReq);
|
|
||||||
if (!finalizeArtifactResp.ok) {
|
|
||||||
throw new InvalidResponseError('Cannot finalize artifact upload');
|
|
||||||
}
|
|
||||||
|
|
||||||
const artifactId = BigInt(finalizeArtifactResp.artifactId);
|
|
||||||
core.info(`Artifact successfully finalized (${artifactId})`);
|
|
||||||
|
|
||||||
const artifactURL = `${GitHub.workflowRunURL()}/artifacts/${artifactId}`;
|
|
||||||
core.info(`Artifact download URL: ${artifactURL}`);
|
core.info(`Artifact download URL: ${artifactURL}`);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: Number(artifactId),
|
id: response.id,
|
||||||
filename: artifactName,
|
filename: artifactName,
|
||||||
size: uploadByteCount,
|
digest: response.digest || '',
|
||||||
|
size,
|
||||||
url: artifactURL
|
url: artifactURL
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,13 @@
|
|||||||
|
|
||||||
export interface UploadOpts {
|
export interface UploadOpts {
|
||||||
filename: string;
|
filename: string;
|
||||||
mimeType?: string;
|
|
||||||
retentionDays?: number;
|
retentionDays?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UploadResponse {
|
export interface UploadResponse {
|
||||||
id: number;
|
id: number;
|
||||||
filename: string;
|
filename: string;
|
||||||
|
digest: string;
|
||||||
size: number;
|
size: number;
|
||||||
url: string;
|
url: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1141,7 +1141,6 @@ __metadata:
|
|||||||
"@actions/http-client": "npm:^4.0.0"
|
"@actions/http-client": "npm:^4.0.0"
|
||||||
"@actions/io": "npm:^3.0.2"
|
"@actions/io": "npm:^3.0.2"
|
||||||
"@actions/tool-cache": "npm:^4.0.0"
|
"@actions/tool-cache": "npm:^4.0.0"
|
||||||
"@azure/storage-blob": "npm:^12.30.0"
|
|
||||||
"@eslint/compat": "npm:^2.0.0"
|
"@eslint/compat": "npm:^2.0.0"
|
||||||
"@eslint/eslintrc": "npm:^3.3.3"
|
"@eslint/eslintrc": "npm:^3.3.3"
|
||||||
"@eslint/js": "npm:^9.39.2"
|
"@eslint/js": "npm:^9.39.2"
|
||||||
|
|||||||
Reference in New Issue
Block a user