|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import {GitHub as Octokit} from '@actions/github/lib/utils'; |
| 17 | +import crypto from 'crypto'; |
| 18 | +import fs from 'fs'; |
| 19 | +import path from 'path'; |
| 20 | +import {CreateArtifactRequest, FinalizeArtifactRequest, StringValue} from '@actions/artifact/lib/generated'; |
| 21 | +import {internalArtifactTwirpClient} from '@actions/artifact/lib/internal/shared/artifact-twirp-client'; |
| 22 | +import {getBackendIdsFromToken} from '@actions/artifact/lib/internal/shared/util'; |
| 23 | +import {getExpiration} from '@actions/artifact/lib/internal/upload/retention'; |
| 24 | +import {InvalidResponseError, NetworkError} from '@actions/artifact'; |
18 | 25 | import * as core from '@actions/core';
|
19 | 26 | import * as github from '@actions/github';
|
| 27 | +import {GitHub as Octokit} from '@actions/github/lib/utils'; |
20 | 28 | import {Context} from '@actions/github/lib/context';
|
| 29 | +import {TransferProgressEvent} from '@azure/core-http'; |
| 30 | +import {BlobClient, BlobHTTPHeaders} from '@azure/storage-blob'; |
21 | 31 | import {jwtDecode, JwtPayload} from 'jwt-decode';
|
22 | 32 |
|
23 |
| -import {GitHubActionsRuntimeToken, GitHubActionsRuntimeTokenAC, GitHubRepo} from './types/github'; |
| 33 | +import {GitHubActionsRuntimeToken, GitHubActionsRuntimeTokenAC, GitHubRepo, UploadArtifactOpts, UploadArtifactResponse} from './types/github'; |
24 | 34 |
|
25 | 35 | export interface GitHubOpts {
|
26 | 36 | token?: string;
|
@@ -90,4 +100,94 @@ export class GitHub {
|
90 | 100 | throw new Error(`Cannot parse GitHub Actions Runtime Token ACs: ${e.message}`);
|
91 | 101 | }
|
92 | 102 | }
|
| 103 | + |
| 104 | + public static async uploadArtifact(opts: UploadArtifactOpts): Promise<UploadArtifactResponse> { |
| 105 | + const artifactName = path.basename(opts.filename); |
| 106 | + const backendIds = getBackendIdsFromToken(); |
| 107 | + const artifactClient = internalArtifactTwirpClient(); |
| 108 | + |
| 109 | + core.info(`Uploading ${artifactName} to blob storage`); |
| 110 | + |
| 111 | + const createArtifactReq: CreateArtifactRequest = { |
| 112 | + workflowRunBackendId: backendIds.workflowRunBackendId, |
| 113 | + workflowJobRunBackendId: backendIds.workflowJobRunBackendId, |
| 114 | + name: artifactName, |
| 115 | + version: 4 |
| 116 | + }; |
| 117 | + |
| 118 | + const expiresAt = getExpiration(opts?.retentionDays); |
| 119 | + if (expiresAt) { |
| 120 | + createArtifactReq.expiresAt = expiresAt; |
| 121 | + } |
| 122 | + |
| 123 | + const createArtifactResp = await artifactClient.CreateArtifact(createArtifactReq); |
| 124 | + if (!createArtifactResp.ok) { |
| 125 | + throw new InvalidResponseError('cannot create artifact client'); |
| 126 | + } |
| 127 | + |
| 128 | + let uploadByteCount = 0; |
| 129 | + const blobClient = new BlobClient(createArtifactResp.signedUploadUrl); |
| 130 | + const blockBlobClient = blobClient.getBlockBlobClient(); |
| 131 | + |
| 132 | + const headers: BlobHTTPHeaders = { |
| 133 | + blobContentDisposition: `attachment; filename="${artifactName}"` |
| 134 | + }; |
| 135 | + if (opts.mimeType) { |
| 136 | + headers.blobContentType = opts.mimeType; |
| 137 | + } |
| 138 | + core.debug(`Upload headers: ${JSON.stringify(headers)}`); |
| 139 | + |
| 140 | + try { |
| 141 | + core.info('Beginning upload of artifact content to blob storage'); |
| 142 | + await blockBlobClient.uploadFile(opts.filename, { |
| 143 | + blobHTTPHeaders: headers, |
| 144 | + onProgress: (progress: TransferProgressEvent): void => { |
| 145 | + core.info(`Uploaded bytes ${progress.loadedBytes}`); |
| 146 | + uploadByteCount = progress.loadedBytes; |
| 147 | + } |
| 148 | + }); |
| 149 | + } catch (error) { |
| 150 | + if (NetworkError.isNetworkErrorCode(error?.code)) { |
| 151 | + throw new NetworkError(error?.code); |
| 152 | + } |
| 153 | + throw error; |
| 154 | + } |
| 155 | + |
| 156 | + core.info('Finished uploading artifact content to blob storage!'); |
| 157 | + |
| 158 | + const sha256Hash = crypto.createHash('sha256').update(fs.readFileSync(opts.filename)).digest('hex'); |
| 159 | + core.info(`SHA256 hash of uploaded artifact is ${sha256Hash}`); |
| 160 | + |
| 161 | + const finalizeArtifactReq: FinalizeArtifactRequest = { |
| 162 | + workflowRunBackendId: backendIds.workflowRunBackendId, |
| 163 | + workflowJobRunBackendId: backendIds.workflowJobRunBackendId, |
| 164 | + name: artifactName, |
| 165 | + size: uploadByteCount ? uploadByteCount.toString() : '0' |
| 166 | + }; |
| 167 | + |
| 168 | + if (sha256Hash) { |
| 169 | + finalizeArtifactReq.hash = StringValue.create({ |
| 170 | + value: `sha256:${sha256Hash}` |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + core.info(`Finalizing artifact upload`); |
| 175 | + const finalizeArtifactResp = await artifactClient.FinalizeArtifact(finalizeArtifactReq); |
| 176 | + if (!finalizeArtifactResp.ok) { |
| 177 | + throw new InvalidResponseError('Cannot finalize artifact upload'); |
| 178 | + } |
| 179 | + |
| 180 | + const artifactId = BigInt(finalizeArtifactResp.artifactId); |
| 181 | + core.info(`Artifact successfully finalized (${artifactId})`); |
| 182 | + |
| 183 | + const artifactURL = `${GitHub.workflowRunURL}/artifacts/${artifactId}`; |
| 184 | + core.info(`Artifact download URL: ${artifactURL}`); |
| 185 | + |
| 186 | + return { |
| 187 | + id: Number(artifactId), |
| 188 | + filename: artifactName, |
| 189 | + size: uploadByteCount, |
| 190 | + url: artifactURL |
| 191 | + }; |
| 192 | + } |
93 | 193 | }
|
0 commit comments