mirror of
				https://gitea.com/actions/cache.git
				synced 2025-10-26 07:16:38 +00:00 
			
		
		
		
	Compare commits
	
		
			39 Commits
		
	
	
		
			phantsure/
			...
			bishal/out
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | 657c52f11e | ||
|   | 34e917cb7d | ||
|   | ac8fc97c06 | ||
|   | 86712a0733 | ||
|   | d6e98d9302 | ||
|   | a76826ef46 | ||
|   | e02e5113ed | ||
|   | 85ae5bbcea | ||
|   | cce93fb2c7 | ||
|   | e3d8fb0b34 | ||
|   | d95c048983 | ||
|   | 706c369cf1 | ||
|   | 11ab7ccfa2 | ||
|   | 4b5f33df54 | ||
|   | 56a0133650 | ||
|   | 19446b165a | ||
|   | 8a88690a20 | ||
|   | 6e2c6a5916 | ||
|   | 2c9fb32186 | ||
|   | 01d96636a0 | ||
|   | 9c5a42a7c9 | ||
|   | a172494938 | ||
|   | f8717682fb | ||
|   | af1210e2a3 | ||
|   | ab0e7714ce | ||
|   | fb4a5dce60 | ||
|   | 71334c58b2 | ||
|   | 888d454557 | ||
|   | dddd7ce07c | ||
|   | abddc4dd44 | ||
|   | 921c58ee44 | ||
|   | 7f45813c72 | ||
|   | 0769f2e443 | ||
|   | 5fe0b944ef | ||
|   | 69b8227b27 | ||
|   | 515d10b4fd | ||
|   | 669e7536d9 | ||
|   | 29dbbce762 | ||
|   | ea5981db97 | 
							
								
								
									
										14
									
								
								.devcontainer/devcontainer.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								.devcontainer/devcontainer.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| { | ||||
| 	"name": "Node.js & TypeScript", | ||||
| 	"image": "mcr.microsoft.com/devcontainers/typescript-node:16-bullseye", | ||||
| 	// Features to add to the dev container. More info: https://containers.dev/implementors/features. | ||||
| 	// "features": {}, | ||||
| 	// Use 'forwardPorts' to make a list of ports inside the container available locally. | ||||
| 	// "forwardPorts": [], | ||||
| 	// Use 'postCreateCommand' to run commands after the container is created. | ||||
| 	"postCreateCommand": "npm install && npm run build" | ||||
| 	// Configure tool-specific properties. | ||||
| 	// "customizations": {}, | ||||
| 	// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. | ||||
| 	// "remoteUser": "root" | ||||
| } | ||||
| @@ -40,9 +40,3 @@ | ||||
| ### 3.0.11 | ||||
| - Update toolkit version to 3.0.5 to include `@actions/core@^1.10.0` | ||||
| - Update `@actions/cache` to use updated `saveState` and `setOutput` functions from `@actions/core@^1.10.0` | ||||
|  | ||||
| ### 3.1.0-beta.1 | ||||
| - Update `@actions/cache` on windows to use gnu tar and zstd by default and fallback to bsdtar and zstd if gnu tar is not available. ([issue](https://github.com/actions/cache/issues/984)) | ||||
|  | ||||
| ### 3.1.0-beta.2 | ||||
| - Added support for fallback to gzip to restore old caches on windows. | ||||
|   | ||||
| @@ -102,7 +102,7 @@ test("restore on GHES with AC available ", async () => { | ||||
|     const infoMock = jest.spyOn(core, "info"); | ||||
|     const failedMock = jest.spyOn(core, "setFailed"); | ||||
|     const stateMock = jest.spyOn(core, "saveState"); | ||||
|     const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput"); | ||||
|     const setCacheHitOutputMock = jest.spyOn(core, "setOutput"); | ||||
|     const restoreCacheMock = jest | ||||
|         .spyOn(cache, "restoreCache") | ||||
|         .mockImplementationOnce(() => { | ||||
| @@ -116,7 +116,7 @@ test("restore on GHES with AC available ", async () => { | ||||
|  | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledWith(true); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "true"); | ||||
|  | ||||
|     expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`); | ||||
|     expect(failedMock).toHaveBeenCalledTimes(0); | ||||
| @@ -270,7 +270,7 @@ test("restore with cache found for key", async () => { | ||||
|     const infoMock = jest.spyOn(core, "info"); | ||||
|     const failedMock = jest.spyOn(core, "setFailed"); | ||||
|     const stateMock = jest.spyOn(core, "saveState"); | ||||
|     const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput"); | ||||
|     const setCacheHitOutputMock = jest.spyOn(core, "setOutput"); | ||||
|     const restoreCacheMock = jest | ||||
|         .spyOn(cache, "restoreCache") | ||||
|         .mockImplementationOnce(() => { | ||||
| @@ -284,7 +284,7 @@ test("restore with cache found for key", async () => { | ||||
|  | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledWith(true); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "true"); | ||||
|  | ||||
|     expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`); | ||||
|     expect(failedMock).toHaveBeenCalledTimes(0); | ||||
| @@ -303,7 +303,7 @@ test("restore with cache found for restore key", async () => { | ||||
|     const infoMock = jest.spyOn(core, "info"); | ||||
|     const failedMock = jest.spyOn(core, "setFailed"); | ||||
|     const stateMock = jest.spyOn(core, "saveState"); | ||||
|     const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput"); | ||||
|     const setCacheHitOutputMock = jest.spyOn(core, "setOutput"); | ||||
|     const restoreCacheMock = jest | ||||
|         .spyOn(cache, "restoreCache") | ||||
|         .mockImplementationOnce(() => { | ||||
| @@ -317,8 +317,7 @@ test("restore with cache found for restore key", async () => { | ||||
|  | ||||
|     expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledWith(false); | ||||
|  | ||||
|     expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "false"); | ||||
|     expect(infoMock).toHaveBeenCalledWith( | ||||
|         `Cache restored from key: ${restoreKey}` | ||||
|     ); | ||||
|   | ||||
| @@ -21,7 +21,7 @@ runs: | ||||
|   using: 'node16' | ||||
|   main: 'dist/restore/index.js' | ||||
|   post: 'dist/save/index.js' | ||||
|   post-if: 'success()' | ||||
|   post-if: success() | ||||
| branding: | ||||
|   icon: 'archive' | ||||
|   color: 'gray-dark' | ||||
|   | ||||
							
								
								
									
										5565
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5565
									
								
								dist/restore/index.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										1022
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1022
									
								
								dist/save/index.js
									
									
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										21
									
								
								examples.md
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								examples.md
									
									
									
									
									
								
							| @@ -309,29 +309,14 @@ We cache the elements of the Cabal store separately, as the entirety of `~/.caba | ||||
| For npm, cache files are stored in `~/.npm` on Posix, or `~\AppData\npm-cache` on Windows, but it's possible to use `npm config get cache` to find the path on any platform. See [the npm docs](https://docs.npmjs.com/cli/cache#cache) for more details. | ||||
|  | ||||
| If using `npm config` to retrieve the cache directory, ensure you run [actions/setup-node](https://github.com/actions/setup-node) first to ensure your `npm` version is correct. | ||||
| After [deprecation](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/) of save-state and set-output commands, the correct way to set output is using `${GITHUB_OUTPUT}`. For linux, we can use `${GITHUB_OUTPUT}` whereas for windows we need to use `${env:GITHUB_OUTPUT}` due to two different default shells in these two different OS ie `bash` and `pwsh` respectively. | ||||
|  | ||||
| >Note: It is not recommended to cache `node_modules`, as it can break across Node versions and won't work with `npm ci` | ||||
|  | ||||
| ### **Get npm cache directory using same shell** | ||||
| ### Bash shell | ||||
| ```yaml | ||||
| - name: Get npm cache directory | ||||
|   id: npm-cache | ||||
|   shell: bash | ||||
|   run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} | ||||
| ``` | ||||
|  | ||||
| ### PWSH shell | ||||
| ```yaml | ||||
| - name: Get npm cache directory | ||||
|   id: npm-cache | ||||
|   shell: pwsh | ||||
|   run: echo "dir=$(npm config get cache)" >> ${env:GITHUB_OUTPUT} | ||||
| ``` | ||||
| `Get npm cache directory` step can then be used with `actions/cache` as shown below | ||||
|  | ||||
| ```yaml | ||||
|   id: npm-cache-dir | ||||
|   run: | | ||||
|     echo "::set-output name=dir::$(npm config get cache)" | ||||
| - uses: actions/cache@v3 | ||||
|   id: npm-cache # use this to check for `cache-hit` ==> if: steps.npm-cache.outputs.cache-hit != 'true' | ||||
|   with: | ||||
|   | ||||
							
								
								
									
										38
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										38
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @@ -1,15 +1,15 @@ | ||||
| { | ||||
|   "name": "cache", | ||||
|   "version": "3.1.0-beta.2", | ||||
|   "version": "3.0.11", | ||||
|   "lockfileVersion": 2, | ||||
|   "requires": true, | ||||
|   "packages": { | ||||
|     "": { | ||||
|       "name": "cache", | ||||
|       "version": "3.1.0-beta.2", | ||||
|       "version": "3.0.11", | ||||
|       "license": "MIT", | ||||
|       "dependencies": { | ||||
|         "@actions/cache": "3.1.0-beta.2", | ||||
|         "@actions/cache": "^3.0.5", | ||||
|         "@actions/core": "^1.10.0", | ||||
|         "@actions/exec": "^1.1.1", | ||||
|         "@actions/io": "^1.1.2" | ||||
| @@ -36,16 +36,15 @@ | ||||
|       } | ||||
|     }, | ||||
|     "node_modules/@actions/cache": { | ||||
|       "version": "3.1.0-beta.2", | ||||
|       "resolved": "https://registry.npmjs.org/@actions/cache/-/cache-3.1.0-beta.2.tgz", | ||||
|       "integrity": "sha512-xt9NLWPCh5WU9Z5ITeGpT5Nza/57wMXeLsGuNVcRCIVpPuNTf3Puj82vjZZQw4rGqiCCs+n4+hnkTcE9BKw2sw==", | ||||
|       "version": "3.0.5", | ||||
|       "resolved": "https://registry.npmjs.org/@actions/cache/-/cache-3.0.5.tgz", | ||||
|       "integrity": "sha512-0WpPmwnRPkn5k5ASmjoX8bY8NrZEPTwN+64nGYJmR/bHjEVgC8svdf5K956wi67tNJBGJky2+UfvNbUOtHmMHg==", | ||||
|       "dependencies": { | ||||
|         "@actions/core": "^1.10.0", | ||||
|         "@actions/exec": "^1.0.1", | ||||
|         "@actions/glob": "^0.1.0", | ||||
|         "@actions/http-client": "^2.0.1", | ||||
|         "@actions/io": "^1.0.1", | ||||
|         "@azure/abort-controller": "^1.1.0", | ||||
|         "@azure/ms-rest-js": "^2.6.0", | ||||
|         "@azure/storage-blob": "^12.8.0", | ||||
|         "semver": "^6.1.0", | ||||
| @@ -112,14 +111,14 @@ | ||||
|       } | ||||
|     }, | ||||
|     "node_modules/@azure/abort-controller": { | ||||
|       "version": "1.1.0", | ||||
|       "resolved": "https://registry.npmjs.org/@azure/abort-controller/-/abort-controller-1.1.0.tgz", | ||||
|       "integrity": "sha512-TrRLIoSQVzfAJX9H1JeFjzAoDGcoK1IYX1UImfceTZpsyYfWr09Ss1aHW1y5TrrR3iq6RZLBwJ3E24uwPhwahw==", | ||||
|       "version": "1.0.4", | ||||
|       "resolved": "https://registry.npmjs.org/@azure/abort-controller/-/abort-controller-1.0.4.tgz", | ||||
|       "integrity": "sha512-lNUmDRVGpanCsiUN3NWxFTdwmdFI53xwhkTFfHDGTYk46ca7Ind3nanJc+U6Zj9Tv+9nTCWRBscWEW1DyKOpTw==", | ||||
|       "dependencies": { | ||||
|         "tslib": "^2.2.0" | ||||
|         "tslib": "^2.0.0" | ||||
|       }, | ||||
|       "engines": { | ||||
|         "node": ">=12.0.0" | ||||
|         "node": ">=8.0.0" | ||||
|       } | ||||
|     }, | ||||
|     "node_modules/@azure/abort-controller/node_modules/tslib": { | ||||
| @@ -9722,16 +9721,15 @@ | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "@actions/cache": { | ||||
|       "version": "3.1.0-beta.2", | ||||
|       "resolved": "https://registry.npmjs.org/@actions/cache/-/cache-3.1.0-beta.2.tgz", | ||||
|       "integrity": "sha512-xt9NLWPCh5WU9Z5ITeGpT5Nza/57wMXeLsGuNVcRCIVpPuNTf3Puj82vjZZQw4rGqiCCs+n4+hnkTcE9BKw2sw==", | ||||
|       "version": "3.0.5", | ||||
|       "resolved": "https://registry.npmjs.org/@actions/cache/-/cache-3.0.5.tgz", | ||||
|       "integrity": "sha512-0WpPmwnRPkn5k5ASmjoX8bY8NrZEPTwN+64nGYJmR/bHjEVgC8svdf5K956wi67tNJBGJky2+UfvNbUOtHmMHg==", | ||||
|       "requires": { | ||||
|         "@actions/core": "^1.10.0", | ||||
|         "@actions/exec": "^1.0.1", | ||||
|         "@actions/glob": "^0.1.0", | ||||
|         "@actions/http-client": "^2.0.1", | ||||
|         "@actions/io": "^1.0.1", | ||||
|         "@azure/abort-controller": "^1.1.0", | ||||
|         "@azure/ms-rest-js": "^2.6.0", | ||||
|         "@azure/storage-blob": "^12.8.0", | ||||
|         "semver": "^6.1.0", | ||||
| @@ -9794,11 +9792,11 @@ | ||||
|       } | ||||
|     }, | ||||
|     "@azure/abort-controller": { | ||||
|       "version": "1.1.0", | ||||
|       "resolved": "https://registry.npmjs.org/@azure/abort-controller/-/abort-controller-1.1.0.tgz", | ||||
|       "integrity": "sha512-TrRLIoSQVzfAJX9H1JeFjzAoDGcoK1IYX1UImfceTZpsyYfWr09Ss1aHW1y5TrrR3iq6RZLBwJ3E24uwPhwahw==", | ||||
|       "version": "1.0.4", | ||||
|       "resolved": "https://registry.npmjs.org/@azure/abort-controller/-/abort-controller-1.0.4.tgz", | ||||
|       "integrity": "sha512-lNUmDRVGpanCsiUN3NWxFTdwmdFI53xwhkTFfHDGTYk46ca7Ind3nanJc+U6Zj9Tv+9nTCWRBscWEW1DyKOpTw==", | ||||
|       "requires": { | ||||
|         "tslib": "^2.2.0" | ||||
|         "tslib": "^2.0.0" | ||||
|       }, | ||||
|       "dependencies": { | ||||
|         "tslib": { | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "cache", | ||||
|   "version": "3.1.0-beta.2", | ||||
|   "version": "3.0.11", | ||||
|   "private": true, | ||||
|   "description": "Cache dependencies and build outputs", | ||||
|   "main": "dist/restore/index.js", | ||||
| @@ -23,7 +23,7 @@ | ||||
|   "author": "GitHub", | ||||
|   "license": "MIT", | ||||
|   "dependencies": { | ||||
|     "@actions/cache": "3.1.0-beta.2", | ||||
|     "@actions/cache": "^3.0.5", | ||||
|     "@actions/core": "^1.10.0", | ||||
|     "@actions/exec": "^1.1.1", | ||||
|     "@actions/io": "^1.1.2" | ||||
|   | ||||
							
								
								
									
										23
									
								
								restore/action.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								restore/action.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| name: 'Restore Cache' | ||||
| description: 'Restore Cache artifacts like dependencies and build outputs to improve workflow execution time' | ||||
| author: 'GitHub' | ||||
| inputs: | ||||
|   path: | ||||
|     description: 'The same list of files, directories, and wildcard patterns to restore cache that were used while saving it' | ||||
|     required: true | ||||
|   key: | ||||
|     description: 'An explicit key for restoring the cache' | ||||
|     required: true | ||||
|   restore-keys: | ||||
|     description: 'An ordered list of keys to use for restoring stale cache if no cache hit occurred for key. Note `cache-hit` returns false in this case.' | ||||
|     required: false | ||||
| outputs: | ||||
|   cache-hit: | ||||
|     description: 'A boolean value to indicate an exact match was found for the primary key' | ||||
| runs: | ||||
|   using: 'node16' | ||||
|   main: '../dist/restore/index.js' | ||||
| branding: | ||||
|   icon: 'archive' | ||||
|   color: 'gray-dark' | ||||
|    | ||||
							
								
								
									
										19
									
								
								save/action.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								save/action.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| name: 'Save Cache' | ||||
| description: 'Save Cache artifacts like dependencies and build outputs to improve workflow execution time' | ||||
| author: 'GitHub' | ||||
| inputs: | ||||
|   path: | ||||
|     description: 'A list of files, directories, and wildcard patterns to cache' | ||||
|     required: true | ||||
|   key: | ||||
|     description: 'An explicit key for saving the cache' | ||||
|     required: true | ||||
|   upload-chunk-size: | ||||
|     description: 'The chunk size used to split up large files during upload, in bytes' | ||||
|     required: false | ||||
| runs: | ||||
|   using: 'node16' | ||||
|   main: '../dist/save/index.js' | ||||
| branding: | ||||
|   icon: 'archive' | ||||
|   color: 'gray-dark' | ||||
| @@ -1,60 +1,8 @@ | ||||
| import * as cache from "@actions/cache"; | ||||
| import * as core from "@actions/core"; | ||||
|  | ||||
| import { Events, Inputs, State } from "./constants"; | ||||
| import * as utils from "./utils/actionUtils"; | ||||
| import { StateProvider } from "./stateProvider"; | ||||
| import restoreImpl from "./restoreImpl"; | ||||
|  | ||||
| async function run(): Promise<void> { | ||||
|     try { | ||||
|         if (!utils.isCacheFeatureAvailable()) { | ||||
|             utils.setCacheHitOutput(false); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         // Validate inputs, this can cause task failure | ||||
|         if (!utils.isValidEvent()) { | ||||
|             utils.logWarning( | ||||
|                 `Event Validation Error: The event type ${ | ||||
|                     process.env[Events.Key] | ||||
|                 } is not supported because it's not tied to a branch or tag ref.` | ||||
|             ); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         const primaryKey = core.getInput(Inputs.Key, { required: true }); | ||||
|         core.saveState(State.CachePrimaryKey, primaryKey); | ||||
|  | ||||
|         const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys); | ||||
|         const cachePaths = utils.getInputAsArray(Inputs.Path, { | ||||
|             required: true | ||||
|         }); | ||||
|  | ||||
|         const cacheKey = await cache.restoreCache( | ||||
|             cachePaths, | ||||
|             primaryKey, | ||||
|             restoreKeys | ||||
|         ); | ||||
|  | ||||
|         if (!cacheKey) { | ||||
|             core.info( | ||||
|                 `Cache not found for input keys: ${[ | ||||
|                     primaryKey, | ||||
|                     ...restoreKeys | ||||
|                 ].join(", ")}` | ||||
|             ); | ||||
|  | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         // Store the matched cache key | ||||
|         utils.setCacheState(cacheKey); | ||||
|  | ||||
|         const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey); | ||||
|         utils.setCacheHitOutput(isExactKeyMatch); | ||||
|         core.info(`Cache restored from key: ${cacheKey}`); | ||||
|     } catch (error: unknown) { | ||||
|         core.setFailed((error as Error).message); | ||||
|     } | ||||
|     await restoreImpl(new StateProvider()); | ||||
| } | ||||
|  | ||||
| run(); | ||||
|   | ||||
							
								
								
									
										67
									
								
								src/restoreImpl.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/restoreImpl.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,67 @@ | ||||
| import * as cache from "@actions/cache"; | ||||
| import * as core from "@actions/core"; | ||||
|  | ||||
| import { Events, Inputs, Outputs, State } from "./constants"; | ||||
| import { IStateProvider } from "./stateProvider"; | ||||
| import * as utils from "./utils/actionUtils"; | ||||
|  | ||||
| async function restoreImpl(stateProvider: IStateProvider): Promise<string | undefined> { | ||||
|     try { | ||||
|         if (!utils.isCacheFeatureAvailable()) { | ||||
|             utils.setCacheHitOutput(false); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         // Validate inputs, this can cause task failure | ||||
|         if (!utils.isValidEvent()) { | ||||
|             utils.logWarning( | ||||
|                 `Event Validation Error: The event type ${ | ||||
|                     process.env[Events.Key] | ||||
|                 } is not supported because it's not tied to a branch or tag ref.` | ||||
|             ); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         const primaryKey = core.getInput(Inputs.Key, { required: true }); | ||||
|         stateProvider.setState(State.CachePrimaryKey, primaryKey); | ||||
|  | ||||
|         const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys); | ||||
|         const cachePaths = utils.getInputAsArray(Inputs.Path, { | ||||
|             required: true | ||||
|         }); | ||||
|  | ||||
|         const cacheKey = await cache.restoreCache( | ||||
|             cachePaths, | ||||
|             primaryKey, | ||||
|             restoreKeys | ||||
|         ); | ||||
|  | ||||
|         if (!cacheKey) { | ||||
|             core.info( | ||||
|                 `Cache not found for input keys: ${[ | ||||
|                     primaryKey, | ||||
|                     ...restoreKeys | ||||
|                 ].join(", ")}` | ||||
|             ); | ||||
|  | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         // Store the matched cache key in states | ||||
|         stateProvider.setState(State.CacheMatchedKey, cacheKey); | ||||
|  | ||||
|         const isExactKeyMatch = utils.isExactKeyMatch( | ||||
|             core.getInput(Inputs.Key, { required: true }), | ||||
|             cacheKey | ||||
|         ); | ||||
|  | ||||
|         core.setOutput(Outputs.CacheHit, isExactKeyMatch.toString()); | ||||
|         core.info(`Cache restored from key: ${cacheKey}`); | ||||
|  | ||||
|         return cacheKey; | ||||
|     } catch (error: unknown) { | ||||
|         core.setFailed((error as Error).message); | ||||
|     } | ||||
| } | ||||
|  | ||||
| export default restoreImpl; | ||||
							
								
								
									
										10
									
								
								src/restoreOnly.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/restoreOnly.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| import restoreImpl from "./restoreImpl"; | ||||
| import { NullStateProvider } from "./stateProvider"; | ||||
|  | ||||
| async function run(): Promise<void> { | ||||
|     await restoreImpl(new NullStateProvider()); | ||||
| } | ||||
|  | ||||
| run(); | ||||
|  | ||||
| export default run; | ||||
							
								
								
									
										57
									
								
								src/save.ts
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								src/save.ts
									
									
									
									
									
								
							| @@ -1,59 +1,8 @@ | ||||
| import * as cache from "@actions/cache"; | ||||
| import * as core from "@actions/core"; | ||||
|  | ||||
| import { Events, Inputs, State } from "./constants"; | ||||
| import * as utils from "./utils/actionUtils"; | ||||
|  | ||||
| // Catch and log any unhandled exceptions.  These exceptions can leak out of the uploadChunk method in | ||||
| // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to | ||||
| // throw an uncaught exception.  Instead of failing this action, just warn. | ||||
| process.on("uncaughtException", e => utils.logWarning(e.message)); | ||||
| import saveImpl from "./saveImpl"; | ||||
| import { StateProvider } from "./stateProvider"; | ||||
|  | ||||
| async function run(): Promise<void> { | ||||
|     try { | ||||
|         if (!utils.isCacheFeatureAvailable()) { | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         if (!utils.isValidEvent()) { | ||||
|             utils.logWarning( | ||||
|                 `Event Validation Error: The event type ${ | ||||
|                     process.env[Events.Key] | ||||
|                 } is not supported because it's not tied to a branch or tag ref.` | ||||
|             ); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         const state = utils.getCacheState(); | ||||
|  | ||||
|         // Inputs are re-evaluted before the post action, so we want the original key used for restore | ||||
|         const primaryKey = core.getState(State.CachePrimaryKey); | ||||
|         if (!primaryKey) { | ||||
|             utils.logWarning(`Error retrieving key from state.`); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         if (utils.isExactKeyMatch(primaryKey, state)) { | ||||
|             core.info( | ||||
|                 `Cache hit occurred on the primary key ${primaryKey}, not saving cache.` | ||||
|             ); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         const cachePaths = utils.getInputAsArray(Inputs.Path, { | ||||
|             required: true | ||||
|         }); | ||||
|  | ||||
|         const cacheId = await cache.saveCache(cachePaths, primaryKey, { | ||||
|             uploadChunkSize: utils.getInputAsInt(Inputs.UploadChunkSize) | ||||
|         }); | ||||
|  | ||||
|         if (cacheId != -1) { | ||||
|             core.info(`Cache saved with key: ${primaryKey}`); | ||||
|         } | ||||
|     } catch (error: unknown) { | ||||
|         utils.logWarning((error as Error).message); | ||||
|     } | ||||
|     await saveImpl(new StateProvider()); | ||||
| } | ||||
|  | ||||
| run(); | ||||
|   | ||||
							
								
								
									
										65
									
								
								src/saveImpl.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								src/saveImpl.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| import * as cache from "@actions/cache"; | ||||
| import * as core from "@actions/core"; | ||||
|  | ||||
| import { Events, Inputs, State } from "./constants"; | ||||
| import { IStateProvider } from "./stateProvider"; | ||||
| import * as utils from "./utils/actionUtils"; | ||||
|  | ||||
| // Catch and log any unhandled exceptions.  These exceptions can leak out of the uploadChunk method in | ||||
| // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to | ||||
| // throw an uncaught exception.  Instead of failing this action, just warn. | ||||
| process.on("uncaughtException", e => utils.logWarning(e.message)); | ||||
|  | ||||
| async function saveImpl(stateProvider: IStateProvider): Promise<void> { | ||||
|     try { | ||||
|         if (!utils.isCacheFeatureAvailable()) { | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         if (!utils.isValidEvent()) { | ||||
|             utils.logWarning( | ||||
|                 `Event Validation Error: The event type ${ | ||||
|                     process.env[Events.Key] | ||||
|                 } is not supported because it's not tied to a branch or tag ref.` | ||||
|             ); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         // If restore has stored a primary key in state, reuse that | ||||
|         // Else re-evaluate from inputs | ||||
|         const primaryKey = | ||||
|             stateProvider.getState(State.CachePrimaryKey) || | ||||
|             core.getInput(Inputs.Key); | ||||
|  | ||||
|         if (!primaryKey) { | ||||
|             utils.logWarning(`Error retrieving key from state.`); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         // If matched restore key is same as primary key, then do not save cache | ||||
|         // NO-OP in case of SaveOnly action | ||||
|         const state = stateProvider.getCacheState(); | ||||
|         if (utils.isExactKeyMatch(primaryKey, state)) { | ||||
|             core.info( | ||||
|                 `Cache hit occurred on the primary key ${primaryKey}, not saving cache.` | ||||
|             ); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         const cachePaths = utils.getInputAsArray(Inputs.Path, { | ||||
|             required: true | ||||
|         }); | ||||
|  | ||||
|         const cacheId = await cache.saveCache(cachePaths, primaryKey, { | ||||
|             uploadChunkSize: utils.getInputAsInt(Inputs.UploadChunkSize) | ||||
|         }); | ||||
|  | ||||
|         if (cacheId != -1) { | ||||
|             core.info(`Cache saved with key: ${primaryKey}`); | ||||
|         } | ||||
|     } catch (error: unknown) { | ||||
|         utils.logWarning((error as Error).message); | ||||
|     } | ||||
| } | ||||
|  | ||||
| export default saveImpl; | ||||
							
								
								
									
										10
									
								
								src/saveOnly.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/saveOnly.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| import saveImpl from "./saveImpl"; | ||||
| import { NullStateProvider } from "./stateProvider"; | ||||
|  | ||||
| async function run(): Promise<void> { | ||||
|     await saveImpl(new NullStateProvider()); | ||||
| } | ||||
|  | ||||
| run(); | ||||
|  | ||||
| export default run; | ||||
							
								
								
									
										42
									
								
								src/stateProvider.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/stateProvider.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| import * as core from "@actions/core"; | ||||
|  | ||||
| import { State } from "./constants"; | ||||
|  | ||||
| export interface IStateProvider { | ||||
|     //setOutput(key: string, value: string): void; | ||||
|     setState(key: string, value: string): void; | ||||
|     getState(key: string): string; | ||||
|  | ||||
|     getCacheState(): string | undefined; | ||||
| } | ||||
|  | ||||
| class StateProviderBase implements IStateProvider { | ||||
|     getCacheState(): string | undefined { | ||||
|         const cacheKey = this.getState(State.CacheMatchedKey); | ||||
|         if (cacheKey) { | ||||
|             core.debug(`Cache state/key: ${cacheKey}`); | ||||
|             return cacheKey; | ||||
|         } | ||||
|  | ||||
|         return undefined; | ||||
|     } | ||||
|  | ||||
|     // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function | ||||
|     setState = (key: string, value: string) => {}; | ||||
|  | ||||
|     // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||||
|     getState = (key: string) => ""; | ||||
| } | ||||
|  | ||||
| export class StateProvider extends StateProviderBase { | ||||
|     //setOutput = core.setOutput; | ||||
|     setState = core.saveState; | ||||
|     getState = core.getState; | ||||
| } | ||||
|  | ||||
| export class NullStateProvider extends StateProviderBase { | ||||
|     //setOutput = core.setOutput; | ||||
|     setState = core.setOutput; | ||||
|     // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||||
|     getState = (key: string) => ""; | ||||
| } | ||||
| @@ -1,7 +1,7 @@ | ||||
| import * as cache from "@actions/cache"; | ||||
| import * as core from "@actions/core"; | ||||
|  | ||||
| import { Outputs, RefKey, State } from "../constants"; | ||||
| import { Outputs, RefKey } from "../constants"; | ||||
|  | ||||
| export function isGhes(): boolean { | ||||
|     const ghUrl = new URL( | ||||
| @@ -19,30 +19,10 @@ export function isExactKeyMatch(key: string, cacheKey?: string): boolean { | ||||
|     ); | ||||
| } | ||||
|  | ||||
| export function setCacheState(state: string): void { | ||||
|     core.saveState(State.CacheMatchedKey, state); | ||||
| } | ||||
|  | ||||
| export function setCacheHitOutput(isCacheHit: boolean): void { | ||||
|     core.setOutput(Outputs.CacheHit, isCacheHit.toString()); | ||||
| } | ||||
|  | ||||
| export function setOutputAndState(key: string, cacheKey?: string): void { | ||||
|     setCacheHitOutput(isExactKeyMatch(key, cacheKey)); | ||||
|     // Store the matched cache key if it exists | ||||
|     cacheKey && setCacheState(cacheKey); | ||||
| } | ||||
|  | ||||
| export function getCacheState(): string | undefined { | ||||
|     const cacheKey = core.getState(State.CacheMatchedKey); | ||||
|     if (cacheKey) { | ||||
|         core.debug(`Cache state/key: ${cacheKey}`); | ||||
|         return cacheKey; | ||||
|     } | ||||
|  | ||||
|     return undefined; | ||||
| } | ||||
|  | ||||
| export function logWarning(message: string): void { | ||||
|     const warningPrefix = "[warning]"; | ||||
|     core.info(`${warningPrefix}${message}`); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user