fix: save cache when build fails

Fixes #99
This commit is contained in:
jdx 2025-04-22 22:41:39 -05:00
parent 83232dd570
commit 81d53cf968
No known key found for this signature in database
GPG key ID: 584DADE86724B407
10 changed files with 505 additions and 77241 deletions

View file

@ -59,5 +59,3 @@ outputs:
runs:
using: node20
main: dist/index.js
post: dist/cache-save/index.js
post-if: success()

76667
dist/cache-save/index.js generated vendored

File diff suppressed because one or more lines are too long

1
dist/cache-save/index.js.map generated vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

976
dist/index.js generated vendored

File diff suppressed because it is too large Load diff

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -22,7 +22,7 @@
"format:check": "prettier --check **/*.ts",
"format:write": "prettier --write **/*.ts",
"lint": "npx eslint . && npm run format:check",
"package": "ncc build -s src/index.ts --license licenses.txt && ncc -s build src/cache-save.ts -o dist/cache-save/",
"package": "ncc build -s src/index.ts --license licenses.txt",
"package:watch": "npm run package -- --watch",
"version": "./scripts/version.sh",
"postversion": "./scripts/postversion.sh",

View file

@ -1,40 +0,0 @@
import * as cache from '@actions/cache'
import * as core from '@actions/core'
import * as fs from 'fs'
import { miseDir } from './utils'
export async function run(): Promise<void> {
try {
await cacheMiseTools()
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
else throw error
}
}
async function cacheMiseTools(): Promise<void> {
if (core.getState('CACHE') !== 'true') {
core.info('Skipping saving cache')
return
}
const state = core.getState('CACHE_KEY')
const primaryKey = core.getState('PRIMARY_KEY')
const cachePath = miseDir()
if (!fs.existsSync(cachePath)) {
throw new Error(`Cache folder path does not exist on disk: ${cachePath}`)
}
if (primaryKey === state) {
core.info(`Cache hit occurred on key ${primaryKey}, not saving cache.`)
return
}
const cacheId = await cache.saveCache([cachePath], primaryKey)
if (cacheId === -1) return
core.info(`Cache saved from ${cachePath} with key: ${primaryKey}`)
}
run()

View file

@ -7,15 +7,15 @@ import * as crypto from 'crypto'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import { miseDir } from './utils'
async function run(): Promise<void> {
try {
await setToolVersions()
await setMiseToml()
let cacheKey: string | undefined
if (core.getBooleanInput('cache')) {
await restoreMiseCache()
cacheKey = await restoreMiseCache()
} else {
core.setOutput('cache-hit', false)
}
@ -26,6 +26,9 @@ async function run(): Promise<void> {
await testMise()
if (core.getBooleanInput('install')) {
await miseInstall()
if (cacheKey && core.getBooleanInput('cache_save')) {
await saveCache(cacheKey)
}
}
await miseLs()
} catch (err) {
@ -55,7 +58,7 @@ async function setEnvVars(): Promise<void> {
core.addPath(shimsDir)
}
async function restoreMiseCache(): Promise<void> {
async function restoreMiseCache(): Promise<string | undefined> {
core.startGroup('Restoring mise cache')
const version = core.getInput('version')
const installArgs = core.getInput('install_args')
@ -106,7 +109,6 @@ async function restoreMiseCache(): Promise<void> {
}
}
core.saveState('CACHE', core.getBooleanInput('cache_save'))
core.saveState('PRIMARY_KEY', primaryKey)
core.saveState('MISE_DIR', cachePath)
@ -115,10 +117,9 @@ async function restoreMiseCache(): Promise<void> {
if (!cacheKey) {
core.info(`mise cache not found for ${primaryKey}`)
return
return primaryKey
}
core.saveState('CACHE_KEY', cacheKey)
core.info(`mise cache restored from key: ${cacheKey}`)
}
@ -242,3 +243,29 @@ const writeFile = async (p: fs.PathLike, body: string): Promise<void> =>
})
run()
function miseDir(): string {
const dir = core.getState('MISE_DIR')
if (dir) return dir
const { MISE_DATA_DIR, XDG_DATA_HOME, LOCALAPPDATA } = process.env
if (MISE_DATA_DIR) return MISE_DATA_DIR
if (XDG_DATA_HOME) return path.join(XDG_DATA_HOME, 'mise')
if (process.platform === 'win32' && LOCALAPPDATA)
return path.join(LOCALAPPDATA, 'mise')
return path.join(os.homedir(), '.local', 'share', 'mise')
}
async function saveCache(cacheKey: string): Promise<void> {
const cachePath = miseDir()
if (!fs.existsSync(cachePath)) {
throw new Error(`Cache folder path does not exist on disk: ${cachePath}`)
}
const cacheId = await cache.saveCache([cachePath], cacheKey)
if (cacheId === -1) return
core.info(`Cache saved from ${cachePath} with key: ${cacheKey}`)
}

View file

@ -1,16 +0,0 @@
import * as core from '@actions/core'
import * as os from 'os'
import * as path from 'path'
export function miseDir(): string {
const dir = core.getState('MISE_DIR')
if (dir) return dir
const { MISE_DATA_DIR, XDG_DATA_HOME, LOCALAPPDATA } = process.env
if (MISE_DATA_DIR) return MISE_DATA_DIR
if (XDG_DATA_HOME) return path.join(XDG_DATA_HOME, 'mise')
if (process.platform === 'win32' && LOCALAPPDATA)
return path.join(LOCALAPPDATA, 'mise')
return path.join(os.homedir(), '.local', 'share', 'mise')
}