feat(action): moved save cache to post step (#321)

Fixes #199

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Amadeusz Kryze 2025-11-20 22:52:34 +01:00 committed by GitHub
parent 07cd07257f
commit 79b896a39d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 140 additions and 26 deletions

View file

@ -8,6 +8,7 @@ import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as Handlebars from 'handlebars'
import * as stateHelper from './state-helper'
// Configuration file patterns for cache key generation
const MISE_CONFIG_FILE_PATTERNS = [
@ -47,9 +48,8 @@ async function run(): Promise<void> {
await setToolVersions()
await setMiseToml()
let cacheKey: string | undefined
if (core.getBooleanInput('cache')) {
cacheKey = await restoreMiseCache()
await restoreMiseCache()
} else {
core.setOutput('cache-hit', false)
}
@ -64,9 +64,7 @@ async function run(): Promise<void> {
await testMise()
if (core.getBooleanInput('install')) {
await miseInstall()
if (cacheKey && core.getBooleanInput('cache_save')) {
await saveCache(cacheKey)
}
// Save cache move to post()
}
await miseLs()
const loadEnv = core.getBooleanInput('env')
@ -79,6 +77,26 @@ async function run(): Promise<void> {
}
}
export async function cleanup(): Promise<void> {
try {
const primaryKey = await getCacheKey()
if (primaryKey && core.getBooleanInput('cache_save')) {
await saveCache(primaryKey)
}
} catch (err) {
if (err instanceof Error) core.setFailed(err.message)
else throw err
}
}
async function getCacheKey(): Promise<string> {
// Use custom cache key if provided, otherwise use default template
const cacheKeyTemplate =
core.getInput('cache_key') || DEFAULT_CACHE_KEY_TEMPLATE
return await processCacheKeyTemplate(cacheKeyTemplate)
}
async function exportMiseEnv(): Promise<void> {
core.startGroup('Exporting mise environment variables')
@ -192,14 +210,11 @@ async function setEnvVars(): Promise<void> {
core.addPath(shimsDir)
}
async function restoreMiseCache(): Promise<string | undefined> {
async function restoreMiseCache(): Promise<void> {
core.startGroup('Restoring mise cache')
const cachePath = miseDir()
// Use custom cache key if provided, otherwise use default template
const cacheKeyTemplate =
core.getInput('cache_key') || DEFAULT_CACHE_KEY_TEMPLATE
const primaryKey = await processCacheKeyTemplate(cacheKeyTemplate)
const primaryKey = await getCacheKey()
core.saveState('PRIMARY_KEY', primaryKey)
core.saveState('MISE_DIR', cachePath)
@ -209,7 +224,7 @@ async function restoreMiseCache(): Promise<string | undefined> {
if (!cacheKey) {
core.info(`mise cache not found for ${primaryKey}`)
return primaryKey
return
}
core.info(`mise cache restored from key: ${cacheKey}`)
@ -369,8 +384,6 @@ const writeFile = async (p: fs.PathLike, body: string): Promise<void> =>
await fs.promises.writeFile(p, body, { encoding: 'utf8' })
})
run()
function miseDir(): string {
const dir = core.getState('MISE_DIR')
if (dir) return dir
@ -475,3 +488,12 @@ async function isMusl() {
})
return stderr.indexOf('musl') > -1
}
// Main
if (!stateHelper.IsPost) {
run()
}
// Post
else {
cleanup()
}

13
src/state-helper.ts Normal file
View file

@ -0,0 +1,13 @@
// https://github.com/actions/checkout/blob/v4/src/state-helper.ts
import * as core from '@actions/core'
/**
* Indicates whether the POST action is running
*/
export const IsPost = !!core.getState('isPost')
// Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic.
// This is necessary since we don't have a separate entry point.
if (!IsPost) {
core.saveState('isPost', 'true')
}