Compare commits

...

5 Commits

Author SHA1 Message Date
Frank Prins
7dd9af18b0 Add force-overwrite for cache 2024-01-22 18:54:37 +01:00
Yang Cao
a2ed59d39b Merge pull request #1305 from actions/yacaovsnc/update_examples
Update examples
2024-01-17 11:44:48 -05:00
Yang Cao
dc88ab52d7 Update examples 2024-01-17 11:42:01 -05:00
Yang Cao
1d78355196 Merge pull request #1304 from actions/yacaovsnc/update_readme
Update README.md
2024-01-17 11:35:20 -05:00
Yang Cao
c36458f13b Update README.md 2024-01-17 11:31:56 -05:00
4 changed files with 33 additions and 13 deletions

View File

@@ -14,6 +14,15 @@ See ["Caching dependencies to speed up workflows"](https://docs.github.com/en/ac
## What's New ## What's New
### Unreleased
* Added the `force-overwrite` flag to force overwrite any existing cache for incremental caching
### v4
* Updated to node 20
* Added a `save-always` flag to save the cache even if a prior step fails
### v3 ### v3
* Added support for caching in GHES 3.5+. * Added support for caching in GHES 3.5+.
@@ -90,7 +99,7 @@ jobs:
- name: Cache Primes - name: Cache Primes
id: cache-primes id: cache-primes
uses: actions/cache@v3 uses: actions/cache@v4
with: with:
path: prime-numbers path: prime-numbers
key: ${{ runner.os }}-primes key: ${{ runner.os }}-primes
@@ -121,7 +130,7 @@ jobs:
- name: Restore cached Primes - name: Restore cached Primes
id: cache-primes-restore id: cache-primes-restore
uses: actions/cache/restore@v3 uses: actions/cache/restore@v4
with: with:
path: | path: |
path/to/dependencies path/to/dependencies
@@ -132,7 +141,7 @@ jobs:
. .
- name: Save Primes - name: Save Primes
id: cache-primes-save id: cache-primes-save
uses: actions/cache/save@v3 uses: actions/cache/save@v4
with: with:
path: | path: |
path/to/dependencies path/to/dependencies
@@ -186,7 +195,7 @@ A cache key can include any of the contexts, functions, literals, and operators
For example, using the [`hashFiles`](https://docs.github.com/en/actions/learn-github-actions/expressions#hashfiles) function allows you to create a new cache when dependencies change. For example, using the [`hashFiles`](https://docs.github.com/en/actions/learn-github-actions/expressions#hashfiles) function allows you to create a new cache when dependencies change.
```yaml ```yaml
- uses: actions/cache@v3 - uses: actions/cache@v4
with: with:
path: | path: |
path/to/dependencies path/to/dependencies
@@ -204,7 +213,7 @@ Additionally, you can use arbitrary command output in a cache key, such as a dat
echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT
shell: bash shell: bash
- uses: actions/cache@v3 - uses: actions/cache@v4
with: with:
path: path/to/dependencies path: path/to/dependencies
key: ${{ runner.os }}-${{ steps.get-date.outputs.date }}-${{ hashFiles('**/lockfiles') }} key: ${{ runner.os }}-${{ steps.get-date.outputs.date }}-${{ hashFiles('**/lockfiles') }}
@@ -226,7 +235,7 @@ Example:
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- uses: actions/cache@v3 - uses: actions/cache@v4
id: cache id: cache
with: with:
path: path/to/dependencies path: path/to/dependencies
@@ -258,7 +267,7 @@ jobs:
- name: Cache Primes - name: Cache Primes
id: cache-primes id: cache-primes
uses: actions/cache@v3 uses: actions/cache@v4
with: with:
path: prime-numbers path: prime-numbers
key: primes key: primes
@@ -269,7 +278,7 @@ jobs:
- name: Cache Numbers - name: Cache Numbers
id: cache-numbers id: cache-numbers
uses: actions/cache@v3 uses: actions/cache@v4
with: with:
path: numbers path: numbers
key: primes key: primes
@@ -285,7 +294,7 @@ jobs:
- name: Cache Primes - name: Cache Primes
id: cache-primes id: cache-primes
uses: actions/cache@v3 uses: actions/cache@v4
with: with:
path: prime-numbers path: prime-numbers
key: primes key: primes

View File

@@ -29,7 +29,11 @@ inputs:
save-always: save-always:
description: 'Run the post step to save the cache even if another step before fails' description: 'Run the post step to save the cache even if another step before fails'
default: 'false' default: 'false'
required: false required: false
force-overwrite:
description: 'Delete any previous (incremental) cache before pushing a new cache even if a cache for the primary cache key exists'
default: 'false'
required: 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'

View File

@@ -5,7 +5,8 @@ export enum Inputs {
UploadChunkSize = "upload-chunk-size", // Input for cache, save action UploadChunkSize = "upload-chunk-size", // Input for cache, save action
EnableCrossOsArchive = "enableCrossOsArchive", // Input for cache, restore, save action EnableCrossOsArchive = "enableCrossOsArchive", // Input for cache, restore, save action
FailOnCacheMiss = "fail-on-cache-miss", // Input for cache, restore action FailOnCacheMiss = "fail-on-cache-miss", // Input for cache, restore action
LookupOnly = "lookup-only" // Input for cache, restore action LookupOnly = "lookup-only", // Input for cache, restore action
ForceOverwrite = "force-overwrite" // Input for cache action
} }
export enum Outputs { export enum Outputs {

View File

@@ -8,6 +8,7 @@ import {
StateProvider StateProvider
} from "./stateProvider"; } from "./stateProvider";
import * as utils from "./utils/actionUtils"; import * as utils from "./utils/actionUtils";
import { issueCommand } from "@actions/core/lib/command";
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in // 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 // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
@@ -47,13 +48,18 @@ export async function saveImpl(
// NO-OP in case of SaveOnly action // NO-OP in case of SaveOnly action
const restoredKey = stateProvider.getCacheState(); const restoredKey = stateProvider.getCacheState();
if (utils.isExactKeyMatch(primaryKey, restoredKey)) { const forceOverwrite = utils.getInputAsBool(Inputs.ForceOverwrite);
if (utils.isExactKeyMatch(primaryKey, restoredKey) && !forceOverwrite) {
core.info( core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.` `Cache hit occurred on the primary key ${primaryKey} and force-overwrite is disabled, not saving cache.`
); );
return; return;
} }
if (utils.isExactKeyMatch(primaryKey, restoredKey) && forceOverwrite) {
await issueCommand('actions-cache delete', {}, primaryKey);
}
const cachePaths = utils.getInputAsArray(Inputs.Path, { const cachePaths = utils.getInputAsArray(Inputs.Path, {
required: true required: true
}); });