mirror of
https://code.forgejo.org/actions/cache.git
synced 2025-04-02 04:57:46 +02:00
Only fail if no cache entry is found
This commit is contained in:
parent
d05f7e6483
commit
155f8e7a3c
7 changed files with 13 additions and 21 deletions
|
@ -206,7 +206,7 @@ test("restore with cache found for restore key", async () => {
|
|||
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
test("Fail restore when fail on cache miss is enabled and primary key not found", async () => {
|
||||
test("Fail restore when fail on cache miss is enabled and primary + restore keys not found", async () => {
|
||||
const path = "node_modules";
|
||||
const key = "node-test";
|
||||
const restoreKey = "node-";
|
||||
|
@ -246,7 +246,7 @@ test("Fail restore when fail on cache miss is enabled and primary key not found"
|
|||
expect(failedMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("Fail restore when fail on cache miss is enabled and primary key doesn't match restored key", async () => {
|
||||
test("restore when fail on cache miss is enabled and primary key doesn't match restored key", async () => {
|
||||
const path = "node_modules";
|
||||
const key = "node-test";
|
||||
const restoreKey = "node-";
|
||||
|
@ -257,6 +257,7 @@ test("Fail restore when fail on cache miss is enabled and primary key doesn't ma
|
|||
failOnCacheMiss: true
|
||||
});
|
||||
|
||||
const infoMock = jest.spyOn(core, "info");
|
||||
const failedMock = jest.spyOn(core, "setFailed");
|
||||
const stateMock = jest.spyOn(core, "saveState");
|
||||
const setCacheHitOutputMock = jest.spyOn(core, "setOutput");
|
||||
|
@ -278,11 +279,14 @@ test("Fail restore when fail on cache miss is enabled and primary key doesn't ma
|
|||
);
|
||||
|
||||
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
|
||||
expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", restoreKey);
|
||||
expect(stateMock).toHaveBeenCalledTimes(2);
|
||||
|
||||
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
|
||||
expect(setCacheHitOutputMock).toHaveBeenCalledWith("cache-hit", "false");
|
||||
|
||||
expect(failedMock).toHaveBeenCalledWith(
|
||||
`Restored cache key doesn't match the given input key. Exiting as fail-on-cache-miss is set. Input key: ${key}`
|
||||
expect(infoMock).toHaveBeenCalledWith(
|
||||
`Cache restored from key: ${restoreKey}`
|
||||
);
|
||||
expect(failedMock).toHaveBeenCalledTimes(1);
|
||||
expect(failedMock).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
|
|
@ -19,7 +19,7 @@ inputs:
|
|||
default: 'false'
|
||||
required: false
|
||||
fail-on-cache-miss:
|
||||
description: 'Fail the workflow if the cache is not found for the primary key or no cache is found at all'
|
||||
description: 'Fail the workflow if no cache entry is not found'
|
||||
default: 'false'
|
||||
required: false
|
||||
outputs:
|
||||
|
|
3
dist/restore-only/index.js
vendored
3
dist/restore-only/index.js
vendored
|
@ -50512,9 +50512,6 @@ function restoreImpl(stateProvider) {
|
|||
stateProvider.setState(constants_1.State.CacheMatchedKey, cacheKey);
|
||||
const isExactKeyMatch = utils.isExactKeyMatch(core.getInput(constants_1.Inputs.Key, { required: true }), cacheKey);
|
||||
core.setOutput(constants_1.Outputs.CacheHit, isExactKeyMatch.toString());
|
||||
if (!isExactKeyMatch && failOnCacheMiss) {
|
||||
throw new Error(`Restored cache key doesn't match the given input key. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`);
|
||||
}
|
||||
core.info(`Cache restored from key: ${cacheKey}`);
|
||||
return cacheKey;
|
||||
}
|
||||
|
|
3
dist/restore/index.js
vendored
3
dist/restore/index.js
vendored
|
@ -50512,9 +50512,6 @@ function restoreImpl(stateProvider) {
|
|||
stateProvider.setState(constants_1.State.CacheMatchedKey, cacheKey);
|
||||
const isExactKeyMatch = utils.isExactKeyMatch(core.getInput(constants_1.Inputs.Key, { required: true }), cacheKey);
|
||||
core.setOutput(constants_1.Outputs.CacheHit, isExactKeyMatch.toString());
|
||||
if (!isExactKeyMatch && failOnCacheMiss) {
|
||||
throw new Error(`Restored cache key doesn't match the given input key. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`);
|
||||
}
|
||||
core.info(`Cache restored from key: ${cacheKey}`);
|
||||
return cacheKey;
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ The restore action, as the name suggest, restores a cache. It acts similar to th
|
|||
* `path` - A list of files, directories, and wildcard patterns to cache and restore. See [`@actions/glob`](https://github.com/actions/toolkit/tree/main/packages/glob) for supported patterns.
|
||||
* `key` - String used while saving cache for restoring the cache
|
||||
* `restore-keys` - An ordered list of prefix-matched keys to use for restoring stale cache if no cache hit occurred for key.
|
||||
* `fail-on-cache-miss` - Fail the workflow if the cache is not found for the primary key or no cache is found at all
|
||||
* `fail-on-cache-miss` - Fail the workflow if no cache entry is not found
|
||||
|
||||
## Outputs
|
||||
|
||||
|
@ -96,7 +96,7 @@ steps:
|
|||
|
||||
### Exit workflow on cache miss
|
||||
|
||||
You can use `fail-on-cache-miss: true` to exit the workflow on a cache miss. This way you can restrict your workflow to only initiate the build when a cache with the exact key is found.
|
||||
You can use `fail-on-cache-miss: true` to exit the workflow on a cache miss. This way you can restrict your workflow to only initiate the build when a cache with the exact key is found. Make sure to leave `restore-keys` empty!
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
|
|
|
@ -16,7 +16,7 @@ inputs:
|
|||
default: 'false'
|
||||
required: false
|
||||
fail-on-cache-miss:
|
||||
description: 'Fail the workflow if the cache is not found for the primary key or no cache is found at all'
|
||||
description: 'Fail the workflow if no cache entry is not found'
|
||||
default: 'false'
|
||||
required: false
|
||||
outputs:
|
||||
|
|
|
@ -69,12 +69,6 @@ async function restoreImpl(
|
|||
);
|
||||
|
||||
core.setOutput(Outputs.CacheHit, isExactKeyMatch.toString());
|
||||
if (!isExactKeyMatch && failOnCacheMiss) {
|
||||
throw new Error(
|
||||
`Restored cache key doesn't match the given input key. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`
|
||||
);
|
||||
}
|
||||
|
||||
core.info(`Cache restored from key: ${cacheKey}`);
|
||||
|
||||
return cacheKey;
|
||||
|
|
Loading…
Add table
Reference in a new issue