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:
CrazyMax
2026-02-26 09:23:01 +01:00
parent 03628ab393
commit 5e783cc801
7 changed files with 16 additions and 96 deletions

View File

@@ -16,10 +16,6 @@ updates:
versioning-strategy: "increase"
allow:
- 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:
- "dependencies"
- "bot"

View File

@@ -33,7 +33,6 @@ maybe('upload', () => {
fs.copyFileSync(path.join(fixturesDir, `github-repo.json`), filename);
const res = await GitHubArtifact.upload({
filename: filename,
mimeType: 'application/json',
retentionDays: 1
});
expect(res).toBeDefined();

View File

@@ -85,7 +85,6 @@ maybe('writeBuildSummary', () => {
const uploadRes = await GitHubArtifact.upload({
filename: exportRes?.dockerbuildFilename,
mimeType: 'application/gzip',
retentionDays: 1
});
expect(uploadRes).toBeDefined();
@@ -165,7 +164,6 @@ maybe('writeBuildSummary', () => {
const uploadRes = await GitHubArtifact.upload({
filename: exportRes?.dockerbuildFilename,
mimeType: 'application/gzip',
retentionDays: 1
});
expect(uploadRes).toBeDefined();
@@ -220,7 +218,6 @@ maybe('writeBuildSummary', () => {
const uploadRes = await GitHubArtifact.upload({
filename: exportRes?.dockerbuildFilename,
mimeType: 'application/gzip',
retentionDays: 1
});
expect(uploadRes).toBeDefined();

View File

@@ -54,7 +54,6 @@
"@actions/http-client": "^4.0.0",
"@actions/io": "^3.0.2",
"@actions/tool-cache": "^4.0.0",
"@azure/storage-blob": "^12.30.0",
"@sigstore/bundle": "^4.0.0",
"@sigstore/sign": "^4.1.0",
"@sigstore/tuf": "^4.0.1",

View File

@@ -14,17 +14,10 @@
* limitations under the License.
*/
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import {CreateArtifactRequest, FinalizeArtifactRequest, StringValue} from '@actions/artifact/lib/generated';
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 {DefaultArtifactClient, InvalidResponseError} from '@actions/artifact';
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 {GitHub} from './github';
@@ -36,90 +29,27 @@ export class GitHubArtifact {
}
const artifactName = path.basename(opts.filename);
const backendIds = getBackendIdsFromToken();
const artifactClient = internalArtifactTwirpClient();
const artifactClient = new DefaultArtifactClient();
core.info(`Uploading ${artifactName} to blob storage`);
const createArtifactReq: CreateArtifactRequest = {
workflowRunBackendId: backendIds.workflowRunBackendId,
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
name: artifactName,
version: 4
};
const expiresAt = getExpiration(opts?.retentionDays);
if (expiresAt) {
createArtifactReq.expiresAt = expiresAt;
core.info(`Uploading ${artifactName} as an artifact`);
const rootDirectory = path.dirname(opts.filename);
const response = await artifactClient.uploadArtifact(artifactName, [opts.filename], rootDirectory, {
retentionDays: opts.retentionDays,
skipArchive: true
});
if (!response.id) {
throw new InvalidResponseError('Cannot upload artifact');
}
const createArtifactResp = await artifactClient.CreateArtifact(createArtifactReq);
if (!createArtifactResp.ok) {
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}`;
const size = response.size ?? fs.statSync(opts.filename).size;
const artifactURL = `${GitHub.workflowRunURL()}/artifacts/${response.id}`;
core.info(`Artifact download URL: ${artifactURL}`);
return {
id: Number(artifactId),
id: response.id,
filename: artifactName,
size: uploadByteCount,
digest: response.digest || '',
size,
url: artifactURL
};
}

View File

@@ -16,13 +16,13 @@
export interface UploadOpts {
filename: string;
mimeType?: string;
retentionDays?: number;
}
export interface UploadResponse {
id: number;
filename: string;
digest: string;
size: number;
url: string;
}

View File

@@ -1141,7 +1141,6 @@ __metadata:
"@actions/http-client": "npm:^4.0.0"
"@actions/io": "npm:^3.0.2"
"@actions/tool-cache": "npm:^4.0.0"
"@azure/storage-blob": "npm:^12.30.0"
"@eslint/compat": "npm:^2.0.0"
"@eslint/eslintrc": "npm:^3.3.3"
"@eslint/js": "npm:^9.39.2"