Compare commits

..

8 Commits

Author SHA1 Message Date
Josh Gross a0a6c0fc37
Merge branch 'main' into feature/bun-example 2024-10-08 15:51:49 -04:00
Josh Gross 3624ceb22c
Restore original behavior of `cache-hit` output (#1467)
* Restore original behavior of `cache-hit` output

* Bump version to 4.1.1
2024-10-08 13:08:22 -04:00
Josh Gross 2cdf405574
Prepare `4.1.0` release (#1464) 2024-10-04 17:00:33 -04:00
Eman Resu a11fb02296
restore action's README now references v4 instead of v3 (#1445)
Co-authored-by: Josh Gross <joshmgross@github.com>
2024-10-03 14:58:28 -04:00
mackey0225 cf7a75e7b9
Fix typo: depening -> depending (#1462)
Co-authored-by: Josh Gross <joshmgross@github.com>
2024-10-03 18:56:27 +00:00
Josh Gross c74ca4022c
Deprecate `save-always` input (#1452)
The `save-always` input added in v4 is not
working as intended due to
`post-if` expressions not supporting the input
context.
To avoid breaking users who have already added
this input to their workflows, it is being
deprecated now and will be removed
in the next major version (v5).
See https://github.com/actions/cache/issues/1315 for more details.
2024-10-03 18:37:55 +00:00
Joel Ambass f8a7ab490b
Merge pull request #1463 from actions/Jcambass-patch-1
Add workflow file for publishing releases to immutable action package
2024-10-03 14:08:27 +02:00
Joel Ambass 45b7be0774
Add workflow file for publishing releases to immutable action package
This workflow file publishes new action releases to the immutable action package of the same name as this repo.

This is part of the Immutable Actions project which is not yet fully released to the public. First party actions like this one are part of our initial testing of this feature.
2024-10-03 08:51:30 +02:00
13 changed files with 89 additions and 47 deletions

View File

@ -0,0 +1,20 @@
name: 'Publish Immutable Action Version'
on:
release:
types: [published]
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
packages: write
steps:
- name: Checking out
uses: actions/checkout@v4
- name: Publish
id: publish
uses: actions/publish-immutable-action@0.0.3

View File

@ -17,7 +17,6 @@ See ["Caching dependencies to speed up workflows"](https://docs.github.com/en/ac
### v4 ### v4
* Updated to node 20 * Updated to node 20
* Added a `save-always` flag to save the cache even if a prior step fails
### v3 ### v3
@ -65,9 +64,9 @@ If you are using a `self-hosted` Windows runner, `GNU tar` and `zstd` are requir
### Outputs ### Outputs
* `cache-hit` - A boolean value to indicate an exact match was found for the key. * `cache-hit` - A string value to indicate an exact match was found for the key.
* If there's a cache hit, this will be 'true' or 'false' to indicate if there's an exact match for `key`.
> **Note** `cache-hit` will only be set to `true` when a cache hit occurs for the exact `key` match. For a partial key match via `restore-keys` or a cache miss, it will be set to `false`. * If there's a cache miss, this will be an empty string.
See [Skipping steps based on cache-hit](#skipping-steps-based-on-cache-hit) for info on using this output See [Skipping steps based on cache-hit](#skipping-steps-based-on-cache-hit) for info on using this output

View File

@ -1,5 +1,12 @@
# Releases # Releases
### 4.1.1
- Restore original behavior of `cache-hit` output - [#1467](https://github.com/actions/cache/pull/1467)
### 4.1.0
- Ensure `cache-hit` output is set when a cache is missed - [#1404](https://github.com/actions/cache/pull/1404)
- Deprecate `save-always` input - [#1452](https://github.com/actions/cache/pull/1452)
### 4.0.2 ### 4.0.2
- Fixed restore `fail-on-cache-miss` not working. - Fixed restore `fail-on-cache-miss` not working.

View File

@ -260,7 +260,7 @@ test("Fail restore when fail on cache miss is enabled and primary + restore keys
); );
expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1); expect(setCacheHitOutputMock).toHaveBeenCalledTimes(0);
expect(failedMock).toHaveBeenCalledWith( expect(failedMock).toHaveBeenCalledWith(
`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${key}` `Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${key}`

View File

@ -86,8 +86,7 @@ test("restore with no cache found", async () => {
); );
expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key); expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key);
expect(outputMock).toHaveBeenCalledWith("cache-hit", "false"); expect(outputMock).toHaveBeenCalledTimes(1);
expect(outputMock).toHaveBeenCalledTimes(2);
expect(failedMock).toHaveBeenCalledTimes(0); expect(failedMock).toHaveBeenCalledTimes(0);
expect(infoMock).toHaveBeenCalledWith( expect(infoMock).toHaveBeenCalledWith(

View File

@ -30,6 +30,10 @@ inputs:
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
deprecationMessage: |
save-always does not work as intended and will be removed in a future release.
A separate `actions/cache/restore` step should be used instead.
See https://github.com/actions/cache/tree/main/save#always-save-cache for more details.
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'
@ -37,7 +41,7 @@ runs:
using: 'node20' using: 'node20'
main: 'dist/restore/index.js' main: 'dist/restore/index.js'
post: 'dist/save/index.js' post: 'dist/save/index.js'
post-if: "success() || github.event.inputs.save-always" post-if: "success()"
branding: branding:
icon: 'archive' icon: 'archive'
color: 'gray-dark' color: 'gray-dark'

View File

@ -81,7 +81,7 @@ On similar lines, commit sha can be used to create a very specialized and short
key: cache-${{ github.sha }} key: cache-${{ github.sha }}
``` ```
### Using multiple factors while forming a key depening on the need ### Using multiple factors while forming a key depending on the need
Cache key can be formed by combination of more than one metadata, evaluated info. Cache key can be formed by combination of more than one metadata, evaluated info.
@ -243,24 +243,7 @@ with:
### Saving cache even if the build fails ### Saving cache even if the build fails
There can be cases where a cache should be saved even if the build job fails. For example, a job can fail due to flaky tests but the caches can still be re-used. You can use `actions/cache/save` action to save the cache by using `if: always()` condition. See [Always save cache](./save/README.md#always-save-cache).
Similarly, `actions/cache/save` action can be conditionally used based on the output of the previous steps. This way you get more control on when to save the cache.
```yaml
steps:
- uses: actions/checkout@v4
.
. // restore if need be
.
- name: Build
run: /build.sh
- uses: actions/cache/save@v4
if: always() // or any other condition to invoke the save action
with:
path: path/to/dependencies
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
```
### Saving cache once and reusing in multiple workflows ### Saving cache once and reusing in multiple workflows

View File

@ -59415,7 +59415,8 @@ function restoreImpl(stateProvider, earlyExit) {
const lookupOnly = utils.getInputAsBool(constants_1.Inputs.LookupOnly); const lookupOnly = utils.getInputAsBool(constants_1.Inputs.LookupOnly);
const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys, { lookupOnly: lookupOnly }, enableCrossOsArchive); const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys, { lookupOnly: lookupOnly }, enableCrossOsArchive);
if (!cacheKey) { if (!cacheKey) {
core.setOutput(constants_1.Outputs.CacheHit, false.toString()); // `cache-hit` is intentionally not set to `false` here to preserve existing behavior
// See https://github.com/actions/cache/issues/1466
if (failOnCacheMiss) { if (failOnCacheMiss) {
throw new Error(`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`); throw new Error(`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`);
} }

View File

@ -59415,7 +59415,8 @@ function restoreImpl(stateProvider, earlyExit) {
const lookupOnly = utils.getInputAsBool(constants_1.Inputs.LookupOnly); const lookupOnly = utils.getInputAsBool(constants_1.Inputs.LookupOnly);
const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys, { lookupOnly: lookupOnly }, enableCrossOsArchive); const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys, { lookupOnly: lookupOnly }, enableCrossOsArchive);
if (!cacheKey) { if (!cacheKey) {
core.setOutput(constants_1.Outputs.CacheHit, false.toString()); // `cache-hit` is intentionally not set to `false` here to preserve existing behavior
// See https://github.com/actions/cache/issues/1466
if (failOnCacheMiss) { if (failOnCacheMiss) {
throw new Error(`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`); throw new Error(`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`);
} }

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "cache", "name": "cache",
"version": "4.0.2", "version": "4.1.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "cache", "name": "cache",
"version": "4.0.2", "version": "4.1.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/cache": "^3.2.3", "@actions/cache": "^3.2.3",

View File

@ -1,6 +1,6 @@
{ {
"name": "cache", "name": "cache",
"version": "4.0.2", "version": "4.1.1",
"private": true, "private": true,
"description": "Cache dependencies and build outputs", "description": "Cache dependencies and build outputs",
"main": "dist/restore/index.js", "main": "dist/restore/index.js",

View File

@ -70,19 +70,45 @@ with:
### Always save cache ### Always save cache
There are instances where some flaky test cases would fail the entire workflow and users would get frustrated because the builds would run for hours and the cache couldn't be saved as the workflow failed in between. For such use-cases, users now have the ability to use the `actions/cache/save` action to save the cache by using an `if: always()` condition. This way the cache will always be saved if generated, or a warning will be generated that nothing is found on the cache path. Users can also use the `if` condition to only execute the `actions/cache/save` action depending on the output of previous steps. This way they get more control of when to save the cache. There are instances where some flaky test cases would fail the entire workflow and users would get frustrated because the builds would run for hours and the cache couldn't be saved as the workflow failed in between.
For such use-cases, users now have the ability to use the `actions/cache/save` action to save the cache by using an [`always()`](https://docs.github.com/actions/writing-workflows/choosing-what-your-workflow-does/expressions#always) condition.
This way the cache will always be saved if generated, or a warning will be generated that nothing is found on the cache path. Users can also use the `if` condition to only execute the `actions/cache/save` action depending on the output of previous steps. This way they get more control of when to save the cache.
To avoid saving a cache that already exists, the `cache-hit` output from a restore step should be checked.
The `cache-primary-key` output from the restore step should also be used to ensure
the cache key does not change during the build if it's calculated based on file contents.
```yaml ```yaml
name: Always Caching Primes
on: push
jobs:
build:
runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
.
. // restore if need be - name: Restore cached Primes
. id: cache-primes-restore
- name: Build uses: actions/cache/restore@v4
run: /build.sh
- uses: actions/cache/save@v4
if: always() // or any other condition to invoke the save action
with: with:
path: path/to/dependencies key: ${{ runner.os }}-primes
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} path: |
path/to/dependencies
some/other/dependencies
# Intermediate workflow steps
- name: Always Save Primes
id: cache-primes-save
if: always() && steps.cache-primes-restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
key: ${{ steps.cache-primes-restore.outputs.cache-primary-key }}
path: |
path/to/dependencies
some/other/dependencies
``` ```

View File

@ -51,7 +51,9 @@ export async function restoreImpl(
); );
if (!cacheKey) { if (!cacheKey) {
core.setOutput(Outputs.CacheHit, false.toString()); // `cache-hit` is intentionally not set to `false` here to preserve existing behavior
// See https://github.com/actions/cache/issues/1466
if (failOnCacheMiss) { if (failOnCacheMiss) {
throw new Error( throw new Error(
`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}` `Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`