mirror of
				https://github.com/actions/cache.git
				synced 2025-11-04 15:48:39 +08:00 
			
		
		
		
	Add reeval & only-restore boolean input parameters
				
					
				
			* implement only-restore within save
This commit is contained in:
		
							parent
							
								
									c3f1317a9e
								
							
						
					
					
						commit
						4827442d41
					
				
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -94,3 +94,6 @@ typings/
 | 
			
		|||
 | 
			
		||||
# Text editor files
 | 
			
		||||
.vscode/
 | 
			
		||||
 | 
			
		||||
# OS
 | 
			
		||||
.DS_Store
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,6 +14,14 @@ inputs:
 | 
			
		|||
  upload-chunk-size:
 | 
			
		||||
    description: 'The chunk size used to split up large files during upload, in bytes'
 | 
			
		||||
    required: false
 | 
			
		||||
  reeval:
 | 
			
		||||
    description: 'Boolean. Whether to reevaluate the key argument in post. Set to TRUE if you would like your cache key set after your job's steps are complete.'
 | 
			
		||||
    required: false
 | 
			
		||||
    default: false
 | 
			
		||||
  only-restore:
 | 
			
		||||
    description: 'Boolean. Whether to only perform cache restoration and NOT the save post run step.'
 | 
			
		||||
    required: false
 | 
			
		||||
    default: false
 | 
			
		||||
outputs:
 | 
			
		||||
  cache-hit:
 | 
			
		||||
    description: 'A boolean value to indicate an exact match was found for the primary key'
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,8 @@
 | 
			
		|||
export enum Inputs {
 | 
			
		||||
    Key = "key",
 | 
			
		||||
    OnlyRestore = "only-restore",
 | 
			
		||||
    Path = "path",
 | 
			
		||||
    Reeval = "reeval",
 | 
			
		||||
    RestoreKeys = "restore-keys",
 | 
			
		||||
    UploadChunkSize = "upload-chunk-size"
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										99
									
								
								src/save.ts
									
									
									
									
									
								
							
							
						
						
									
										99
									
								
								src/save.ts
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -10,57 +10,60 @@ import * as utils from "./utils/actionUtils";
 | 
			
		|||
process.on("uncaughtException", e => utils.logWarning(e.message));
 | 
			
		||||
 | 
			
		||||
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 save = ! core.getBooleanInput(Inputs.OnlyRestore);
 | 
			
		||||
    if (save) {
 | 
			
		||||
        try {
 | 
			
		||||
            await cache.saveCache(cachePaths, primaryKey, {
 | 
			
		||||
                uploadChunkSize: utils.getInputAsInt(Inputs.UploadChunkSize)
 | 
			
		||||
            });
 | 
			
		||||
            core.info(`Cache saved with key: ${primaryKey}`);
 | 
			
		||||
        } catch (error: unknown) {
 | 
			
		||||
            const typedError = error as Error;
 | 
			
		||||
            if (typedError.name === cache.ValidationError.name) {
 | 
			
		||||
                throw error;
 | 
			
		||||
            } else if (typedError.name === cache.ReserveCacheError.name) {
 | 
			
		||||
                core.info(typedError.message);
 | 
			
		||||
            } else {
 | 
			
		||||
                utils.logWarning(typedError.message);
 | 
			
		||||
            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
 | 
			
		||||
            });
 | 
			
		||||
    
 | 
			
		||||
            try {
 | 
			
		||||
                await cache.saveCache(cachePaths, primaryKey, {
 | 
			
		||||
                    uploadChunkSize: utils.getInputAsInt(Inputs.UploadChunkSize)
 | 
			
		||||
                });
 | 
			
		||||
                core.info(`Cache saved with key: ${primaryKey}`);
 | 
			
		||||
            } catch (error: unknown) {
 | 
			
		||||
                const typedError = error as Error;
 | 
			
		||||
                if (typedError.name === cache.ValidationError.name) {
 | 
			
		||||
                    throw error;
 | 
			
		||||
                } else if (typedError.name === cache.ReserveCacheError.name) {
 | 
			
		||||
                    core.info(typedError.message);
 | 
			
		||||
                } else {
 | 
			
		||||
                    utils.logWarning(typedError.message);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } catch (error: unknown) {
 | 
			
		||||
            utils.logWarning((error as Error).message);
 | 
			
		||||
        }
 | 
			
		||||
    } catch (error: unknown) {
 | 
			
		||||
        utils.logWarning((error as Error).message);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user