mirror of
				https://kkgithub.com/actions/cache.git
				synced 2025-11-01 03:01:50 +08:00 
			
		
		
		
	Compare commits
	
		
			8 Commits
		
	
	
		
			joshmgross
			...
			joshmgross
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 7527073910 | |||
| a631fadf14 | |||
| e223b0a12d | |||
| decbafc350 | |||
| 3854a40aee | |||
| 0188dffc5a | |||
| 002d3a77f4 | |||
| 4809f4ada4 | 
							
								
								
									
										27
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								README.md
									
									
									
									
									
								
							| @ -35,7 +35,7 @@ on: push | ||||
| jobs: | ||||
|   build: | ||||
|     runs-on: ubuntu-latest | ||||
|      | ||||
|  | ||||
|     steps: | ||||
|     - uses: actions/checkout@v1 | ||||
|  | ||||
| @ -49,14 +49,31 @@ jobs: | ||||
|     - name: Generate Prime Numbers | ||||
|       if: steps.cache-primes.outputs.cache-hit != 'true' | ||||
|       run: /generate-primes.sh -d prime-numbers | ||||
|      | ||||
|  | ||||
|     - name: Use Prime Numbers | ||||
|       run: /primes.sh -d prime-numbers | ||||
| ``` | ||||
|  | ||||
| ## Ecosystem Examples | ||||
| ## Implementation Examples | ||||
|  | ||||
| Every programming language and framework has its own way of caching. | ||||
|  | ||||
| See [Examples](examples.md) for a list of `actions/cache` implementations for use with: | ||||
|  | ||||
| - [C# - Nuget](./examples.md#c---nuget) | ||||
| - [Elixir - Mix](./examples.md#elixir---mix) | ||||
| - [Go - Modules](./examples.md#go---modules) | ||||
| - [Java - Gradle](./examples.md#java---gradle) | ||||
| - [Java - Maven](./examples.md#java---maven) | ||||
| - [Node - npm](./examples.md#node---npm) | ||||
| - [Node - Yarn](./examples.md#node---yarn) | ||||
| - [PHP - Composer](./examples.md#php---composer) | ||||
| - [Python - pip](./examples.md#python---pip) | ||||
| - [Ruby - Gem](./examples.md#ruby---gem) | ||||
| - [Rust - Cargo](./examples.md#rust---cargo) | ||||
| - [Swift, Objective-C - Carthage](./examples.md#swift-objective-c---carthage) | ||||
| - [Swift, Objective-C - CocoaPods](./examples.md#swift-objective-c---cocoapods) | ||||
|  | ||||
| See [Examples](examples.md) | ||||
|  | ||||
| ## Cache Limits | ||||
|  | ||||
| @ -76,7 +93,7 @@ steps: | ||||
|     with: | ||||
|       path: path/to/dependencies | ||||
|       key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} | ||||
|    | ||||
|  | ||||
|   - name: Install Dependencies | ||||
|     if: steps.cache.outputs.cache-hit != 'true' | ||||
|     run: /install.sh | ||||
|  | ||||
| @ -6,17 +6,11 @@ jest.mock("@actions/exec"); | ||||
| jest.mock("@actions/io"); | ||||
|  | ||||
| beforeAll(() => { | ||||
|     process.env["windir"] = "C:"; | ||||
|  | ||||
|     jest.spyOn(io, "which").mockImplementation(tool => { | ||||
|         return Promise.resolve(tool); | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| afterAll(() => { | ||||
|     delete process.env["windir"]; | ||||
| }); | ||||
|  | ||||
| test("extract tar", async () => { | ||||
|     const mkdirMock = jest.spyOn(io, "mkdirP"); | ||||
|     const execMock = jest.spyOn(exec, "exec"); | ||||
| @ -28,7 +22,9 @@ test("extract tar", async () => { | ||||
|     expect(mkdirMock).toHaveBeenCalledWith(targetDirectory); | ||||
|  | ||||
|     const IS_WINDOWS = process.platform === "win32"; | ||||
|     const tarPath = IS_WINDOWS ? "C:\\System32\\tar.exe" : "tar"; | ||||
|     const tarPath = IS_WINDOWS | ||||
|         ? `${process.env["windir"]}\\System32\\tar.exe` | ||||
|         : "tar"; | ||||
|     expect(execMock).toHaveBeenCalledTimes(1); | ||||
|     expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [ | ||||
|         "-xz", | ||||
| @ -47,7 +43,9 @@ test("create tar", async () => { | ||||
|     await tar.createTar(archivePath, sourceDirectory); | ||||
|  | ||||
|     const IS_WINDOWS = process.platform === "win32"; | ||||
|     const tarPath = IS_WINDOWS ? "C:\\System32\\tar.exe" : "tar"; | ||||
|     const tarPath = IS_WINDOWS | ||||
|         ? `${process.env["windir"]}\\System32\\tar.exe` | ||||
|         : "tar"; | ||||
|     expect(execMock).toHaveBeenCalledTimes(1); | ||||
|     expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [ | ||||
|         "-cz", | ||||
|  | ||||
							
								
								
									
										48
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										48
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							| @ -5168,35 +5168,53 @@ var __importStar = (this && this.__importStar) || function (mod) { | ||||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||||
| const exec_1 = __webpack_require__(986); | ||||
| const io = __importStar(__webpack_require__(1)); | ||||
| const fs_1 = __webpack_require__(747); | ||||
| function getTarPath() { | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         // Explicitly use BSD Tar on Windows | ||||
|         const IS_WINDOWS = process.platform === "win32"; | ||||
|         if (IS_WINDOWS) { | ||||
|             const systemTar = `${process.env["windir"]}\\System32\\tar.exe`; | ||||
|             if (fs_1.existsSync(systemTar)) { | ||||
|                 return systemTar; | ||||
|             } | ||||
|         } | ||||
|         return yield io.which("tar", true); | ||||
|     }); | ||||
| } | ||||
| function execTar(args) { | ||||
|     var _a, _b; | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         try { | ||||
|             const tarPath = yield getTarPath(); | ||||
|             const tarExec = process.platform !== "win32" ? `sudo ${tarPath}` : tarPath; | ||||
|             yield exec_1.exec(`"${tarExec}"`, args); | ||||
|         } | ||||
|         catch (error) { | ||||
|             const IS_WINDOWS = process.platform === "win32"; | ||||
|             if (IS_WINDOWS) { | ||||
|                 throw new Error(`Tar failed with error: ${(_a = error) === null || _a === void 0 ? void 0 : _a.message}. Ensure BSD tar is installed and on the PATH.`); | ||||
|             } | ||||
|             throw new Error(`Tar failed with error: ${(_b = error) === null || _b === void 0 ? void 0 : _b.message}`); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
| function extractTar(archivePath, targetDirectory) { | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         // Create directory to extract tar into | ||||
|         yield io.mkdirP(targetDirectory); | ||||
|         // http://man7.org/linux/man-pages/man1/tar.1.html | ||||
|         // tar [-options] <name of the tar archive> [files or directories which to add into archive] | ||||
|         const args = ["-xz", "-f", archivePath, "-C", targetDirectory]; | ||||
|         yield exec_1.exec(`"${yield getTarPath()}"`, args); | ||||
|         yield execTar(args); | ||||
|     }); | ||||
| } | ||||
| exports.extractTar = extractTar; | ||||
| function createTar(archivePath, sourceDirectory) { | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         // http://man7.org/linux/man-pages/man1/tar.1.html | ||||
|         // tar [-options] <name of the tar archive> [files or directories which to add into archive] | ||||
|         const args = ["-cz", "-f", archivePath, "-C", sourceDirectory, "."]; | ||||
|         yield exec_1.exec(`"${yield getTarPath()}"`, args); | ||||
|         yield execTar(args); | ||||
|     }); | ||||
| } | ||||
| exports.createTar = createTar; | ||||
| function getTarPath() { | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         // Explicitly use BSD Tar on Windows | ||||
|         const IS_WINDOWS = process.platform === "win32"; | ||||
|         return IS_WINDOWS | ||||
|             ? `${process.env["windir"]}\\System32\\tar.exe` | ||||
|             : yield io.which("tar", true); | ||||
|     }); | ||||
| } | ||||
|  | ||||
|  | ||||
| /***/ }), | ||||
|  | ||||
							
								
								
									
										48
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										48
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							| @ -5142,35 +5142,53 @@ var __importStar = (this && this.__importStar) || function (mod) { | ||||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||||
| const exec_1 = __webpack_require__(986); | ||||
| const io = __importStar(__webpack_require__(1)); | ||||
| const fs_1 = __webpack_require__(747); | ||||
| function getTarPath() { | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         // Explicitly use BSD Tar on Windows | ||||
|         const IS_WINDOWS = process.platform === "win32"; | ||||
|         if (IS_WINDOWS) { | ||||
|             const systemTar = `${process.env["windir"]}\\System32\\tar.exe`; | ||||
|             if (fs_1.existsSync(systemTar)) { | ||||
|                 return systemTar; | ||||
|             } | ||||
|         } | ||||
|         return yield io.which("tar", true); | ||||
|     }); | ||||
| } | ||||
| function execTar(args) { | ||||
|     var _a, _b; | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         try { | ||||
|             const tarPath = yield getTarPath(); | ||||
|             const tarExec = process.platform !== "win32" ? `sudo ${tarPath}` : tarPath; | ||||
|             yield exec_1.exec(`"${tarExec}"`, args); | ||||
|         } | ||||
|         catch (error) { | ||||
|             const IS_WINDOWS = process.platform === "win32"; | ||||
|             if (IS_WINDOWS) { | ||||
|                 throw new Error(`Tar failed with error: ${(_a = error) === null || _a === void 0 ? void 0 : _a.message}. Ensure BSD tar is installed and on the PATH.`); | ||||
|             } | ||||
|             throw new Error(`Tar failed with error: ${(_b = error) === null || _b === void 0 ? void 0 : _b.message}`); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
| function extractTar(archivePath, targetDirectory) { | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         // Create directory to extract tar into | ||||
|         yield io.mkdirP(targetDirectory); | ||||
|         // http://man7.org/linux/man-pages/man1/tar.1.html | ||||
|         // tar [-options] <name of the tar archive> [files or directories which to add into archive] | ||||
|         const args = ["-xz", "-f", archivePath, "-C", targetDirectory]; | ||||
|         yield exec_1.exec(`"${yield getTarPath()}"`, args); | ||||
|         yield execTar(args); | ||||
|     }); | ||||
| } | ||||
| exports.extractTar = extractTar; | ||||
| function createTar(archivePath, sourceDirectory) { | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         // http://man7.org/linux/man-pages/man1/tar.1.html | ||||
|         // tar [-options] <name of the tar archive> [files or directories which to add into archive] | ||||
|         const args = ["-cz", "-f", archivePath, "-C", sourceDirectory, "."]; | ||||
|         yield exec_1.exec(`"${yield getTarPath()}"`, args); | ||||
|         yield execTar(args); | ||||
|     }); | ||||
| } | ||||
| exports.createTar = createTar; | ||||
| function getTarPath() { | ||||
|     return __awaiter(this, void 0, void 0, function* () { | ||||
|         // Explicitly use BSD Tar on Windows | ||||
|         const IS_WINDOWS = process.platform === "win32"; | ||||
|         return IS_WINDOWS | ||||
|             ? `${process.env["windir"]}\\System32\\tar.exe` | ||||
|             : yield io.which("tar", true); | ||||
|     }); | ||||
| } | ||||
|  | ||||
|  | ||||
| /***/ }), | ||||
|  | ||||
							
								
								
									
										19
									
								
								examples.md
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								examples.md
									
									
									
									
									
								
							| @ -1,6 +1,6 @@ | ||||
| # Examples | ||||
|  | ||||
| - [C# - Nuget](#c---nuget) | ||||
| - [C# - NuGet](#c---nuget) | ||||
| - [Elixir - Mix](#elixir---mix) | ||||
| - [Go - Modules](#go---modules) | ||||
| - [Java - Gradle](#java---gradle) | ||||
| @ -14,7 +14,7 @@ | ||||
| - [Swift, Objective-C - Carthage](#swift-objective-c---carthage) | ||||
| - [Swift, Objective-C - CocoaPods](#swift-objective-c---cocoapods) | ||||
|  | ||||
| ## C# - Nuget | ||||
| ## C# - NuGet | ||||
| Using [NuGet lock files](https://docs.microsoft.com/nuget/consume-packages/package-references-in-project-files#locking-dependencies): | ||||
|  | ||||
| ```yaml | ||||
| @ -26,6 +26,21 @@ Using [NuGet lock files](https://docs.microsoft.com/nuget/consume-packages/packa | ||||
|       ${{ runner.os }}-nuget- | ||||
| ``` | ||||
|  | ||||
| Depending on the environment, huge packages might be pre-installed in the global cache folder. | ||||
| If you do not want to include them, consider to move the cache folder like below. | ||||
| >Note: This workflow does not work for projects that require files to be placed in user profile package folder | ||||
| ```yaml | ||||
| env: | ||||
|   NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages | ||||
| steps: | ||||
|   - uses: actions/cache@v1 | ||||
|     with: | ||||
|       path: ${{ github.workspace }}/.nuget/packages | ||||
|       key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} | ||||
|       restore-keys: | | ||||
|         ${{ runner.os }}-nuget- | ||||
| ``` | ||||
|  | ||||
| ## Elixir - Mix | ||||
| ```yaml | ||||
| - uses: actions/cache@v1 | ||||
|  | ||||
							
								
								
									
										12
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										12
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @ -4859,9 +4859,9 @@ | ||||
|       "dev": true | ||||
|     }, | ||||
|     "prettier": { | ||||
|       "version": "1.18.2", | ||||
|       "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.18.2.tgz", | ||||
|       "integrity": "sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==", | ||||
|       "version": "1.19.1", | ||||
|       "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz", | ||||
|       "integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==", | ||||
|       "dev": true | ||||
|     }, | ||||
|     "prettier-linter-helpers": { | ||||
| @ -5983,9 +5983,9 @@ | ||||
|       } | ||||
|     }, | ||||
|     "typescript": { | ||||
|       "version": "3.6.4", | ||||
|       "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz", | ||||
|       "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==", | ||||
|       "version": "3.7.3", | ||||
|       "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.3.tgz", | ||||
|       "integrity": "sha512-Mcr/Qk7hXqFBXMN7p7Lusj1ktCBydylfQM/FZCk5glCNQJrCUKPkMHdo9R0MTFWsC/4kPFvDS0fDPvukfCkFsw==", | ||||
|       "dev": true | ||||
|     }, | ||||
|     "uglify-js": { | ||||
|  | ||||
| @ -46,8 +46,8 @@ | ||||
|     "jest": "^24.8.0", | ||||
|     "jest-circus": "^24.7.1", | ||||
|     "nock": "^11.7.0", | ||||
|     "prettier": "1.18.2", | ||||
|     "prettier": "^1.19.1", | ||||
|     "ts-jest": "^24.0.2", | ||||
|     "typescript": "^3.6.4" | ||||
|     "typescript": "^3.7.3" | ||||
|   } | ||||
| } | ||||
|  | ||||
							
								
								
									
										36
									
								
								src/tar.ts
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								src/tar.ts
									
									
									
									
									
								
							| @ -1,12 +1,33 @@ | ||||
| import { exec } from "@actions/exec"; | ||||
| import * as io from "@actions/io"; | ||||
| import { existsSync } from "fs"; | ||||
|  | ||||
| async function getTarPath(): Promise<string> { | ||||
|     // Explicitly use BSD Tar on Windows | ||||
|     const IS_WINDOWS = process.platform === "win32"; | ||||
|     return IS_WINDOWS | ||||
|         ? `${process.env["windir"]}\\System32\\tar.exe` | ||||
|         : await io.which("tar", true); | ||||
|     if (IS_WINDOWS) { | ||||
|         const systemTar = `${process.env["windir"]}\\System32\\tar.exe`; | ||||
|         if (existsSync(systemTar)) { | ||||
|             return systemTar; | ||||
|         } | ||||
|     } | ||||
|     return await io.which("tar", true); | ||||
| } | ||||
|  | ||||
| async function execTar(args: string[]): Promise<void> { | ||||
|     try { | ||||
|         const tarPath = await getTarPath(); | ||||
|         const tarExec = process.platform !== "win32" ? `sudo ${tarPath}` : tarPath; | ||||
|         await exec(`"${tarExec}"`, args); | ||||
|     } catch (error) { | ||||
|         const IS_WINDOWS = process.platform === "win32"; | ||||
|         if (IS_WINDOWS) { | ||||
|             throw new Error( | ||||
|                 `Tar failed with error: ${error?.message}. Ensure BSD tar is installed and on the PATH.` | ||||
|             ); | ||||
|         } | ||||
|         throw new Error(`Tar failed with error: ${error?.message}`); | ||||
|     } | ||||
| } | ||||
|  | ||||
| export async function extractTar( | ||||
| @ -15,19 +36,14 @@ export async function extractTar( | ||||
| ): Promise<void> { | ||||
|     // Create directory to extract tar into | ||||
|     await io.mkdirP(targetDirectory); | ||||
|  | ||||
|     // http://man7.org/linux/man-pages/man1/tar.1.html | ||||
|     // tar [-options] <name of the tar archive> [files or directories which to add into archive] | ||||
|     const args = ["-xz", "-f", archivePath, "-C", targetDirectory]; | ||||
|     await exec(`"${await getTarPath()}"`, args); | ||||
|     await execTar(args); | ||||
| } | ||||
|  | ||||
| export async function createTar( | ||||
|     archivePath: string, | ||||
|     sourceDirectory: string | ||||
| ): Promise<void> { | ||||
|     // http://man7.org/linux/man-pages/man1/tar.1.html | ||||
|     // tar [-options] <name of the tar archive> [files or directories which to add into archive] | ||||
|     const args = ["-cz", "-f", archivePath, "-C", sourceDirectory, "."]; | ||||
|     await exec(`"${await getTarPath()}"`, args); | ||||
|     await execTar(args); | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	