mirror of
				https://github.com/Swatinem/rust-cache
				synced 2025-10-30 20:32:27 +00:00 
			
		
		
		
	support working-directory input, cleanup
This commit is contained in:
		
							parent
							
								
									fb2efae33d
								
							
						
					
					
						commit
						9e10a44ea3
					
				
					 6 changed files with 53 additions and 49 deletions
				
			
		|  | @ -1,5 +1,11 @@ | ||||||
| # Changelog | # Changelog | ||||||
| 
 | 
 | ||||||
|  | ## 1.1.0 | ||||||
|  | 
 | ||||||
|  | - Add a new `working-directory` input. | ||||||
|  | - Support caching git dependencies. | ||||||
|  | - Lots of other improvements. | ||||||
|  | 
 | ||||||
| ## 1.0.2 | ## 1.0.2 | ||||||
| 
 | 
 | ||||||
| - Don’t prune targets that have a different name from the crate, but do prune targets from the workspace. | - Don’t prune targets that have a different name from the crate, but do prune targets from the workspace. | ||||||
|  |  | ||||||
							
								
								
									
										18
									
								
								README.md
									
										
									
									
									
								
							
							
						
						
									
										18
									
								
								README.md
									
										
									
									
									
								
							|  | @ -6,10 +6,26 @@ sensible defaults. | ||||||
| ## Example usage | ## Example usage | ||||||
| 
 | 
 | ||||||
| ```yaml | ```yaml | ||||||
|  | # selecting a toolchain either by action or manual `rustup` calls should happen | ||||||
|  | # before the plugin, as it uses the current rustc version as its cache key | ||||||
|  | - uses: actions-rs/toolchain@v1 | ||||||
|  |   with: | ||||||
|  |     profile: minimal | ||||||
|  |     toolchain: stable | ||||||
|  | 
 | ||||||
| - uses: Swatinem/rust-cache@v1 | - uses: Swatinem/rust-cache@v1 | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| ### Cache Details | ## Inputs | ||||||
|  | 
 | ||||||
|  | : `key` | ||||||
|  | An optional key that is added to the automatic cache key. | ||||||
|  | 
 | ||||||
|  | : `working-directory` | ||||||
|  | The working directory the action operates in, is case the cargo project is not | ||||||
|  | located in the repo root. | ||||||
|  | 
 | ||||||
|  | ## Cache Details | ||||||
| 
 | 
 | ||||||
| The cache currently caches the following directories: | The cache currently caches the following directories: | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -5,6 +5,9 @@ inputs: | ||||||
|   key: |   key: | ||||||
|     description: "An additional key for the cache" |     description: "An additional key for the cache" | ||||||
|     required: false |     required: false | ||||||
|  |   working-directory: | ||||||
|  |     description: "The working directory this action should operate in" | ||||||
|  |     required: false | ||||||
| runs: | runs: | ||||||
|   using: "node12" |   using: "node12" | ||||||
|   main: "dist/restore/index.js" |   main: "dist/restore/index.js" | ||||||
|  |  | ||||||
|  | @ -11,6 +11,11 @@ process.on("uncaughtException", (e) => { | ||||||
|   core.info(`[warning] ${e.message}`); |   core.info(`[warning] ${e.message}`); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | const cwd = core.getInput("working-directory"); | ||||||
|  | if (cwd) { | ||||||
|  |   process.chdir(cwd); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export const stateKey = "RUST_CACHE_KEY"; | export const stateKey = "RUST_CACHE_KEY"; | ||||||
| const stateHash = "RUST_CACHE_HASH"; | const stateHash = "RUST_CACHE_HASH"; | ||||||
| 
 | 
 | ||||||
|  | @ -196,6 +201,7 @@ export async function rmExcept(dirName: string, keepPrefix: Set<string>) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function rm(parent: string, dirent: fs.Dirent) { | export async function rm(parent: string, dirent: fs.Dirent) { | ||||||
|  |   try { | ||||||
|     const fileName = path.join(parent, dirent.name); |     const fileName = path.join(parent, dirent.name); | ||||||
|     core.debug(`deleting "${fileName}"`); |     core.debug(`deleting "${fileName}"`); | ||||||
|     if (dirent.isFile()) { |     if (dirent.isFile()) { | ||||||
|  | @ -203,4 +209,5 @@ export async function rm(parent: string, dirent: fs.Dirent) { | ||||||
|     } else if (dirent.isDirectory()) { |     } else if (dirent.isDirectory()) { | ||||||
|       await io.rmRF(fileName); |       await io.rmRF(fileName); | ||||||
|     } |     } | ||||||
|  |   } catch {} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,21 +1,15 @@ | ||||||
| import * as cache from "@actions/cache"; | import * as cache from "@actions/cache"; | ||||||
| import * as core from "@actions/core"; | import * as core from "@actions/core"; | ||||||
| import { cleanTarget, getCacheConfig, getPackages, isValidEvent, stateKey } from "./common"; | import { cleanTarget, getCacheConfig, getPackages, stateKey } from "./common"; | ||||||
| 
 | 
 | ||||||
| async function run() { | async function run() { | ||||||
|   if (!isValidEvent()) { |  | ||||||
|     return; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   try { |   try { | ||||||
|     core.exportVariable("CARGO_INCREMENTAL", 0); |     core.exportVariable("CARGO_INCREMENTAL", 0); | ||||||
| 
 | 
 | ||||||
|     const start = Date.now(); |  | ||||||
|     const { paths, key, restoreKeys } = await getCacheConfig(); |     const { paths, key, restoreKeys } = await getCacheConfig(); | ||||||
| 
 | 
 | ||||||
|     core.info(`Restoring paths:\n    ${paths.join("\n    ")}`); |     core.info(`Restoring paths:\n    ${paths.join("\n    ")}`); | ||||||
|     core.info(`Using keys:\n    ${[key, ...restoreKeys].join("\n    ")}`); |     core.info(`Using keys:\n    ${[key, ...restoreKeys].join("\n    ")}`); | ||||||
|     try { |  | ||||||
|     const restoreKey = await cache.restoreCache(paths, key, restoreKeys); |     const restoreKey = await cache.restoreCache(paths, key, restoreKeys); | ||||||
|     if (restoreKey) { |     if (restoreKey) { | ||||||
|       core.info(`Restored from cache key "${restoreKey}".`); |       core.info(`Restored from cache key "${restoreKey}".`); | ||||||
|  | @ -33,14 +27,6 @@ async function run() { | ||||||
|   } catch (e) { |   } catch (e) { | ||||||
|     core.info(`[warning] ${e.message}`); |     core.info(`[warning] ${e.message}`); | ||||||
|   } |   } | ||||||
| 
 |  | ||||||
|     const duration = Math.round((Date.now() - start) / 1000); |  | ||||||
|     if (duration) { |  | ||||||
|       core.info(`Took ${duration}s.`); |  | ||||||
|     } |  | ||||||
|   } catch (e) { |  | ||||||
|     core.info(`[warning] ${e.message}`); |  | ||||||
|   } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| run(); | run(); | ||||||
|  |  | ||||||
							
								
								
									
										16
									
								
								src/save.ts
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								src/save.ts
									
										
									
									
									
								
							|  | @ -5,15 +5,10 @@ import * as glob from "@actions/glob"; | ||||||
| import * as io from "@actions/io"; | import * as io from "@actions/io"; | ||||||
| import fs from "fs"; | import fs from "fs"; | ||||||
| import path from "path"; | import path from "path"; | ||||||
| import { cleanTarget, getCacheConfig, getPackages, isValidEvent, Packages, paths, rm, stateKey } from "./common"; | import { cleanTarget, getCacheConfig, getPackages, Packages, paths, rm, stateKey } from "./common"; | ||||||
| 
 | 
 | ||||||
| async function run() { | async function run() { | ||||||
|   if (!isValidEvent()) { |  | ||||||
|     return; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   try { |   try { | ||||||
|     const start = Date.now(); |  | ||||||
|     const { paths: savePaths, key } = await getCacheConfig(); |     const { paths: savePaths, key } = await getCacheConfig(); | ||||||
| 
 | 
 | ||||||
|     if (core.getState(stateKey) === key) { |     if (core.getState(stateKey) === key) { | ||||||
|  | @ -41,19 +36,10 @@ async function run() { | ||||||
| 
 | 
 | ||||||
|     core.info(`Saving paths:\n    ${savePaths.join("\n    ")}`); |     core.info(`Saving paths:\n    ${savePaths.join("\n    ")}`); | ||||||
|     core.info(`Using key "${key}".`); |     core.info(`Using key "${key}".`); | ||||||
|     try { |  | ||||||
|     await cache.saveCache(savePaths, key); |     await cache.saveCache(savePaths, key); | ||||||
|   } catch (e) { |   } catch (e) { | ||||||
|     core.info(`[warning] ${e.message}`); |     core.info(`[warning] ${e.message}`); | ||||||
|   } |   } | ||||||
| 
 |  | ||||||
|     const duration = Math.round((Date.now() - start) / 1000); |  | ||||||
|     if (duration) { |  | ||||||
|       core.info(`Took ${duration}s.`); |  | ||||||
|     } |  | ||||||
|   } catch (e) { |  | ||||||
|     core.info(`[warning] ${e.message}`); |  | ||||||
|   } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| run(); | run(); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue