mirror of
https://github.com/jdx/mise-action.git
synced 2026-05-14 05:50:31 +00:00
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:
parent
07cd07257f
commit
79b896a39d
5 changed files with 140 additions and 26 deletions
|
|
@ -49,8 +49,8 @@ inputs:
|
|||
required: false
|
||||
description: |
|
||||
Override the complete cache key (ignores all other cache key options).
|
||||
Supports template variables: {{version}}, {{cache_key_prefix}}, {{platform}}, {{file_hash}},
|
||||
{{mise_env}}, {{install_args_hash}}, {{default}}, {{env.VAR_NAME}} for environment variables,
|
||||
Supports template variables: {{version}}, {{cache_key_prefix}}, {{platform}}, {{file_hash}},
|
||||
{{mise_env}}, {{install_args_hash}}, {{default}}, {{env.VAR_NAME}} for environment variables,
|
||||
and conditional logic like {{#if version}}...{{/if}}
|
||||
experimental:
|
||||
required: false
|
||||
|
|
@ -87,3 +87,4 @@ outputs:
|
|||
runs:
|
||||
using: node20
|
||||
main: dist/index.js
|
||||
post: dist/index.js
|
||||
|
|
|
|||
98
dist/index.js
generated
vendored
98
dist/index.js
generated
vendored
|
|
@ -49986,6 +49986,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.cleanup = cleanup;
|
||||
const cache = __importStar(__nccwpck_require__(5116));
|
||||
const io = __importStar(__nccwpck_require__(94994));
|
||||
const core = __importStar(__nccwpck_require__(37484));
|
||||
|
|
@ -49996,6 +49997,7 @@ const fs = __importStar(__nccwpck_require__(79896));
|
|||
const os = __importStar(__nccwpck_require__(70857));
|
||||
const path = __importStar(__nccwpck_require__(16928));
|
||||
const Handlebars = __importStar(__nccwpck_require__(88508));
|
||||
const stateHelper = __importStar(__nccwpck_require__(87155));
|
||||
// Configuration file patterns for cache key generation
|
||||
const MISE_CONFIG_FILE_PATTERNS = [
|
||||
`**/.config/mise/config.toml`,
|
||||
|
|
@ -50030,9 +50032,8 @@ async function run() {
|
|||
try {
|
||||
await setToolVersions();
|
||||
await setMiseToml();
|
||||
let cacheKey;
|
||||
if (core.getBooleanInput('cache')) {
|
||||
cacheKey = await restoreMiseCache();
|
||||
await restoreMiseCache();
|
||||
}
|
||||
else {
|
||||
core.setOutput('cache-hit', false);
|
||||
|
|
@ -50047,9 +50048,7 @@ async function run() {
|
|||
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');
|
||||
|
|
@ -50064,6 +50063,25 @@ async function run() {
|
|||
throw err;
|
||||
}
|
||||
}
|
||||
async function cleanup() {
|
||||
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() {
|
||||
// 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() {
|
||||
core.startGroup('Exporting mise environment variables');
|
||||
// Check if mise supports --redacted flags based on version input
|
||||
|
|
@ -50162,16 +50180,14 @@ async function setEnvVars() {
|
|||
async function restoreMiseCache() {
|
||||
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);
|
||||
const cacheKey = await cache.restoreCache([cachePath], primaryKey);
|
||||
core.setOutput('cache-hit', Boolean(cacheKey));
|
||||
if (!cacheKey) {
|
||||
core.info(`mise cache not found for ${primaryKey}`);
|
||||
return primaryKey;
|
||||
return;
|
||||
}
|
||||
core.info(`mise cache restored from key: ${cacheKey}`);
|
||||
}
|
||||
|
|
@ -50306,7 +50322,6 @@ const writeFile = async (p, body) => await core.group(`Writing ${p}`, async () =
|
|||
core.info(`Body:\n${body}`);
|
||||
await fs.promises.writeFile(p, body, { encoding: 'utf8' });
|
||||
});
|
||||
run();
|
||||
function miseDir() {
|
||||
const dir = core.getState('MISE_DIR');
|
||||
if (dir)
|
||||
|
|
@ -50399,6 +50414,69 @@ async function isMusl() {
|
|||
});
|
||||
return stderr.indexOf('musl') > -1;
|
||||
}
|
||||
// Main
|
||||
if (!stateHelper.IsPost) {
|
||||
run();
|
||||
}
|
||||
// Post
|
||||
else {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 87155:
|
||||
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || (function () {
|
||||
var ownKeys = function(o) {
|
||||
ownKeys = Object.getOwnPropertyNames || function (o) {
|
||||
var ar = [];
|
||||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
||||
return ar;
|
||||
};
|
||||
return ownKeys(o);
|
||||
};
|
||||
return function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.IsPost = void 0;
|
||||
// https://github.com/actions/checkout/blob/v4/src/state-helper.ts
|
||||
const core = __importStar(__nccwpck_require__(37484));
|
||||
/**
|
||||
* Indicates whether the POST action is running
|
||||
*/
|
||||
exports.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 (!exports.IsPost) {
|
||||
core.saveState('isPost', 'true');
|
||||
}
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
|
|
|||
2
dist/index.js.map
generated
vendored
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
48
src/index.ts
48
src/index.ts
|
|
@ -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
13
src/state-helper.ts
Normal 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')
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue