mirror of
				https://github.com/actions/cache.git
				synced 2025-11-04 10:14:02 +00:00 
			
		
		
		
	Add option reevaluate the key
This commit is contained in:
		
							parent
							
								
									5c79b3fd6c
								
							
						
					
					
						commit
						fa2f4155aa
					
				@ -389,3 +389,31 @@ test("save with valid inputs uploads a cache", async () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    expect(failedMock).toHaveBeenCalledTimes(0);
 | 
					    expect(failedMock).toHaveBeenCalledTimes(0);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("save with reevaluate-key uses the key", async () => {
 | 
				
			||||||
 | 
					    const failedMock = jest.spyOn(core, "setFailed");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const inputPath = "node_modules";
 | 
				
			||||||
 | 
					    testUtils.setInput(Inputs.Path, inputPath);
 | 
				
			||||||
 | 
					    testUtils.setInput(Inputs.Key, primaryKey);
 | 
				
			||||||
 | 
					    testUtils.setInput(Inputs.ReevaluateKey, "true");
 | 
				
			||||||
 | 
					    testUtils.setInput(Inputs.UploadChunkSize, "4000000");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const cacheId = 4;
 | 
				
			||||||
 | 
					    const saveCacheMock = jest
 | 
				
			||||||
 | 
					        .spyOn(cache, "saveCache")
 | 
				
			||||||
 | 
					        .mockImplementationOnce(() => {
 | 
				
			||||||
 | 
					            return Promise.resolve(cacheId);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await run();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    expect(saveCacheMock).toHaveBeenCalledTimes(1);
 | 
				
			||||||
 | 
					    expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey, {
 | 
				
			||||||
 | 
					        uploadChunkSize: 4000000
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    expect(failedMock).toHaveBeenCalledTimes(0);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -14,6 +14,10 @@ inputs:
 | 
				
			|||||||
  upload-chunk-size:
 | 
					  upload-chunk-size:
 | 
				
			||||||
    description: 'The chunk size used to split up large files during upload, in bytes'
 | 
					    description: 'The chunk size used to split up large files during upload, in bytes'
 | 
				
			||||||
    required: false
 | 
					    required: false
 | 
				
			||||||
 | 
					  reevaluate-key:
 | 
				
			||||||
 | 
					    description: 'If set to true, the key will be re-evaluated when saving the cache.'
 | 
				
			||||||
 | 
					    required: false
 | 
				
			||||||
 | 
					    default: false
 | 
				
			||||||
outputs:
 | 
					outputs:
 | 
				
			||||||
  cache-hit:
 | 
					  cache-hit:
 | 
				
			||||||
    description: 'A boolean value to indicate an exact match was found for the primary key'
 | 
					    description: 'A boolean value to indicate an exact match was found for the primary key'
 | 
				
			||||||
 | 
				
			|||||||
@ -2,7 +2,8 @@ export enum Inputs {
 | 
				
			|||||||
    Key = "key",
 | 
					    Key = "key",
 | 
				
			||||||
    Path = "path",
 | 
					    Path = "path",
 | 
				
			||||||
    RestoreKeys = "restore-keys",
 | 
					    RestoreKeys = "restore-keys",
 | 
				
			||||||
    UploadChunkSize = "upload-chunk-size"
 | 
					    UploadChunkSize = "upload-chunk-size",
 | 
				
			||||||
 | 
					    ReevaluateKey = "reevaluate-key"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export enum Outputs {
 | 
					export enum Outputs {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										16
									
								
								src/save.ts
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								src/save.ts
									
									
									
									
									
								
							@ -26,11 +26,17 @@ async function run(): Promise<void> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        const state = utils.getCacheState();
 | 
					        const state = utils.getCacheState();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Inputs are re-evaluted before the post action, so we want the original key used for restore
 | 
					        let primaryKey : string;
 | 
				
			||||||
        const primaryKey = core.getState(State.CachePrimaryKey);
 | 
					        if (core.getInput(Inputs.ReevaluateKey)?.toLowerCase() === 'true') {
 | 
				
			||||||
        if (!primaryKey) {
 | 
					            // Inputs are re-evaluated before the post action
 | 
				
			||||||
            utils.logWarning(`Error retrieving key from state.`);
 | 
					            primaryKey = core.getInput(Inputs.Key);
 | 
				
			||||||
            return;
 | 
					        } else {
 | 
				
			||||||
 | 
					            // Get the original key used for restore from the cache
 | 
				
			||||||
 | 
					            primaryKey = core.getState(State.CachePrimaryKey);
 | 
				
			||||||
 | 
					            if (!primaryKey) {
 | 
				
			||||||
 | 
					                utils.logWarning(`Error retrieving key from state.`);
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (utils.isExactKeyMatch(primaryKey, state)) {
 | 
					        if (utils.isExactKeyMatch(primaryKey, state)) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user