mirror of
				https://github.com/actions/cache.git
				synced 2025-10-29 09:58:35 +08:00 
			
		
		
		
	Attempt to delete the archive after extraction (#209)
This reduces storage space used once the Action has finished executing.
This commit is contained in:
		
							parent
							
								
									af8651e0c5
								
							
						
					
					
						commit
						cae64ca3cd
					
				|  | @ -1,4 +1,5 @@ | ||||||
| import * as core from "@actions/core"; | import * as core from "@actions/core"; | ||||||
|  | import * as fs from "fs"; | ||||||
| import * as os from "os"; | import * as os from "os"; | ||||||
| import * as path from "path"; | import * as path from "path"; | ||||||
| 
 | 
 | ||||||
|  | @ -234,3 +235,15 @@ test("isValidEvent returns true for pull request event", () => { | ||||||
| 
 | 
 | ||||||
|     expect(isValidEvent).toBe(true); |     expect(isValidEvent).toBe(true); | ||||||
| }); | }); | ||||||
|  | 
 | ||||||
|  | test("unlinkFile unlinks file", async () => { | ||||||
|  |     const testDirectory = fs.mkdtempSync("unlinkFileTest"); | ||||||
|  |     const testFile = path.join(testDirectory, "test.txt"); | ||||||
|  |     fs.writeFileSync(testFile, "hello world"); | ||||||
|  | 
 | ||||||
|  |     await actionUtils.unlinkFile(testFile); | ||||||
|  | 
 | ||||||
|  |     expect(fs.existsSync(testFile)).toBe(false); | ||||||
|  | 
 | ||||||
|  |     fs.rmdirSync(testDirectory); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -241,6 +241,7 @@ test("restore with cache found", async () => { | ||||||
|         .mockReturnValue(fileSize); |         .mockReturnValue(fileSize); | ||||||
| 
 | 
 | ||||||
|     const extractTarMock = jest.spyOn(tar, "extractTar"); |     const extractTarMock = jest.spyOn(tar, "extractTar"); | ||||||
|  |     const unlinkFileMock = jest.spyOn(actionUtils, "unlinkFile"); | ||||||
|     const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput"); |     const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput"); | ||||||
| 
 | 
 | ||||||
|     await run(); |     await run(); | ||||||
|  | @ -258,6 +259,9 @@ test("restore with cache found", async () => { | ||||||
|     expect(extractTarMock).toHaveBeenCalledTimes(1); |     expect(extractTarMock).toHaveBeenCalledTimes(1); | ||||||
|     expect(extractTarMock).toHaveBeenCalledWith(archivePath, cachePath); |     expect(extractTarMock).toHaveBeenCalledWith(archivePath, cachePath); | ||||||
| 
 | 
 | ||||||
|  |     expect(unlinkFileMock).toHaveBeenCalledTimes(1); | ||||||
|  |     expect(unlinkFileMock).toHaveBeenCalledWith(archivePath); | ||||||
|  | 
 | ||||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); |     expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); | ||||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledWith(true); |     expect(setCacheHitOutputMock).toHaveBeenCalledWith(true); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										28
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										28
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							|  | @ -1651,6 +1651,7 @@ const io = __importStar(__webpack_require__(1)); | ||||||
| const fs = __importStar(__webpack_require__(747)); | const fs = __importStar(__webpack_require__(747)); | ||||||
| const os = __importStar(__webpack_require__(87)); | const os = __importStar(__webpack_require__(87)); | ||||||
| const path = __importStar(__webpack_require__(622)); | const path = __importStar(__webpack_require__(622)); | ||||||
|  | const util = __importStar(__webpack_require__(669)); | ||||||
| const uuidV4 = __importStar(__webpack_require__(826)); | const uuidV4 = __importStar(__webpack_require__(826)); | ||||||
| const constants_1 = __webpack_require__(694); | const constants_1 = __webpack_require__(694); | ||||||
| // From https://github.com/actions/toolkit/blob/master/packages/tool-cache/src/tool-cache.ts#L23
 | // From https://github.com/actions/toolkit/blob/master/packages/tool-cache/src/tool-cache.ts#L23
 | ||||||
|  | @ -1743,6 +1744,10 @@ function isValidEvent() { | ||||||
|     return getSupportedEvents().includes(githubEvent); |     return getSupportedEvents().includes(githubEvent); | ||||||
| } | } | ||||||
| exports.isValidEvent = isValidEvent; | exports.isValidEvent = isValidEvent; | ||||||
|  | function unlinkFile(path) { | ||||||
|  |     return util.promisify(fs.unlink)(path); | ||||||
|  | } | ||||||
|  | exports.unlinkFile = unlinkFile; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| /***/ }), | /***/ }), | ||||||
|  | @ -2831,18 +2836,29 @@ function run() { | ||||||
|             try { |             try { | ||||||
|                 const cacheEntry = yield cacheHttpClient.getCacheEntry(keys); |                 const cacheEntry = yield cacheHttpClient.getCacheEntry(keys); | ||||||
|                 if (!((_a = cacheEntry) === null || _a === void 0 ? void 0 : _a.archiveLocation)) { |                 if (!((_a = cacheEntry) === null || _a === void 0 ? void 0 : _a.archiveLocation)) { | ||||||
|                     core.info(`Cache not found for input keys: ${keys.join(", ")}.`); |                     core.info(`Cache not found for input keys: ${keys.join(", ")}`); | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
|                 const archivePath = path.join(yield utils.createTempDirectory(), "cache.tgz"); |                 const archivePath = path.join(yield utils.createTempDirectory(), "cache.tgz"); | ||||||
|                 core.debug(`Archive Path: ${archivePath}`); |                 core.debug(`Archive Path: ${archivePath}`); | ||||||
|                 // Store the cache result
 |                 // Store the cache result
 | ||||||
|                 utils.setCacheState(cacheEntry); |                 utils.setCacheState(cacheEntry); | ||||||
|                 // Download the cache from the cache entry
 |                 try { | ||||||
|                 yield cacheHttpClient.downloadCache(cacheEntry.archiveLocation, archivePath); |                     // Download the cache from the cache entry
 | ||||||
|                 const archiveFileSize = utils.getArchiveFileSize(archivePath); |                     yield cacheHttpClient.downloadCache(cacheEntry.archiveLocation, archivePath); | ||||||
|                 core.info(`Cache Size: ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B)`); |                     const archiveFileSize = utils.getArchiveFileSize(archivePath); | ||||||
|                 yield tar_1.extractTar(archivePath, cachePath); |                     core.info(`Cache Size: ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B)`); | ||||||
|  |                     yield tar_1.extractTar(archivePath, cachePath); | ||||||
|  |                 } | ||||||
|  |                 finally { | ||||||
|  |                     // Try to delete the archive to save space
 | ||||||
|  |                     try { | ||||||
|  |                         yield utils.unlinkFile(archivePath); | ||||||
|  |                     } | ||||||
|  |                     catch (error) { | ||||||
|  |                         core.debug(`Failed to delete archive: ${error}`); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|                 const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheEntry); |                 const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheEntry); | ||||||
|                 utils.setCacheHitOutput(isExactKeyMatch); |                 utils.setCacheHitOutput(isExactKeyMatch); | ||||||
|                 core.info(`Cache restored from key: ${cacheEntry && cacheEntry.cacheKey}`); |                 core.info(`Cache restored from key: ${cacheEntry && cacheEntry.cacheKey}`); | ||||||
|  |  | ||||||
							
								
								
									
										5
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							|  | @ -1651,6 +1651,7 @@ const io = __importStar(__webpack_require__(1)); | ||||||
| const fs = __importStar(__webpack_require__(747)); | const fs = __importStar(__webpack_require__(747)); | ||||||
| const os = __importStar(__webpack_require__(87)); | const os = __importStar(__webpack_require__(87)); | ||||||
| const path = __importStar(__webpack_require__(622)); | const path = __importStar(__webpack_require__(622)); | ||||||
|  | const util = __importStar(__webpack_require__(669)); | ||||||
| const uuidV4 = __importStar(__webpack_require__(826)); | const uuidV4 = __importStar(__webpack_require__(826)); | ||||||
| const constants_1 = __webpack_require__(694); | const constants_1 = __webpack_require__(694); | ||||||
| // From https://github.com/actions/toolkit/blob/master/packages/tool-cache/src/tool-cache.ts#L23
 | // From https://github.com/actions/toolkit/blob/master/packages/tool-cache/src/tool-cache.ts#L23
 | ||||||
|  | @ -1743,6 +1744,10 @@ function isValidEvent() { | ||||||
|     return getSupportedEvents().includes(githubEvent); |     return getSupportedEvents().includes(githubEvent); | ||||||
| } | } | ||||||
| exports.isValidEvent = isValidEvent; | exports.isValidEvent = isValidEvent; | ||||||
|  | function unlinkFile(path) { | ||||||
|  |     return util.promisify(fs.unlink)(path); | ||||||
|  | } | ||||||
|  | exports.unlinkFile = unlinkFile; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| /***/ }), | /***/ }), | ||||||
|  |  | ||||||
|  | @ -75,20 +75,29 @@ async function run(): Promise<void> { | ||||||
|             // Store the cache result
 |             // Store the cache result
 | ||||||
|             utils.setCacheState(cacheEntry); |             utils.setCacheState(cacheEntry); | ||||||
| 
 | 
 | ||||||
|             // Download the cache from the cache entry
 |             try { | ||||||
|             await cacheHttpClient.downloadCache( |                 // Download the cache from the cache entry
 | ||||||
|                 cacheEntry.archiveLocation, |                 await cacheHttpClient.downloadCache( | ||||||
|                 archivePath |                     cacheEntry.archiveLocation, | ||||||
|             ); |                     archivePath | ||||||
|  |                 ); | ||||||
| 
 | 
 | ||||||
|             const archiveFileSize = utils.getArchiveFileSize(archivePath); |                 const archiveFileSize = utils.getArchiveFileSize(archivePath); | ||||||
|             core.info( |                 core.info( | ||||||
|                 `Cache Size: ~${Math.round( |                     `Cache Size: ~${Math.round( | ||||||
|                     archiveFileSize / (1024 * 1024) |                         archiveFileSize / (1024 * 1024) | ||||||
|                 )} MB (${archiveFileSize} B)` |                     )} MB (${archiveFileSize} B)` | ||||||
|             ); |                 ); | ||||||
| 
 | 
 | ||||||
|             await extractTar(archivePath, cachePath); |                 await extractTar(archivePath, cachePath); | ||||||
|  |             } finally { | ||||||
|  |                 // Try to delete the archive to save space
 | ||||||
|  |                 try { | ||||||
|  |                     await utils.unlinkFile(archivePath); | ||||||
|  |                 } catch (error) { | ||||||
|  |                     core.debug(`Failed to delete archive: ${error}`); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             const isExactKeyMatch = utils.isExactKeyMatch( |             const isExactKeyMatch = utils.isExactKeyMatch( | ||||||
|                 primaryKey, |                 primaryKey, | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ import * as io from "@actions/io"; | ||||||
| import * as fs from "fs"; | import * as fs from "fs"; | ||||||
| import * as os from "os"; | import * as os from "os"; | ||||||
| import * as path from "path"; | import * as path from "path"; | ||||||
|  | import * as util from "util"; | ||||||
| import * as uuidV4 from "uuid/v4"; | import * as uuidV4 from "uuid/v4"; | ||||||
| 
 | 
 | ||||||
| import { Events, Outputs, State } from "../constants"; | import { Events, Outputs, State } from "../constants"; | ||||||
|  | @ -105,3 +106,7 @@ export function isValidEvent(): boolean { | ||||||
|     const githubEvent = process.env[Events.Key] || ""; |     const githubEvent = process.env[Events.Key] || ""; | ||||||
|     return getSupportedEvents().includes(githubEvent); |     return getSupportedEvents().includes(githubEvent); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | export function unlinkFile(path: fs.PathLike): Promise<void> { | ||||||
|  |     return util.promisify(fs.unlink)(path); | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Henry Mercer
						Henry Mercer