Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add force-overwrite for cache #1308

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -14,6 +14,10 @@ See ["Caching dependencies to speed up workflows"](https://docs.github.com/en/ac

## What's New

### Unreleased

* Added the `force-overwrite` flag to force overwrite any existing cache for incremental caching

### v4

* Updated to node 20
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -29,7 +29,11 @@ inputs:
save-always:
description: 'Run the post step to save the cache even if another step before fails'
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:
cache-hit:
description: 'A boolean value to indicate an exact match was found for the primary key'
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,8 @@ export enum Inputs {
UploadChunkSize = "upload-chunk-size", // Input for cache, save action
EnableCrossOsArchive = "enableCrossOsArchive", // Input for cache, restore, save 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 {
10 changes: 8 additions & 2 deletions src/saveImpl.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import {
StateProvider
} from "./stateProvider";
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
// @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
const restoredKey = stateProvider.getCacheState();

if (utils.isExactKeyMatch(primaryKey, restoredKey)) {
const forceOverwrite = utils.getInputAsBool(Inputs.ForceOverwrite);
if (utils.isExactKeyMatch(primaryKey, restoredKey) && !forceOverwrite) {
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;
}

if (utils.isExactKeyMatch(primaryKey, restoredKey) && forceOverwrite) {
await issueCommand('actions-cache delete', {}, primaryKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PrinsFrank were you able to validate these changes in your own repository?

I'm not entirely sure if the backend supporting the cache action will allow overwriting an existing cache entry like this, and as far as I know this actions-cache delete command is not a real command.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I got confused with the CI options that are available in the actions-cache plugin, but not in the toolkit yet. I use that here: https://github.com/PrinsFrank/CI-PHP/blob/main/.github/workflows/quality.yml#L134, so I can confirm this workflow works, the code just currently doesn't. I wasn't sure how to run this code, but I figured it out and can see this doesn't work in the current form.

If I understand correctly there's currently not any code for this at all in upstream packages, so I tried implementing it in actions/toolkit#1783. As I mentioned before, I'm not a typescript developer so any pointers would be very much appreciated.

}

const cachePaths = utils.getInputAsArray(Inputs.Path, {
required: true
});