Add inputs restore-cache and save-cache (#568)

Closes: #555
This commit is contained in:
Kevin Stillhammer
2025-09-14 14:55:08 +02:00
committed by GitHub
parent f67343ac2e
commit dc724a12b6
8 changed files with 149 additions and 9 deletions

View File

@ -457,7 +457,7 @@ jobs:
working-directory: __tests__/fixtures/requirements-txt-project working-directory: __tests__/fixtures/requirements-txt-project
test-restore-cache-requirements-txt: test-restore-cache-requirements-txt:
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: test-setup-cache needs: test-setup-cache-requirements-txt
steps: steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with: with:
@ -525,6 +525,78 @@ jobs:
env: env:
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }} CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}
test-setup-cache-save-cache-false:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: Setup with cache
uses: ./
with:
enable-cache: true
save-cache: false
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-save-cache-false
- run: uv sync
working-directory: __tests__/fixtures/uv-project
shell: bash
test-restore-cache-save-cache-false:
runs-on: ubuntu-latest
needs: test-setup-cache-save-cache-false
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: Restore with cache
id: restore
uses: ./
with:
enable-cache: true
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-save-cache-false
- name: Cache was not hit
run: |
if [ "$CACHE_HIT" == "true" ]; then
exit 1
fi
env:
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}
test-setup-cache-restore-cache-false:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: Setup with cache
uses: ./
with:
enable-cache: true
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-restore-cache-false
- run: uv sync
working-directory: __tests__/fixtures/uv-project
shell: bash
test-restore-cache-restore-cache-false:
runs-on: ubuntu-latest
needs: test-setup-cache-restore-cache-false
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: Restore with cache
id: restore
uses: ./
with:
enable-cache: true
restore-cache: false
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-restore-cache-false
- name: Cache was not hit
run: |
if [ "$CACHE_HIT" == "true" ]; then
exit 1
fi
env:
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}
test-cache-local: test-cache-local:
strategy: strategy:
matrix: matrix:
@ -747,6 +819,10 @@ jobs:
- test-restore-cache-requirements-txt - test-restore-cache-requirements-txt
- test-setup-cache-dependency-glob - test-setup-cache-dependency-glob
- test-restore-cache-dependency-glob - test-restore-cache-dependency-glob
- test-setup-cache-save-cache-false
- test-restore-cache-save-cache-false
- test-setup-cache-restore-cache-false
- test-restore-cache-restore-cache-false
- test-setup-cache-local - test-setup-cache-local
- test-restore-cache-local - test-restore-cache-local
- test-tilde-expansion-cache-local-path - test-tilde-expansion-cache-local-path

View File

@ -22,6 +22,8 @@ Set up your GitHub Actions workflow with a specific version of [uv](https://docs
- [Validate checksum](#validate-checksum) - [Validate checksum](#validate-checksum)
- [Enable Caching](#enable-caching) - [Enable Caching](#enable-caching)
- [Cache dependency glob](#cache-dependency-glob) - [Cache dependency glob](#cache-dependency-glob)
- [Restore cache](#restore-cache)
- [Save cache](#save-cache)
- [Local cache path](#local-cache-path) - [Local cache path](#local-cache-path)
- [Disable cache pruning](#disable-cache-pruning) - [Disable cache pruning](#disable-cache-pruning)
- [Ignore nothing to cache](#ignore-nothing-to-cache) - [Ignore nothing to cache](#ignore-nothing-to-cache)
@ -284,6 +286,33 @@ changes. If you use relative paths, they are relative to the repository root.
cache-dependency-glob: "" cache-dependency-glob: ""
``` ```
#### Restore cache
Restoring an existing cache can be enabled or disabled with the `restore-cache` input.
By default, the cache will be restored.
```yaml
- name: Don't restore an existing cache
uses: astral-sh/setup-uv@v6
with:
enable-cache: true
restore-cache: false
```
#### Save cache
You can also disable saving the cache after the run with the `save-cache` input.
This can be useful to save cache storage when you know you will not use the cache of the run again.
By default, the cache will be saved.
```yaml
- name: Don't save the cache after the run
uses: astral-sh/setup-uv@v6
with:
enable-cache: true
save-cache: false
```
### Local cache path ### Local cache path
This action controls where uv stores its cache on the runner's filesystem by setting `UV_CACHE_DIR`. This action controls where uv stores its cache on the runner's filesystem by setting `UV_CACHE_DIR`.

View File

@ -44,6 +44,12 @@ inputs:
**/*constraints*.in **/*constraints*.in
**/pyproject.toml **/pyproject.toml
**/uv.lock **/uv.lock
restore-cache:
description: "Whether to restore the cache if found."
default: "true"
save-cache:
description: "Whether to save the cache after the run."
default: "true"
cache-suffix: cache-suffix:
description: "Suffix for the cache key" description: "Suffix for the cache key"
required: false required: false

17
dist/save-cache/index.js generated vendored
View File

@ -89809,6 +89809,11 @@ exports.STATE_CACHE_MATCHED_KEY = "cache-matched-key";
const CACHE_VERSION = "1"; const CACHE_VERSION = "1";
async function restoreCache() { async function restoreCache() {
const cacheKey = await computeKeys(); const cacheKey = await computeKeys();
core.saveState(exports.STATE_CACHE_KEY, cacheKey);
if (!inputs_1.restoreCache) {
core.info("restore-cache is false. Skipping restore cache step.");
return;
}
let matchedKey; let matchedKey;
core.info(`Trying to restore uv cache from GitHub Actions cache with key: ${cacheKey}`); core.info(`Trying to restore uv cache from GitHub Actions cache with key: ${cacheKey}`);
try { try {
@ -89820,7 +89825,6 @@ async function restoreCache() {
core.setOutput("cache-hit", false); core.setOutput("cache-hit", false);
return; return;
} }
core.saveState(exports.STATE_CACHE_KEY, cacheKey);
handleMatchResult(matchedKey, cacheKey); handleMatchResult(matchedKey, cacheKey);
} }
async function computeKeys() { async function computeKeys() {
@ -90019,7 +90023,12 @@ const inputs_1 = __nccwpck_require__(9612);
async function run() { async function run() {
try { try {
if (inputs_1.enableCache) { if (inputs_1.enableCache) {
await saveCache(); if (inputs_1.saveCache) {
await saveCache();
}
else {
core.info("save-cache is false. Skipping save cache step.");
}
// node will stay alive if any promises are not resolved, // node will stay alive if any promises are not resolved,
// which is a possibility if HTTP requests are dangling // which is a possibility if HTTP requests are dangling
// due to retries or timeouts. We know that if we got here // due to retries or timeouts. We know that if we got here
@ -90121,7 +90130,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.addProblemMatchers = exports.manifestFile = exports.githubToken = exports.serverUrl = exports.toolDir = exports.toolBinDir = exports.ignoreEmptyWorkdir = exports.ignoreNothingToCache = exports.pruneCache = exports.cacheDependencyGlob = exports.cacheLocalPath = exports.cacheSuffix = exports.enableCache = exports.checkSum = exports.activateEnvironment = exports.pythonVersion = exports.versionFile = exports.version = exports.workingDirectory = void 0; exports.addProblemMatchers = exports.manifestFile = exports.githubToken = exports.serverUrl = exports.toolDir = exports.toolBinDir = exports.ignoreEmptyWorkdir = exports.ignoreNothingToCache = exports.pruneCache = exports.cacheDependencyGlob = exports.cacheLocalPath = exports.cacheSuffix = exports.saveCache = exports.restoreCache = exports.enableCache = exports.checkSum = exports.activateEnvironment = exports.pythonVersion = exports.versionFile = exports.version = exports.workingDirectory = void 0;
const node_path_1 = __importDefault(__nccwpck_require__(6760)); const node_path_1 = __importDefault(__nccwpck_require__(6760));
const core = __importStar(__nccwpck_require__(7484)); const core = __importStar(__nccwpck_require__(7484));
exports.workingDirectory = core.getInput("working-directory"); exports.workingDirectory = core.getInput("working-directory");
@ -90131,6 +90140,8 @@ exports.pythonVersion = core.getInput("python-version");
exports.activateEnvironment = core.getBooleanInput("activate-environment"); exports.activateEnvironment = core.getBooleanInput("activate-environment");
exports.checkSum = core.getInput("checksum"); exports.checkSum = core.getInput("checksum");
exports.enableCache = getEnableCache(); exports.enableCache = getEnableCache();
exports.restoreCache = core.getInput("restore-cache") === "true";
exports.saveCache = core.getInput("save-cache") === "true";
exports.cacheSuffix = core.getInput("cache-suffix") || ""; exports.cacheSuffix = core.getInput("cache-suffix") || "";
exports.cacheLocalPath = getCacheLocalPath(); exports.cacheLocalPath = getCacheLocalPath();
exports.cacheDependencyGlob = getCacheDependencyGlob(); exports.cacheDependencyGlob = getCacheDependencyGlob();

10
dist/setup/index.js generated vendored
View File

@ -124221,6 +124221,11 @@ exports.STATE_CACHE_MATCHED_KEY = "cache-matched-key";
const CACHE_VERSION = "1"; const CACHE_VERSION = "1";
async function restoreCache() { async function restoreCache() {
const cacheKey = await computeKeys(); const cacheKey = await computeKeys();
core.saveState(exports.STATE_CACHE_KEY, cacheKey);
if (!inputs_1.restoreCache) {
core.info("restore-cache is false. Skipping restore cache step.");
return;
}
let matchedKey; let matchedKey;
core.info(`Trying to restore uv cache from GitHub Actions cache with key: ${cacheKey}`); core.info(`Trying to restore uv cache from GitHub Actions cache with key: ${cacheKey}`);
try { try {
@ -124232,7 +124237,6 @@ async function restoreCache() {
core.setOutput("cache-hit", false); core.setOutput("cache-hit", false);
return; return;
} }
core.saveState(exports.STATE_CACHE_KEY, cacheKey);
handleMatchResult(matchedKey, cacheKey); handleMatchResult(matchedKey, cacheKey);
} }
async function computeKeys() { async function computeKeys() {
@ -128634,7 +128638,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.addProblemMatchers = exports.manifestFile = exports.githubToken = exports.serverUrl = exports.toolDir = exports.toolBinDir = exports.ignoreEmptyWorkdir = exports.ignoreNothingToCache = exports.pruneCache = exports.cacheDependencyGlob = exports.cacheLocalPath = exports.cacheSuffix = exports.enableCache = exports.checkSum = exports.activateEnvironment = exports.pythonVersion = exports.versionFile = exports.version = exports.workingDirectory = void 0; exports.addProblemMatchers = exports.manifestFile = exports.githubToken = exports.serverUrl = exports.toolDir = exports.toolBinDir = exports.ignoreEmptyWorkdir = exports.ignoreNothingToCache = exports.pruneCache = exports.cacheDependencyGlob = exports.cacheLocalPath = exports.cacheSuffix = exports.saveCache = exports.restoreCache = exports.enableCache = exports.checkSum = exports.activateEnvironment = exports.pythonVersion = exports.versionFile = exports.version = exports.workingDirectory = void 0;
const node_path_1 = __importDefault(__nccwpck_require__(76760)); const node_path_1 = __importDefault(__nccwpck_require__(76760));
const core = __importStar(__nccwpck_require__(37484)); const core = __importStar(__nccwpck_require__(37484));
exports.workingDirectory = core.getInput("working-directory"); exports.workingDirectory = core.getInput("working-directory");
@ -128644,6 +128648,8 @@ exports.pythonVersion = core.getInput("python-version");
exports.activateEnvironment = core.getBooleanInput("activate-environment"); exports.activateEnvironment = core.getBooleanInput("activate-environment");
exports.checkSum = core.getInput("checksum"); exports.checkSum = core.getInput("checksum");
exports.enableCache = getEnableCache(); exports.enableCache = getEnableCache();
exports.restoreCache = core.getInput("restore-cache") === "true";
exports.saveCache = core.getInput("save-cache") === "true";
exports.cacheSuffix = core.getInput("cache-suffix") || ""; exports.cacheSuffix = core.getInput("cache-suffix") || "";
exports.cacheLocalPath = getCacheLocalPath(); exports.cacheLocalPath = getCacheLocalPath();
exports.cacheDependencyGlob = getCacheDependencyGlob(); exports.cacheDependencyGlob = getCacheDependencyGlob();

View File

@ -8,6 +8,7 @@ import {
cacheSuffix, cacheSuffix,
pruneCache, pruneCache,
pythonVersion as pythonVersionInput, pythonVersion as pythonVersionInput,
restoreCache as shouldRestoreCache,
workingDirectory, workingDirectory,
} from "../utils/inputs"; } from "../utils/inputs";
import { getArch, getPlatform } from "../utils/platforms"; import { getArch, getPlatform } from "../utils/platforms";
@ -18,6 +19,12 @@ const CACHE_VERSION = "1";
export async function restoreCache(): Promise<void> { export async function restoreCache(): Promise<void> {
const cacheKey = await computeKeys(); const cacheKey = await computeKeys();
core.saveState(STATE_CACHE_KEY, cacheKey);
if (!shouldRestoreCache) {
core.info("restore-cache is false. Skipping restore cache step.");
return;
}
let matchedKey: string | undefined; let matchedKey: string | undefined;
core.info( core.info(
@ -32,8 +39,6 @@ export async function restoreCache(): Promise<void> {
return; return;
} }
core.saveState(STATE_CACHE_KEY, cacheKey);
handleMatchResult(matchedKey, cacheKey); handleMatchResult(matchedKey, cacheKey);
} }

View File

@ -11,12 +11,17 @@ import {
enableCache, enableCache,
ignoreNothingToCache, ignoreNothingToCache,
pruneCache as shouldPruneCache, pruneCache as shouldPruneCache,
saveCache as shouldSaveCache,
} from "./utils/inputs"; } from "./utils/inputs";
export async function run(): Promise<void> { export async function run(): Promise<void> {
try { try {
if (enableCache) { if (enableCache) {
await saveCache(); if (shouldSaveCache) {
await saveCache();
} else {
core.info("save-cache is false. Skipping save cache step.");
}
// node will stay alive if any promises are not resolved, // node will stay alive if any promises are not resolved,
// which is a possibility if HTTP requests are dangling // which is a possibility if HTTP requests are dangling
// due to retries or timeouts. We know that if we got here // due to retries or timeouts. We know that if we got here

View File

@ -8,6 +8,8 @@ export const pythonVersion = core.getInput("python-version");
export const activateEnvironment = core.getBooleanInput("activate-environment"); export const activateEnvironment = core.getBooleanInput("activate-environment");
export const checkSum = core.getInput("checksum"); export const checkSum = core.getInput("checksum");
export const enableCache = getEnableCache(); export const enableCache = getEnableCache();
export const restoreCache = core.getInput("restore-cache") === "true";
export const saveCache = core.getInput("save-cache") === "true";
export const cacheSuffix = core.getInput("cache-suffix") || ""; export const cacheSuffix = core.getInput("cache-suffix") || "";
export const cacheLocalPath = getCacheLocalPath(); export const cacheLocalPath = getCacheLocalPath();
export const cacheDependencyGlob = getCacheDependencyGlob(); export const cacheDependencyGlob = getCacheDependencyGlob();