Skip to content

Commit b42be1f

Browse files
shaanmajidj178
andauthored
Add explicit cache input to enable/disable caching (#111)
Should seldom be manually set, but a worthwhile failsafe for debugging or working around broken environments. --------- Co-authored-by: Jo <[email protected]>
1 parent 7774ba8 commit b42be1f

12 files changed

Lines changed: 65 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ For a stable reference, pin to a specific release tag such as `v1.2.3`, or pin t
3737
| --- | --- | --- | --- |
3838
| `extra-args` | Additional arguments appended to `prek run --show-diff-on-failure --color=always` | No | `--all-files` |
3939
| `extra_args` | Deprecated alias for `extra-args` | No | |
40-
| `install-only` | Install `prek` but skip `prek run` | No | `false` |
4140
| `prek-version` | Version or semver range to install, for example `0.2.30`, `0.3.x`, `<=1.0.0`, or `latest` | No | `latest` |
41+
| `install-only` | Install `prek` but skip `prek run` | No | `false` |
4242
| `working-directory` | Directory where `prek run` is executed | No | `.` |
4343
| `show-verbose-logs` | Print the `prek` verbose log after `prek run` completes | No | `true` |
44+
| `cache` | Cache the prek environment between workflow runs | No | `true` |
4445
| `token` | Deprecated and unused; retained for backward compatibility | No | `''` |
4546

4647
## Outputs

‎action.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ inputs:
99
description: Options to pass to `prek run` (deprecated, use extra-args)
1010
deprecationMessage: The extra_args input has been renamed to extra-args. Update your workflow.
1111
required: false
12+
cache:
13+
description: Whether to cache the prek environment between workflow runs
14+
required: false
15+
default: "true"
1216
install-only:
1317
description: Only install prek, do not run it
1418
required: false

‎dist/index.cjs‎

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/post/index.cjs‎

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/cache.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ export async function savePrekCache(): Promise<void> {
5353
const primaryKey = core.getState(CACHE_KEY_STATE)
5454
const matchedKey = core.getState(CACHE_MATCHED_KEY_STATE)
5555
const rawPaths = core.getState(CACHE_PATHS_STATE)
56+
5657
if (!primaryKey || !rawPaths) {
57-
core.info('No cache state found, skipping cache save')
58+
// restorePrekCache() is the only place that records cache state. When
59+
// caching is disabled, the post step lands here and intentionally no-ops.
60+
core.info(
61+
'No cache state found, skipping cache save (cache disabled or restore step did not run)',
62+
)
5863
return
5964
}
6065

‎src/inputs.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function getInputs(): Inputs {
88
const showVerboseLogsInput = core.getInput('show-verbose-logs')
99

1010
return {
11+
cache: core.getBooleanInput('cache'),
1112
extraArgs: legacyExtraArgs || modernExtraArgs,
1213
installOnly: core.getBooleanInput('install-only'),
1314
prekVersion: core.getInput('prek-version') || 'latest',

‎src/main.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ export async function run(): Promise<void> {
1515
core.setOutput('prek-version', normalizeVersion(version))
1616

1717
await installPrek(version)
18-
const { matchedKey, primaryKey } = await restorePrekCache(inputs.workingDirectory)
19-
core.setOutput('cache-hit', String(matchedKey === primaryKey))
18+
19+
if (inputs.cache) {
20+
const { matchedKey, primaryKey } = await restorePrekCache(inputs.workingDirectory)
21+
core.setOutput('cache-hit', String(matchedKey === primaryKey))
22+
} else {
23+
core.info('Caching disabled via cache=false; skipping cache restore')
24+
core.setOutput('cache-hit', 'false')
25+
}
2026

2127
if (inputs.installOnly) {
2228
core.info('Skipping prek run because install-only=true')

‎src/post.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as core from '@actions/core'
22
import { savePrekCache } from './cache'
33

44
async function run(): Promise<void> {
5+
// GitHub always runs the post step; savePrekCache() decides whether the main
6+
// step initialized any cache state worth persisting.
57
await savePrekCache()
68
}
79

‎src/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type Version = string & { readonly __brand: 'Version' }
77
export type NormalizedVersion = `v${string}`
88

99
export type Inputs = {
10+
cache: boolean
1011
extraArgs: string
1112
installOnly: boolean
1213
prekVersion: string

‎test/cache.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ describe('savePrekCache', () => {
157157
await savePrekCache()
158158

159159
expect(toolkitMocks.saveCache).not.toHaveBeenCalled()
160-
expect(mockContext.telemetry.infos).toEqual(['No cache state found, skipping cache save'])
160+
expect(mockContext.telemetry.infos).toEqual([
161+
'No cache state found, skipping cache save (cache disabled or restore step did not run)',
162+
])
161163
expect(mockContext.telemetry.warnings).toEqual([])
162164
})
163165

0 commit comments

Comments
 (0)