Skip to content

Commit cae64ca

Browse files
authored
Attempt to delete the archive after extraction (actions#209)
This reduces storage space used once the Action has finished executing.
1 parent af8651e commit cae64ca

6 files changed

Lines changed: 70 additions & 18 deletions

File tree

‎__tests__/actionUtils.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as core from "@actions/core";
2+
import * as fs from "fs";
23
import * as os from "os";
34
import * as path from "path";
45

@@ -234,3 +235,15 @@ test("isValidEvent returns true for pull request event", () => {
234235

235236
expect(isValidEvent).toBe(true);
236237
});
238+
239+
test("unlinkFile unlinks file", async () => {
240+
const testDirectory = fs.mkdtempSync("unlinkFileTest");
241+
const testFile = path.join(testDirectory, "test.txt");
242+
fs.writeFileSync(testFile, "hello world");
243+
244+
await actionUtils.unlinkFile(testFile);
245+
246+
expect(fs.existsSync(testFile)).toBe(false);
247+
248+
fs.rmdirSync(testDirectory);
249+
});

‎__tests__/restore.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ test("restore with cache found", async () => {
241241
.mockReturnValue(fileSize);
242242

243243
const extractTarMock = jest.spyOn(tar, "extractTar");
244+
const unlinkFileMock = jest.spyOn(actionUtils, "unlinkFile");
244245
const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
245246

246247
await run();
@@ -258,6 +259,9 @@ test("restore with cache found", async () => {
258259
expect(extractTarMock).toHaveBeenCalledTimes(1);
259260
expect(extractTarMock).toHaveBeenCalledWith(archivePath, cachePath);
260261

262+
expect(unlinkFileMock).toHaveBeenCalledTimes(1);
263+
expect(unlinkFileMock).toHaveBeenCalledWith(archivePath);
264+
261265
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
262266
expect(setCacheHitOutputMock).toHaveBeenCalledWith(true);
263267

‎dist/restore/index.js‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,7 @@ const io = __importStar(__webpack_require__(1));
16511651
const fs = __importStar(__webpack_require__(747));
16521652
const os = __importStar(__webpack_require__(87));
16531653
const path = __importStar(__webpack_require__(622));
1654+
const util = __importStar(__webpack_require__(669));
16541655
const uuidV4 = __importStar(__webpack_require__(826));
16551656
const constants_1 = __webpack_require__(694);
16561657
// From https://github.com/actions/toolkit/blob/master/packages/tool-cache/src/tool-cache.ts#L23
@@ -1743,6 +1744,10 @@ function isValidEvent() {
17431744
return getSupportedEvents().includes(githubEvent);
17441745
}
17451746
exports.isValidEvent = isValidEvent;
1747+
function unlinkFile(path) {
1748+
return util.promisify(fs.unlink)(path);
1749+
}
1750+
exports.unlinkFile = unlinkFile;
17461751

17471752

17481753
/***/ }),
@@ -2831,18 +2836,29 @@ function run() {
28312836
try {
28322837
const cacheEntry = yield cacheHttpClient.getCacheEntry(keys);
28332838
if (!((_a = cacheEntry) === null || _a === void 0 ? void 0 : _a.archiveLocation)) {
2834-
core.info(`Cache not found for input keys: ${keys.join(", ")}.`);
2839+
core.info(`Cache not found for input keys: ${keys.join(", ")}`);
28352840
return;
28362841
}
28372842
const archivePath = path.join(yield utils.createTempDirectory(), "cache.tgz");
28382843
core.debug(`Archive Path: ${archivePath}`);
28392844
// Store the cache result
28402845
utils.setCacheState(cacheEntry);
2841-
// Download the cache from the cache entry
2842-
yield cacheHttpClient.downloadCache(cacheEntry.archiveLocation, archivePath);
2843-
const archiveFileSize = utils.getArchiveFileSize(archivePath);
2844-
core.info(`Cache Size: ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B)`);
2845-
yield tar_1.extractTar(archivePath, cachePath);
2846+
try {
2847+
// Download the cache from the cache entry
2848+
yield cacheHttpClient.downloadCache(cacheEntry.archiveLocation, archivePath);
2849+
const archiveFileSize = utils.getArchiveFileSize(archivePath);
2850+
core.info(`Cache Size: ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B)`);
2851+
yield tar_1.extractTar(archivePath, cachePath);
2852+
}
2853+
finally {
2854+
// Try to delete the archive to save space
2855+
try {
2856+
yield utils.unlinkFile(archivePath);
2857+
}
2858+
catch (error) {
2859+
core.debug(`Failed to delete archive: ${error}`);
2860+
}
2861+
}
28462862
const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheEntry);
28472863
utils.setCacheHitOutput(isExactKeyMatch);
28482864
core.info(`Cache restored from key: ${cacheEntry && cacheEntry.cacheKey}`);

‎dist/save/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,7 @@ const io = __importStar(__webpack_require__(1));
16511651
const fs = __importStar(__webpack_require__(747));
16521652
const os = __importStar(__webpack_require__(87));
16531653
const path = __importStar(__webpack_require__(622));
1654+
const util = __importStar(__webpack_require__(669));
16541655
const uuidV4 = __importStar(__webpack_require__(826));
16551656
const constants_1 = __webpack_require__(694);
16561657
// From https://github.com/actions/toolkit/blob/master/packages/tool-cache/src/tool-cache.ts#L23
@@ -1743,6 +1744,10 @@ function isValidEvent() {
17431744
return getSupportedEvents().includes(githubEvent);
17441745
}
17451746
exports.isValidEvent = isValidEvent;
1747+
function unlinkFile(path) {
1748+
return util.promisify(fs.unlink)(path);
1749+
}
1750+
exports.unlinkFile = unlinkFile;
17461751

17471752

17481753
/***/ }),

‎src/restore.ts‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,29 @@ async function run(): Promise<void> {
7575
// Store the cache result
7676
utils.setCacheState(cacheEntry);
7777

78-
// Download the cache from the cache entry
79-
await cacheHttpClient.downloadCache(
80-
cacheEntry.archiveLocation,
81-
archivePath
82-
);
78+
try {
79+
// Download the cache from the cache entry
80+
await cacheHttpClient.downloadCache(
81+
cacheEntry.archiveLocation,
82+
archivePath
83+
);
8384

84-
const archiveFileSize = utils.getArchiveFileSize(archivePath);
85-
core.info(
86-
`Cache Size: ~${Math.round(
87-
archiveFileSize / (1024 * 1024)
88-
)} MB (${archiveFileSize} B)`
89-
);
85+
const archiveFileSize = utils.getArchiveFileSize(archivePath);
86+
core.info(
87+
`Cache Size: ~${Math.round(
88+
archiveFileSize / (1024 * 1024)
89+
)} MB (${archiveFileSize} B)`
90+
);
9091

91-
await extractTar(archivePath, cachePath);
92+
await extractTar(archivePath, cachePath);
93+
} finally {
94+
// Try to delete the archive to save space
95+
try {
96+
await utils.unlinkFile(archivePath);
97+
} catch (error) {
98+
core.debug(`Failed to delete archive: ${error}`);
99+
}
100+
}
92101

93102
const isExactKeyMatch = utils.isExactKeyMatch(
94103
primaryKey,

‎src/utils/actionUtils.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as io from "@actions/io";
33
import * as fs from "fs";
44
import * as os from "os";
55
import * as path from "path";
6+
import * as util from "util";
67
import * as uuidV4 from "uuid/v4";
78

89
import { Events, Outputs, State } from "../constants";
@@ -105,3 +106,7 @@ export function isValidEvent(): boolean {
105106
const githubEvent = process.env[Events.Key] || "";
106107
return getSupportedEvents().includes(githubEvent);
107108
}
109+
110+
export function unlinkFile(path: fs.PathLike): Promise<void> {
111+
return util.promisify(fs.unlink)(path);
112+
}

0 commit comments

Comments
 (0)