Skip to content

Conversation

@helly25
Copy link

@helly25 helly25 commented Mar 11, 2025

Proposal to implement #1570, https://github.com/actions/cache/discussions/1598

Description

Allows to control whether new caches can be written in post action on success.

Motivation and Context

In some cases you want to read but not save a new cache. For instance when you run workflows on a tag action, then you probably want to read caches from the main/default branch, but you most likely do not want to create new caches. The current solution is to separate restore and save actions which is cumbersome.

In #1452 the always_save feature was removed.

I propose to add a new config save-on-success that defaults to true and can be set false to prevent cache writing. Now this new value can be set from available context - just like you could when separating the steps - granted the decision must be available upfront. That above described decision is in fact available immediately on job creation. So we can use the new setting to simplify the setup.

How Has This Been Tested?

Local experiments

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (add or update README or docs)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@helly25 helly25 requested a review from a team as a code owner March 11, 2025 20:21
@helly25 helly25 force-pushed the feat/save_on_success branch from b38d23e to 40b3745 Compare April 25, 2025 13:45
@Rossbro2
Copy link

What is the status of this? This could really improve our workflows!

helly25 and others added 2 commits May 21, 2025 21:40
Co-authored-by: Kyle Ross <[email protected]>
Co-authored-by: Kyle Ross <[email protected]>
@helly25
Copy link
Author

helly25 commented May 21, 2025

What is the status of this? This could really improve our workflows!

An owner needs to approve the workflows and merge on green :-)

@Rossbro2
Copy link

@salmanmkc any chance you could approve, merge & release this? I've tested this on a fork and this is working great!

@eirnym
Copy link

eirnym commented Aug 30, 2025

@salmanmkc Is there's any chance to merge this?

@salmanmkc
Copy link
Contributor

@salmanmkc any chance you could approve, merge & release this? I've tested this on a fork and this is working great!

@salmanmkc Is there's any chance to merge this?

Hi, this is not my area, I'm not one of the reviewers for cache

@eirnym
Copy link

eirnym commented Sep 2, 2025

I see, could you please help us and ping a person responsible?

@eirnym
Copy link

eirnym commented Sep 2, 2025

@GhadimiR hey, could you please review this PR?

main: 'dist/restore/index.js'
post: 'dist/save/index.js'
post-if: "success()"
post-if: "success() && github.event.inputs.save-on-success"
Copy link
Contributor

Choose a reason for hiding this comment

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

This will not work for the same reason that save-always was removed - #1452.

post-if does not have access to the input context, so this will always be false and completely break saving the cache.

To enable this type of workflow, you need to either:

  1. Enable accessing the input context in post-if (requires actions/runner changes)
  2. Use two separate actions for restore and save (this should already work)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for weighing in @joshmgross 🙂

@GhadimiR
Copy link
Contributor

GhadimiR commented Sep 3, 2025

Hi @eirnym, thanks for contributing, as this change won't work due to the limitation pointed out by @joshmgross, we won't be accepting it.

Given that this use-case can be addressed with a combination of save/restore actions, I don't see a need for us to introduce new functionality to achieve the same outcome using a new parameter.

@eirnym
Copy link

eirnym commented Sep 3, 2025

Hi, thank you for your comments. Is it correct to say, that the only way is to write a custom action like it's done in Python setup?

@GhadimiR
Copy link
Contributor

GhadimiR commented Sep 3, 2025

@eirnym that might be the best way to accomplish your use-case, if you want to apply complicated logic to your caching, however for precisely what is described above, it sounds like you could accomplish this with

actions/cache/save@v4

and

actions/cache/restore@v4

You can see some examples that may help you here and combine that with simple conditions based on branch name or whatever else you'd like to act on.

@eirnym
Copy link

eirnym commented Sep 3, 2025

In 99% of cases of such customization (on my experience) it's enough to save only on default branch (whatever the name is), and there will be many users if there will be an official generic cache support of this if functionality provided.

This is important to avoid potential cache poisoning from PRs coming outside.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants