Improve restoreInputs

This commit is contained in:
Michael B. Gale
2026-10-19 22:18:06 +01:00
parent 5b8ae33509
commit 64ec5d5f67
3 changed files with 26 additions and 12 deletions

17
lib/entry-points.js generated
View File

@@ -170390,17 +170390,22 @@ var persistInputs = function(env = getEnv()) {
);
core4.saveState(persistedInputsKey, JSON.stringify(inputEnvironmentVariables));
};
function restoreInputs(logger2) {
function restoreInputs(action) {
try {
const persistedInputsValue = core4.getState(persistedInputsKey);
const persistedInputsValue = action.actions.getState(persistedInputsKey);
if (persistedInputsValue) {
const persistedInputs = JSON.parse(persistedInputsValue);
const persistedInputs = JSON.parse(
persistedInputsValue
);
if (!Array.isArray(persistedInputs)) {
throw new Error("Restored value is not an array.");
}
for (const [name, value] of persistedInputs) {
process.env[name] = value;
action.env.set(name, value);
}
}
} catch (err) {
logger2.error(`Unable to restore inputs: ${getErrorMessage(err)}`);
action.logger.error(`Unable to restore inputs: ${getErrorMessage(err)}`);
throw new Error(
"Failed to restore inputs from the state set by this action's main execution."
);
@@ -171783,7 +171788,7 @@ async function runInActions(action) {
actions: actionsEnv
};
if (isPostAction(action.name)) {
restoreInputs(logger2);
restoreInputs(actionState);
}
getJobUUID(actionState);
await action.run(actionState);

View File

@@ -183,7 +183,7 @@ export async function runInActions(action: Action) {
// If this is a post-action, restore inputs from the main action.
if (isPostAction(action.name)) {
restoreInputs(logger);
restoreInputs(actionState);
}
// Create a unique identifier for this run.

View File

@@ -6,6 +6,7 @@ import * as toolrunner from "@actions/exec/lib/toolrunner";
import * as github from "@actions/github";
import * as io from "@actions/io";
import type { ActionState } from "./action-common";
import { hasConfigKey, type Config } from "./config/action-config";
import { Env, EnvVar, ActionsEnvVars, ReadOnlyEnv } from "./environment";
import { Logger } from "./logging";
@@ -426,18 +427,26 @@ export const persistInputs = function (env: Env = getEnv()) {
/**
* Restores all inputs to the action from the persisted state.
*/
export function restoreInputs(logger: Logger) {
export function restoreInputs(
action: ActionState<["Env", "Logger", "Actions"]>,
) {
try {
const persistedInputsValue = core.getState(persistedInputsKey);
const persistedInputsValue = action.actions.getState(persistedInputsKey);
if (persistedInputsValue) {
const persistedInputs = JSON.parse(persistedInputsValue);
const persistedInputs = JSON.parse(
persistedInputsValue,
) as PersistedInputs;
if (!Array.isArray(persistedInputs)) {
throw new Error("Restored value is not an array.");
}
for (const [name, value] of persistedInputs) {
process.env[name] = value;
action.env.set(name, value);
}
}
} catch (err) {
logger.error(`Unable to restore inputs: ${getErrorMessage(err)}`);
action.logger.error(`Unable to restore inputs: ${getErrorMessage(err)}`);
throw new Error(
"Failed to restore inputs from the state set by this action's main execution.",
);