Skip to content
Merged
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 .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_VERSION: ${{ matrix.node_version }}
DOCKER_BUILD_SUMMARY: false
-
name: Check coverage
run: |
Expand Down
1 change: 0 additions & 1 deletion __tests__/github/artifact.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions __tests__/github/summary.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ maybe('writeBuildSummary', () => {

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

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

const uploadRes = await GitHubArtifact.upload({
filename: exportRes?.dockerbuildFilename,
mimeType: 'application/gzip',
retentionDays: 1
});
expect(uploadRes).toBeDefined();
Expand Down
5 changes: 4 additions & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ module.exports = {
}
]
},
transformIgnorePatterns: ['/node_modules/(?!(?:@actions/cache|@actions/core|@actions/exec|@actions/github|@actions/glob|@actions/http-client|@actions/io|@actions/tool-cache|@octokit|universal-user-agent|before-after-hook)/)'],
// prettier-ignore
transformIgnorePatterns: ['/node_modules/(?!(?:@actions/artifact|@actions/cache|@actions/core|@actions/exec|@actions/github|@actions/glob|@actions/http-client|@actions/io|@actions/tool-cache|@octokit|universal-user-agent|before-after-hook)/)'],
moduleNameMapper: {
'^@actions/artifact$': '<rootDir>/node_modules/@actions/artifact/lib/artifact.js',
'^@actions/cache$': '<rootDir>/node_modules/@actions/cache/lib/cache.js',
'^@actions/core': '<rootDir>/node_modules/@actions/core/lib/core.js',
'^@actions/exec$': '<rootDir>/node_modules/@actions/exec/lib/exec.js',
'^@actions/github$': '<rootDir>/node_modules/@actions/github/lib/github.js',
'^@actions/github/lib/utils$': '<rootDir>/node_modules/@actions/github/lib/utils.js',
'^@actions/glob$': '<rootDir>/node_modules/@actions/glob/lib/glob.js',
'^@actions/http-client$': '<rootDir>/node_modules/@actions/http-client/lib/index.js',
'^@actions/http-client/lib/auth$': '<rootDir>/node_modules/@actions/http-client/lib/auth.js',
Expand Down
5 changes: 4 additions & 1 deletion jest.config.itg.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ module.exports = {
}
]
},
transformIgnorePatterns: ['/node_modules/(?!(?:@actions/cache|@actions/core|@actions/exec|@actions/github|@actions/glob|@actions/http-client|@actions/io|@actions/tool-cache|@octokit|universal-user-agent|before-after-hook)/)'],
// prettier-ignore
transformIgnorePatterns: ['/node_modules/(?!(?:@actions/artifact|@actions/cache|@actions/core|@actions/exec|@actions/github|@actions/glob|@actions/http-client|@actions/io|@actions/tool-cache|@octokit|universal-user-agent|before-after-hook)/)'],
moduleNameMapper: {
'^@actions/artifact$': '<rootDir>/node_modules/@actions/artifact/lib/artifact.js',
'^@actions/cache$': '<rootDir>/node_modules/@actions/cache/lib/cache.js',
'^@actions/core': '<rootDir>/node_modules/@actions/core/lib/core.js',
'^@actions/exec$': '<rootDir>/node_modules/@actions/exec/lib/exec.js',
'^@actions/github$': '<rootDir>/node_modules/@actions/github/lib/github.js',
'^@actions/github/lib/utils$': '<rootDir>/node_modules/@actions/github/lib/utils.js',
'^@actions/glob$': '<rootDir>/node_modules/@actions/glob/lib/glob.js',
'^@actions/http-client$': '<rootDir>/node_modules/@actions/http-client/lib/index.js',
'^@actions/http-client/lib/auth$': '<rootDir>/node_modules/@actions/http-client/lib/auth.js',
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"@actions/artifact": "^5.0.3",
"@actions/artifact": "^6.2.0",
"@actions/cache": "^6.0.0",
"@actions/core": "^3.0.0",
"@actions/exec": "^3.0.0",
"@actions/github": "^9.0.0",
"@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",
Expand Down
102 changes: 16 additions & 86 deletions src/github/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -36,90 +29,27 @@ export class GitHubArtifact {
}

const artifactName = path.basename(opts.filename);
const backendIds = getBackendIdsFromToken();
const artifactClient = internalArtifactTwirpClient();

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

const createArtifactResp = await artifactClient.CreateArtifact(createArtifactReq);
if (!createArtifactResp.ok) {
throw new InvalidResponseError('cannot create artifact client');
const artifactClient = new DefaultArtifactClient();

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

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
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/github/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading
Loading