mirror of
https://code.forgejo.org/actions/cache.git
synced 2025-04-23 01:03:57 +08:00
save state
This commit is contained in:
parent
ab5323b726
commit
0ad6d2d6f2
13 changed files with 595 additions and 1301 deletions
95
src/cache.service.ts
Normal file
95
src/cache.service.ts
Normal file
|
@ -0,0 +1,95 @@
|
|||
import * as utils from "@actions/cache/lib/internal/cacheUtils";
|
||||
import { createTar, listTar } from "@actions/cache/lib/internal/tar";
|
||||
import * as core from "@actions/core";
|
||||
import { S3 } from "aws-sdk";
|
||||
import filesize from "filesize";
|
||||
import fs from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
export class CacheService {
|
||||
private _client: S3;
|
||||
private _bucket: string;
|
||||
|
||||
constructor(
|
||||
accessKeyId: string,
|
||||
secretAccessKey: string,
|
||||
region: string,
|
||||
bucket: string
|
||||
) {
|
||||
this._client = new S3({
|
||||
region: region,
|
||||
credentials: {
|
||||
accessKeyId: accessKeyId,
|
||||
secretAccessKey: secretAccessKey
|
||||
}
|
||||
});
|
||||
this._bucket = bucket;
|
||||
}
|
||||
|
||||
async restoreCache(
|
||||
paths: string[],
|
||||
primaryKey: string,
|
||||
restoreKeys: string[]
|
||||
): Promise<string | undefined> {
|
||||
return "";
|
||||
}
|
||||
|
||||
async saveCache(paths: string[], key: string): Promise<string> {
|
||||
const compressionMethod = await utils.getCompressionMethod();
|
||||
|
||||
const cachePaths = await utils.resolvePaths(paths);
|
||||
core.debug("Cache Paths:");
|
||||
core.debug(`${JSON.stringify(cachePaths)}`);
|
||||
|
||||
const archiveFolder = await utils.createTempDirectory();
|
||||
const archivePath = path.join(
|
||||
archiveFolder,
|
||||
utils.getCacheFileName(compressionMethod)
|
||||
);
|
||||
|
||||
core.debug(`Archive Path: ${archivePath}`);
|
||||
|
||||
try {
|
||||
await createTar(archiveFolder, cachePaths, compressionMethod);
|
||||
if (core.isDebug()) {
|
||||
await listTar(archivePath, compressionMethod);
|
||||
}
|
||||
|
||||
core.info(
|
||||
`Archive Size: ${filesize(fs.statSync(archivePath).size)}`
|
||||
);
|
||||
|
||||
core.debug(`Saving Cache (ID: ${key})`);
|
||||
await this.uploadToS3(key, archivePath);
|
||||
} finally {
|
||||
// Try to delete the archive to save space
|
||||
try {
|
||||
await utils.unlinkFile(archivePath);
|
||||
} catch (error) {
|
||||
core.debug(`Failed to delete archive: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
private async uploadToS3(key: string, archivePath: string): Promise<void> {
|
||||
const client = new S3();
|
||||
// Read in the file, convert it to base64, store to S3
|
||||
fs.readFile(archivePath, (err, data) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
const base64data = data.toString("base64");
|
||||
|
||||
client
|
||||
.putObject({
|
||||
Bucket: this._bucket,
|
||||
Key: key,
|
||||
Body: base64data
|
||||
})
|
||||
.send();
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue