2019-10-31 02:48:49 +08:00
|
|
|
import * as core from "@actions/core";
|
2020-02-05 22:24:37 +08:00
|
|
|
import { HttpClient, HttpCodes } from "@actions/http-client";
|
2020-03-18 21:35:13 +08:00
|
|
|
import { BearerCredentialHandler } from "@actions/http-client/auth";
|
2020-01-07 02:05:50 +08:00
|
|
|
import {
|
2020-02-05 22:24:37 +08:00
|
|
|
IHttpClientResponse,
|
2020-01-07 02:05:50 +08:00
|
|
|
IRequestOptions,
|
2020-02-05 22:24:37 +08:00
|
|
|
ITypedResponse
|
|
|
|
} from "@actions/http-client/interfaces";
|
2020-03-21 04:02:11 +08:00
|
|
|
import * as crypto from "crypto";
|
2020-03-18 21:35:13 +08:00
|
|
|
import * as fs from "fs";
|
|
|
|
|
2020-03-21 04:02:11 +08:00
|
|
|
import { Inputs } from "./constants";
|
2020-01-07 02:05:50 +08:00
|
|
|
import {
|
|
|
|
ArtifactCacheEntry,
|
|
|
|
CommitCacheRequest,
|
|
|
|
ReserveCacheRequest,
|
|
|
|
ReserveCacheResponse
|
|
|
|
} from "./contracts";
|
|
|
|
import * as utils from "./utils/actionUtils";
|
2019-10-31 02:48:49 +08:00
|
|
|
|
2020-03-21 04:02:11 +08:00
|
|
|
const versionSalt = "1.0";
|
|
|
|
|
2020-02-05 22:24:37 +08:00
|
|
|
function isSuccessStatusCode(statusCode?: number): boolean {
|
|
|
|
if (!statusCode) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-07 02:05:50 +08:00
|
|
|
return statusCode >= 200 && statusCode < 300;
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:24:37 +08:00
|
|
|
function isRetryableStatusCode(statusCode?: number): boolean {
|
|
|
|
if (!statusCode) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-07 02:05:50 +08:00
|
|
|
const retryableStatusCodes = [
|
|
|
|
HttpCodes.BadGateway,
|
|
|
|
HttpCodes.ServiceUnavailable,
|
|
|
|
HttpCodes.GatewayTimeout
|
|
|
|
];
|
|
|
|
return retryableStatusCodes.includes(statusCode);
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:24:37 +08:00
|
|
|
function getCacheApiUrl(resource: string): string {
|
2019-11-13 05:48:02 +08:00
|
|
|
// Ideally we just use ACTIONS_CACHE_URL
|
2020-01-07 02:05:50 +08:00
|
|
|
const baseUrl: string = (
|
2019-11-13 05:48:02 +08:00
|
|
|
process.env["ACTIONS_CACHE_URL"] ||
|
|
|
|
process.env["ACTIONS_RUNTIME_URL"] ||
|
|
|
|
""
|
|
|
|
).replace("pipelines", "artifactcache");
|
2020-01-07 02:05:50 +08:00
|
|
|
if (!baseUrl) {
|
2019-11-13 05:48:02 +08:00
|
|
|
throw new Error(
|
|
|
|
"Cache Service Url not found, unable to restore cache."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:24:37 +08:00
|
|
|
const url = `${baseUrl}_apis/artifactcache/${resource}`;
|
|
|
|
core.debug(`Resource Url: ${url}`);
|
|
|
|
return url;
|
2019-11-13 05:48:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createAcceptHeader(type: string, apiVersion: string): string {
|
|
|
|
return `${type};api-version=${apiVersion}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRequestOptions(): IRequestOptions {
|
|
|
|
const requestOptions: IRequestOptions = {
|
2020-02-05 22:24:37 +08:00
|
|
|
headers: {
|
|
|
|
Accept: createAcceptHeader("application/json", "6.0-preview.1")
|
|
|
|
}
|
2019-11-13 05:48:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return requestOptions;
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:24:37 +08:00
|
|
|
function createHttpClient(): HttpClient {
|
2019-10-31 02:48:49 +08:00
|
|
|
const token = process.env["ACTIONS_RUNTIME_TOKEN"] || "";
|
|
|
|
const bearerCredentialHandler = new BearerCredentialHandler(token);
|
|
|
|
|
2020-02-05 22:24:37 +08:00
|
|
|
return new HttpClient(
|
|
|
|
"actions/cache",
|
|
|
|
[bearerCredentialHandler],
|
|
|
|
getRequestOptions()
|
|
|
|
);
|
2020-01-07 02:05:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-21 04:02:11 +08:00
|
|
|
export function getCacheVersion(): string {
|
|
|
|
// Add salt to cache version to support breaking changes in cache entry
|
|
|
|
const components = [
|
|
|
|
core.getInput(Inputs.Path, { required: true }),
|
|
|
|
versionSalt
|
|
|
|
];
|
|
|
|
|
|
|
|
return crypto
|
|
|
|
.createHash("sha256")
|
|
|
|
.update(components.join("|"))
|
|
|
|
.digest("hex");
|
|
|
|
}
|
|
|
|
|
2020-01-07 02:05:50 +08:00
|
|
|
export async function getCacheEntry(
|
|
|
|
keys: string[]
|
|
|
|
): Promise<ArtifactCacheEntry | null> {
|
2020-02-05 22:24:37 +08:00
|
|
|
const httpClient = createHttpClient();
|
2020-03-21 04:02:11 +08:00
|
|
|
const version = getCacheVersion();
|
|
|
|
const resource = `cache?keys=${encodeURIComponent(
|
|
|
|
keys.join(",")
|
|
|
|
)}&version=${version}`;
|
2019-10-31 02:48:49 +08:00
|
|
|
|
2020-02-05 22:24:37 +08:00
|
|
|
const response = await httpClient.getJson<ArtifactCacheEntry>(
|
|
|
|
getCacheApiUrl(resource)
|
2019-10-31 02:48:49 +08:00
|
|
|
);
|
|
|
|
if (response.statusCode === 204) {
|
2019-11-05 00:03:18 +08:00
|
|
|
return null;
|
2019-10-31 02:48:49 +08:00
|
|
|
}
|
2020-01-07 02:05:50 +08:00
|
|
|
if (!isSuccessStatusCode(response.statusCode)) {
|
2019-10-31 02:48:49 +08:00
|
|
|
throw new Error(`Cache service responded with ${response.statusCode}`);
|
|
|
|
}
|
2020-02-05 22:24:37 +08:00
|
|
|
|
2019-10-31 02:48:49 +08:00
|
|
|
const cacheResult = response.result;
|
2020-01-07 02:05:50 +08:00
|
|
|
const cacheDownloadUrl = cacheResult?.archiveLocation;
|
|
|
|
if (!cacheDownloadUrl) {
|
2019-10-31 02:48:49 +08:00
|
|
|
throw new Error("Cache not found.");
|
|
|
|
}
|
2020-01-07 02:05:50 +08:00
|
|
|
core.setSecret(cacheDownloadUrl);
|
2019-11-22 03:37:32 +08:00
|
|
|
core.debug(`Cache Result:`);
|
|
|
|
core.debug(JSON.stringify(cacheResult));
|
2019-10-31 02:48:49 +08:00
|
|
|
|
|
|
|
return cacheResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function pipeResponseToStream(
|
|
|
|
response: IHttpClientResponse,
|
|
|
|
stream: NodeJS.WritableStream
|
|
|
|
): Promise<void> {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
response.message.pipe(stream).on("close", () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-13 05:48:02 +08:00
|
|
|
export async function downloadCache(
|
2020-01-07 02:05:50 +08:00
|
|
|
archiveLocation: string,
|
2019-11-13 05:48:02 +08:00
|
|
|
archivePath: string
|
|
|
|
): Promise<void> {
|
|
|
|
const stream = fs.createWriteStream(archivePath);
|
|
|
|
const httpClient = new HttpClient("actions/cache");
|
2020-01-07 02:05:50 +08:00
|
|
|
const downloadResponse = await httpClient.get(archiveLocation);
|
2019-11-13 05:48:02 +08:00
|
|
|
await pipeResponseToStream(downloadResponse, stream);
|
|
|
|
}
|
|
|
|
|
2020-01-07 02:05:50 +08:00
|
|
|
// Reserve Cache
|
|
|
|
export async function reserveCache(key: string): Promise<number> {
|
2020-02-05 22:24:37 +08:00
|
|
|
const httpClient = createHttpClient();
|
2020-03-21 04:02:11 +08:00
|
|
|
const version = getCacheVersion();
|
2019-11-15 06:14:16 +08:00
|
|
|
|
2020-01-07 02:05:50 +08:00
|
|
|
const reserveCacheRequest: ReserveCacheRequest = {
|
2020-03-21 04:02:11 +08:00
|
|
|
key,
|
|
|
|
version
|
2020-01-07 02:05:50 +08:00
|
|
|
};
|
2020-02-05 22:24:37 +08:00
|
|
|
const response = await httpClient.postJson<ReserveCacheResponse>(
|
|
|
|
getCacheApiUrl("caches"),
|
|
|
|
reserveCacheRequest
|
2020-01-07 02:05:50 +08:00
|
|
|
);
|
|
|
|
return response?.result?.cacheId ?? -1;
|
|
|
|
}
|
2019-10-31 02:48:49 +08:00
|
|
|
|
2020-01-07 02:05:50 +08:00
|
|
|
function getContentRange(start: number, end: number): string {
|
|
|
|
// Format: `bytes start-end/filesize
|
|
|
|
// start and end are inclusive
|
|
|
|
// filesize can be *
|
|
|
|
// For a 200 byte chunk starting at byte 0:
|
|
|
|
// Content-Range: bytes 0-199/*
|
|
|
|
return `bytes ${start}-${end}/*`;
|
|
|
|
}
|
2019-10-31 02:48:49 +08:00
|
|
|
|
2020-01-07 02:05:50 +08:00
|
|
|
async function uploadChunk(
|
2020-02-05 22:24:37 +08:00
|
|
|
httpClient: HttpClient,
|
2020-01-07 02:05:50 +08:00
|
|
|
resourceUrl: string,
|
|
|
|
data: NodeJS.ReadableStream,
|
|
|
|
start: number,
|
|
|
|
end: number
|
|
|
|
): Promise<void> {
|
|
|
|
core.debug(
|
|
|
|
`Uploading chunk of size ${end -
|
|
|
|
start +
|
|
|
|
1} bytes at offset ${start} with content range: ${getContentRange(
|
|
|
|
start,
|
|
|
|
end
|
|
|
|
)}`
|
|
|
|
);
|
2020-02-05 22:24:37 +08:00
|
|
|
const additionalHeaders = {
|
2020-01-07 02:05:50 +08:00
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
"Content-Range": getContentRange(start, end)
|
|
|
|
};
|
|
|
|
|
2020-02-05 22:24:37 +08:00
|
|
|
const uploadChunkRequest = async (): Promise<IHttpClientResponse> => {
|
|
|
|
return await httpClient.sendStream(
|
2020-01-07 02:05:50 +08:00
|
|
|
"PATCH",
|
|
|
|
resourceUrl,
|
|
|
|
data,
|
2020-02-05 22:24:37 +08:00
|
|
|
additionalHeaders
|
2020-01-07 02:05:50 +08:00
|
|
|
);
|
2019-10-31 02:48:49 +08:00
|
|
|
};
|
|
|
|
|
2020-01-07 02:05:50 +08:00
|
|
|
const response = await uploadChunkRequest();
|
2020-02-05 22:24:37 +08:00
|
|
|
if (isSuccessStatusCode(response.message.statusCode)) {
|
2020-01-07 02:05:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:24:37 +08:00
|
|
|
if (isRetryableStatusCode(response.message.statusCode)) {
|
2020-01-07 02:05:50 +08:00
|
|
|
core.debug(
|
2020-02-05 22:24:37 +08:00
|
|
|
`Received ${response.message.statusCode}, retrying chunk at offset ${start}.`
|
2020-01-07 02:05:50 +08:00
|
|
|
);
|
|
|
|
const retryResponse = await uploadChunkRequest();
|
2020-02-05 22:24:37 +08:00
|
|
|
if (isSuccessStatusCode(retryResponse.message.statusCode)) {
|
2020-01-07 02:05:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(
|
2020-02-05 22:24:37 +08:00
|
|
|
`Cache service responded with ${response.message.statusCode} during chunk upload.`
|
2020-01-07 02:05:50 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-07 03:06:24 +08:00
|
|
|
function parseEnvNumber(key: string): number | undefined {
|
|
|
|
const value = Number(process.env[key]);
|
|
|
|
if (Number.isNaN(value) || value < 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-01-07 02:05:50 +08:00
|
|
|
async function uploadFile(
|
2020-02-05 22:24:37 +08:00
|
|
|
httpClient: HttpClient,
|
2020-01-07 02:05:50 +08:00
|
|
|
cacheId: number,
|
|
|
|
archivePath: string
|
|
|
|
): Promise<void> {
|
|
|
|
// Upload Chunks
|
|
|
|
const fileSize = fs.statSync(archivePath).size;
|
2020-02-05 22:24:37 +08:00
|
|
|
const resourceUrl = getCacheApiUrl(`caches/${cacheId.toString()}`);
|
2020-01-07 02:05:50 +08:00
|
|
|
const fd = fs.openSync(archivePath, "r");
|
|
|
|
|
2020-01-07 03:06:24 +08:00
|
|
|
const concurrency = parseEnvNumber("CACHE_UPLOAD_CONCURRENCY") ?? 4; // # of HTTP requests in parallel
|
2020-01-07 02:05:50 +08:00
|
|
|
const MAX_CHUNK_SIZE =
|
2020-01-07 03:06:24 +08:00
|
|
|
parseEnvNumber("CACHE_UPLOAD_CHUNK_SIZE") ?? 32 * 1024 * 1024; // 32 MB Chunks
|
2020-01-07 02:05:50 +08:00
|
|
|
core.debug(`Concurrency: ${concurrency} and Chunk Size: ${MAX_CHUNK_SIZE}`);
|
|
|
|
|
|
|
|
const parallelUploads = [...new Array(concurrency).keys()];
|
|
|
|
core.debug("Awaiting all uploads");
|
|
|
|
let offset = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
|
|
|
parallelUploads.map(async () => {
|
|
|
|
while (offset < fileSize) {
|
|
|
|
const chunkSize = Math.min(
|
|
|
|
fileSize - offset,
|
|
|
|
MAX_CHUNK_SIZE
|
|
|
|
);
|
|
|
|
const start = offset;
|
|
|
|
const end = offset + chunkSize - 1;
|
|
|
|
offset += MAX_CHUNK_SIZE;
|
|
|
|
const chunk = fs.createReadStream(archivePath, {
|
|
|
|
fd,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
autoClose: false
|
|
|
|
});
|
|
|
|
|
|
|
|
await uploadChunk(
|
2020-02-05 22:24:37 +08:00
|
|
|
httpClient,
|
2020-01-07 02:05:50 +08:00
|
|
|
resourceUrl,
|
|
|
|
chunk,
|
|
|
|
start,
|
|
|
|
end
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
fs.closeSync(fd);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function commitCache(
|
2020-02-05 22:24:37 +08:00
|
|
|
httpClient: HttpClient,
|
2020-01-07 02:05:50 +08:00
|
|
|
cacheId: number,
|
|
|
|
filesize: number
|
2020-02-05 22:24:37 +08:00
|
|
|
): Promise<ITypedResponse<null>> {
|
2020-01-07 02:05:50 +08:00
|
|
|
const commitCacheRequest: CommitCacheRequest = { size: filesize };
|
2020-02-05 22:24:37 +08:00
|
|
|
return await httpClient.postJson<null>(
|
|
|
|
getCacheApiUrl(`caches/${cacheId.toString()}`),
|
|
|
|
commitCacheRequest
|
2019-10-31 02:48:49 +08:00
|
|
|
);
|
2020-01-07 02:05:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function saveCache(
|
|
|
|
cacheId: number,
|
|
|
|
archivePath: string
|
|
|
|
): Promise<void> {
|
2020-02-05 22:24:37 +08:00
|
|
|
const httpClient = createHttpClient();
|
2020-01-07 02:05:50 +08:00
|
|
|
|
|
|
|
core.debug("Upload cache");
|
2020-02-05 22:24:37 +08:00
|
|
|
await uploadFile(httpClient, cacheId, archivePath);
|
2020-01-07 02:05:50 +08:00
|
|
|
|
|
|
|
// Commit Cache
|
|
|
|
core.debug("Commiting cache");
|
|
|
|
const cacheSize = utils.getArchiveFileSize(archivePath);
|
|
|
|
const commitCacheResponse = await commitCache(
|
2020-02-05 22:24:37 +08:00
|
|
|
httpClient,
|
2020-01-07 02:05:50 +08:00
|
|
|
cacheId,
|
|
|
|
cacheSize
|
|
|
|
);
|
|
|
|
if (!isSuccessStatusCode(commitCacheResponse.statusCode)) {
|
|
|
|
throw new Error(
|
|
|
|
`Cache service responded with ${commitCacheResponse.statusCode} during commit cache.`
|
|
|
|
);
|
2019-10-31 02:48:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
core.info("Cache saved successfully");
|
|
|
|
}
|