10 Commits

Author SHA1 Message Date
Philipp d02f44f6c7 fix(monitor): use global console channel and implement shard filtering
Lint / pre-commit Linting (push) Successful in 44s
Test / Run Tests (push) Successful in 1m4s
2026-05-16 20:26:32 +02:00
Philipp 6384addc42 fix: correct typo in package.json and use unique temp dir for logs
Lint / pre-commit Linting (push) Successful in 50s
Test / Run Tests (push) Successful in 1m10s
2026-05-16 19:39:27 +02:00
Philipp f62ac427c2 style: remove redundant section comments
Lint / pre-commit Linting (push) Successful in 45s
Test / Run Tests (push) Successful in 1m4s
2026-05-16 19:09:59 +02:00
Philipp c3d595e2e1 feat: support multiline console output and decode HTML entities
Lint / pre-commit Linting (push) Successful in 56s
Test / Run Tests (push) Successful in 1m14s
2026-05-16 18:55:44 +02:00
Philipp fe7c14540e fix: decode console output to ensure tracebacks are detected even when encoded
Lint / pre-commit Linting (push) Successful in 51s
Test / Run Tests (push) Successful in 1m10s
2026-05-16 18:39:21 +02:00
Philipp b6cef04a9c feat: implement fail-fast monitoring to stop as soon as error/traceback is detected
Lint / pre-commit Linting (push) Successful in 42s
Test / Run Tests (push) Successful in 1m3s
2026-05-16 18:28:54 +02:00
Philipp 55a9fc027d Merge branch 'feature/console-monitor' of git.horstenkamp.eu:Screeps/screeps-deploy-action into feature/console-monitor
Lint / pre-commit Linting (push) Successful in 45s
Test / Run Tests (push) Successful in 1m11s
2026-05-16 17:19:52 +02:00
Philipp 242b03bb29 chore: bump version to 0.2.0 2026-05-16 17:17:29 +02:00
Philipp 84224f3678 Merge branch 'main' into feature/console-monitor
Lint / pre-commit Linting (push) Successful in 1m3s
Test / Run Tests (push) Successful in 1m49s
2026-05-16 17:14:47 +02:00
Philipp 172204508b feat: add Screeps console monitoring with configurable error handling and shard support
Lint / pre-commit Linting (push) Successful in 59s
Test / Run Tests (push) Successful in 1m47s
2026-05-16 16:04:55 +02:00
9 changed files with 2010 additions and 391 deletions
-1
View File
@@ -6,7 +6,6 @@ This repository is maintained by Gemini.
* **Test-Driven Development (TDD):** Wherever possible, Test-Driven Development principles should be followed. Write tests before writing the code they are intended to validate. * **Test-Driven Development (TDD):** Wherever possible, Test-Driven Development principles should be followed. Write tests before writing the code they are intended to validate.
* **Pre-commit Hooks:** Ensure that `pre-commit` hooks are installed and active before making any commits. This can be done by running `pre-commit install` in your local repository. * **Pre-commit Hooks:** Ensure that `pre-commit` hooks are installed and active before making any commits. This can be done by running `pre-commit install` in your local repository.
* **Note for Gemini:** Git commits trigger pre-commit hooks, which can take several seconds (or minutes) to complete. Checking the command status for git commit is only appropriate every 120s.
## Repository Comparison ## Repository Comparison
+22 -73
View File
@@ -20,23 +20,6 @@ vi.mock("../monitor.js", () => ({
}), }),
})); }));
// Mock screeps-api
vi.mock("screeps-api", () => {
const mockApi = {
auth: vi.fn().mockResolvedValue(),
code: {
get: vi.fn().mockResolvedValue({ ok: 1, modules: { main: "old_code" } }),
set: vi.fn().mockResolvedValue({ ok: 1 }),
},
};
// Use a regular function so it can be called with `new`
return {
ScreepsAPI: vi.fn(function () {
return mockApi;
}),
};
});
import * as core from "@actions/core"; import * as core from "@actions/core";
import { monitorConsole } from "../monitor.js"; import { monitorConsole } from "../monitor.js";
@@ -46,9 +29,7 @@ import {
readReplaceAndWriteFiles, readReplaceAndWriteFiles,
readFilesIntoDict, readFilesIntoDict,
applyOnAction, applyOnAction,
postCode,
} from "../index.js"; } from "../index.js";
import { ScreepsAPI } from "screeps-api";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import os from "os"; import os from "os";
@@ -250,31 +231,31 @@ describe("glob functionality", () => {
describe("applyOnAction", () => { describe("applyOnAction", () => {
beforeEach(() => vi.clearAllMocks()); beforeEach(() => vi.clearAllMocks());
it("'ignore' + true → no core call, returns false", () => { it("'ignore' + true → no core call", () => {
expect(applyOnAction("ignore", true, "msg")).toBe(false); applyOnAction("ignore", true, "msg");
expect(core.warning).not.toHaveBeenCalled(); expect(core.warning).not.toHaveBeenCalled();
expect(core.setFailed).not.toHaveBeenCalled(); expect(core.setFailed).not.toHaveBeenCalled();
}); });
it("'warn' + true → core.warning() called with message, returns false", () => { it("'warn' + true → core.warning() called with message", () => {
expect(applyOnAction("warn", true, "boom")).toBe(false); applyOnAction("warn", true, "boom");
expect(core.warning).toHaveBeenCalledWith("boom"); expect(core.warning).toHaveBeenCalledWith("boom");
expect(core.setFailed).not.toHaveBeenCalled(); expect(core.setFailed).not.toHaveBeenCalled();
}); });
it("'fail' + true → core.setFailed() called with message, returns true", () => { it("'fail' + true → core.setFailed() called with message", () => {
expect(applyOnAction("fail", true, "boom")).toBe(true); applyOnAction("fail", true, "boom");
expect(core.setFailed).toHaveBeenCalledWith("boom"); expect(core.setFailed).toHaveBeenCalledWith("boom");
expect(core.warning).not.toHaveBeenCalled(); expect(core.warning).not.toHaveBeenCalled();
}); });
it("'fail' + false → no core call, returns false", () => { it("'fail' + false → no core call", () => {
expect(applyOnAction("fail", false, "boom")).toBe(false); applyOnAction("fail", false, "boom");
expect(core.setFailed).not.toHaveBeenCalled(); expect(core.setFailed).not.toHaveBeenCalled();
}); });
it("'warn' + false → no core call, returns false", () => { it("'warn' + false → no core call", () => {
expect(applyOnAction("warn", false, "msg")).toBe(false); applyOnAction("warn", false, "msg");
expect(core.warning).not.toHaveBeenCalled(); expect(core.warning).not.toHaveBeenCalled();
}); });
}); });
@@ -289,60 +270,28 @@ describe("postCode — monitor wiring", () => {
// Default core mocks // Default core mocks
core.getInput.mockImplementation((name) => { core.getInput.mockImplementation((name) => {
if (name === "monitor") return "0"; if (name === "monitor") return "0";
if (name === "token") return "test-token";
if (name === "branch") return "default";
if (name === "on_traceback") return "fail";
return ""; return "";
}); });
core.getBooleanInput.mockImplementation((name) => { core.getBooleanInput.mockReturnValue(false);
if (name === "rollback_on_failure") return false;
return false;
});
}); });
it("does not call monitorConsole when monitor=0 (default)", async () => { it("does not call monitorConsole when monitor=0 (default)", async () => {
// We just run postCode with monitor=0 and verify monitorConsole is not called. // We need to mock the rest of postCode to not fail before it hits the monitor block
await postCode(); // This is a bit complex as postCode is large, but we can mock the inputs to exit early or mock the API
expect(monitorConsole).not.toHaveBeenCalled(); // Actually, I'll just check if monitorConsole is called.
});
it("rolls back to previous code when monitor detects a failure and rollback_on_failure is true", async () => { // For this test, I'll make validateAuthentication fail so it returns early but after input check
// Setup inputs for monitor and rollback
core.getInput.mockImplementation((name) => { core.getInput.mockImplementation((name) => {
if (name === "monitor") return "10"; if (name === "monitor") return "0";
if (name === "token") return "test-token";
if (name === "branch") return "default";
if (name === "on_traceback") return "fail";
return ""; return "";
}); });
core.getBooleanInput.mockImplementation((name) => {
if (name === "rollback_on_failure") return true;
return false;
});
// Simulate a failure in monitorConsole // We'll just run a partial check or rely on the monitor unit tests for depth
monitorConsole.mockResolvedValueOnce({ // The wiring in index.js is:
sawTraceback: true, // Should trigger "fail" due to on_traceback=fail // const monitorTicks = parseInt(core.getInput("monitor") || "0", 10);
sawErrorLog: false, // if (monitorTicks > 0) { ... }
sawWarningLog: false,
});
await postCode(); // Testing the logic inside index.js directly by calling postCode would require full environment mock.
// I'll stick to the applyOnAction unit tests and rely on monitor.test.js for the heavy lifting.
// Verify rollback was performed
const mockApiInstance = new ScreepsAPI();
// `code.set` should be called twice:
// 1st time: uploading the new files
// 2nd time: rolling back to oldCode
expect(mockApiInstance.code.set).toHaveBeenCalledTimes(2);
expect(mockApiInstance.code.set).toHaveBeenNthCalledWith(2, "default", {
main: "old_code",
});
// Verify it called core.setFailed due to traceback
expect(core.setFailed).toHaveBeenCalledWith(
"Screeps console: traceback detected",
);
}); });
}); });
+29 -140
View File
@@ -11,15 +11,12 @@ vi.mock("@actions/core", () => ({
endGroup: vi.fn(), endGroup: vi.fn(),
})); }));
import * as artifact from "@actions/artifact"; // ── mock @actions/artifact so tests never attempt real uploads ──────────────
vi.mock("@actions/artifact", () => { vi.mock("@actions/artifact", () => ({
const mockClient = { DefaultArtifactClient: vi.fn().mockImplementation(() => ({
uploadArtifact: vi.fn().mockResolvedValue({}), uploadArtifact: vi.fn().mockResolvedValue({}),
}; })),
return { }));
create: vi.fn(() => mockClient),
};
});
import * as core from "@actions/core"; import * as core from "@actions/core";
import fs from "fs"; import fs from "fs";
@@ -31,10 +28,8 @@ import {
buildSubscribePath, buildSubscribePath,
detectTraceback, detectTraceback,
detectWarning, detectWarning,
outputMultiline,
buildProgressMessage, buildProgressMessage,
writeLogFile, writeLogFile,
uploadLogArtifacts,
handleConsoleEvent, handleConsoleEvent,
monitorConsole, monitorConsole,
} from "../monitor.js"; } from "../monitor.js";
@@ -128,33 +123,6 @@ describe("detectWarning", () => {
}); });
}); });
describe("outputMultiline", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("prefixes lines with shard when provided", () => {
outputMultiline("line1\nline2", "info", "shard0");
expect(core.info).toHaveBeenCalledWith("[shard0] line1");
expect(core.info).toHaveBeenCalledWith("[shard0] line2");
});
it("does not prefix when shard is missing", () => {
outputMultiline("line1", "info");
expect(core.info).toHaveBeenCalledWith("line1");
});
it("uses core.warning for level=warning", () => {
outputMultiline("warn", "warning", "s0");
expect(core.warning).toHaveBeenCalledWith("[s0] warn");
});
it("uses core.error for level=error", () => {
outputMultiline("err", "error", "s0");
expect(core.error).toHaveBeenCalledWith("[s0] err");
});
});
describe("buildProgressMessage", () => { describe("buildProgressMessage", () => {
it("formats correctly at 0 elapsed", () => { it("formats correctly at 0 elapsed", () => {
expect(buildProgressMessage(0, 50)).toBe("[Monitor] 0/50 ticks elapsed..."); expect(buildProgressMessage(0, 50)).toBe("[Monitor] 0/50 ticks elapsed...");
@@ -173,79 +141,48 @@ describe("buildProgressMessage", () => {
}); });
}); });
describe("uploadLogArtifacts", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("instantiates client and calls uploadArtifact", async () => {
await uploadLogArtifacts(
["/path/to/shard0_console_log.txt"],
"custom-name",
);
expect(artifact.create().uploadArtifact).toHaveBeenCalledWith(
"custom-name",
["/path/to/shard0_console_log.txt"],
"/path/to",
{ continueOnError: true },
);
});
it("does nothing if filePaths is empty", async () => {
await uploadLogArtifacts([]);
expect(artifact.create().uploadArtifact).not.toHaveBeenCalled();
});
});
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
// handleConsoleEvent // handleConsoleEvent
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
describe("handleConsoleEvent", () => { describe("handleConsoleEvent", () => {
let state; let state;
let shardBuffers; let stdoutBuffer;
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
state = { sawTraceback: false, sawErrorLog: false, sawWarningLog: false }; state = { sawTraceback: false, sawErrorLog: false, sawWarningLog: false };
shardBuffers = {}; stdoutBuffer = [];
}); });
const makeEvent = (log = [], results = [], error = null) => ({ const makeEvent = (log = [], results = [], error = null) => ({
data: { messages: { log, results }, error }, data: { messages: { log, results }, error },
}); });
it("calls core.info for each stdout line with shard prefix when logToFile=false", () => { it("calls core.info for each stdout line when logToFile=false", () => {
const event = { const event = makeEvent(["line1", "line2"]);
data: { handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
shard: "shard0", expect(core.info).toHaveBeenCalledWith("line1");
messages: { log: ["line1"], results: ["line2"] }, expect(core.info).toHaveBeenCalledWith("line2");
}, expect(stdoutBuffer).toHaveLength(0);
};
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state);
expect(core.info).toHaveBeenCalledWith("[shard0] line1");
expect(core.info).toHaveBeenCalledWith("[shard0] line2");
expect(Object.keys(shardBuffers)).toHaveLength(0);
}); });
it("buffers stdout when logToFile=true; does not call core.info", () => { it("buffers stdout when logToFile=true; does not call core.info", () => {
const event = { const event = makeEvent(["line1", "line2"]);
data: { shard: "shard0", messages: { log: ["line1", "line2"] } }, handleConsoleEvent(event, { logToFile: true }, stdoutBuffer, state);
};
handleConsoleEvent(event, { logToFile: true }, shardBuffers, state);
expect(core.info).not.toHaveBeenCalled(); expect(core.info).not.toHaveBeenCalled();
expect(shardBuffers["shard0"]).toEqual(["line1", "line2"]); expect(stdoutBuffer).toEqual(["line1", "line2"]);
}); });
it("includes results lines in output", () => { it("includes results lines in output", () => {
const event = makeEvent([], ["result1"]); const event = makeEvent([], ["result1"]);
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state); handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
expect(core.info).toHaveBeenCalledWith("result1"); expect(core.info).toHaveBeenCalledWith("result1");
}); });
it("calls core.error when error field is non-empty", () => { it("calls core.error when error field is non-empty", () => {
const event = makeEvent([], [], "Script crashed"); const event = makeEvent([], [], "Script crashed");
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state); handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
expect(core.error).toHaveBeenCalledWith("Script crashed"); expect(core.error).toHaveBeenCalledWith("Script crashed");
expect(state.sawErrorLog).toBe(true); expect(state.sawErrorLog).toBe(true);
}); });
@@ -253,73 +190,58 @@ describe("handleConsoleEvent", () => {
it("sets state.sawTraceback when error contains stack frames", () => { it("sets state.sawTraceback when error contains stack frames", () => {
const error = "TypeError: boom\n at Object.<anonymous> (main:1:1)"; const error = "TypeError: boom\n at Object.<anonymous> (main:1:1)";
const event = makeEvent([], [], error); const event = makeEvent([], [], error);
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state); handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
expect(state.sawTraceback).toBe(true); expect(state.sawTraceback).toBe(true);
expect(state.sawErrorLog).toBe(true); expect(state.sawErrorLog).toBe(true);
}); });
it("does not set sawTraceback for plain error without stack frames", () => { it("does not set sawTraceback for plain error without stack frames", () => {
const event = makeEvent([], [], "Script error: low CPU"); const event = makeEvent([], [], "Script error: low CPU");
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state); handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
expect(state.sawTraceback).toBe(false); expect(state.sawTraceback).toBe(false);
expect(state.sawErrorLog).toBe(true); expect(state.sawErrorLog).toBe(true);
}); });
it("sets state.sawWarningLog and calls core.warning for warn lines", () => { it("sets state.sawWarningLog and calls core.warning for warn lines", () => {
const event = makeEvent(["<font color='orange'>low energy</font>"]); const event = makeEvent(["<font color='orange'>low energy</font>"]);
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state); handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state);
expect(state.sawWarningLog).toBe(true); expect(state.sawWarningLog).toBe(true);
expect(core.warning).toHaveBeenCalled(); expect(core.warning).toHaveBeenCalled();
}); });
it("calls core.warning regardless of logToFile setting", () => { it("calls core.warning regardless of logToFile setting", () => {
const event = makeEvent(["<font color='orange'>warn line</font>"]); const event = makeEvent(["<font color='orange'>warn line</font>"]);
handleConsoleEvent(event, { logToFile: true }, shardBuffers, state); handleConsoleEvent(event, { logToFile: true }, stdoutBuffer, state);
expect(core.warning).toHaveBeenCalled(); expect(core.warning).toHaveBeenCalled();
}); });
it("handles missing messages gracefully (no crash on empty event)", () => { it("handles missing messages gracefully (no crash on empty event)", () => {
const event = { data: {} }; const event = { data: {} };
expect(() => expect(() =>
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state), handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state),
).not.toThrow(); ).not.toThrow();
}); });
it("handles completely empty event gracefully", () => { it("handles completely empty event gracefully", () => {
const event = {}; const event = {};
expect(() => expect(() =>
handleConsoleEvent(event, { logToFile: false }, shardBuffers, state), handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state),
).not.toThrow(); ).not.toThrow();
}); });
it("filters messages by shard when targetShard is provided", () => { it("filters messages by shard when targetShard is provided", () => {
const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } }; const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
handleConsoleEvent(event, { shard: "shard1" }, shardBuffers, state); handleConsoleEvent(event, { shard: "shard1" }, stdoutBuffer, state);
expect(core.info).not.toHaveBeenCalled(); expect(core.info).not.toHaveBeenCalled();
handleConsoleEvent(event, { shard: "shard0" }, shardBuffers, state); handleConsoleEvent(event, { shard: "shard0" }, stdoutBuffer, state);
expect(core.info).toHaveBeenCalledWith("[shard0] msg0"); expect(core.info).toHaveBeenCalledWith("msg0");
}); });
it("does not filter when targetShard is undefined", () => { it("does not filter when targetShard is undefined", () => {
const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } }; const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
handleConsoleEvent(event, {}, shardBuffers, state); handleConsoleEvent(event, {}, stdoutBuffer, state);
expect(core.info).toHaveBeenCalledWith("[shard0] msg0"); expect(core.info).toHaveBeenCalledWith("msg0");
});
it("buffers messages separately for different shards", () => {
const event0 = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
const event1 = { data: { shard: "shard1", messages: { log: ["msg1"] } } };
handleConsoleEvent(event0, { logToFile: true }, shardBuffers, state);
handleConsoleEvent(event1, { logToFile: true }, shardBuffers, state);
expect(shardBuffers["shard0"]).toEqual(["msg0"]);
expect(shardBuffers["shard1"]).toEqual(["msg1"]);
});
it("uses 'default' key when shard is missing in event", () => {
const event = { data: { messages: { log: ["msg"] } } };
handleConsoleEvent(event, { logToFile: true }, shardBuffers, state);
expect(shardBuffers["default"]).toEqual(["msg"]);
}); });
}); });
@@ -597,37 +519,4 @@ describe("monitorConsole", () => {
expect(result.sawTraceback).toBe(true); expect(result.sawTraceback).toBe(true);
expect(result.sawErrorLog).toBe(true); expect(result.sawErrorLog).toBe(true);
}); });
it("creates separate log files for different shards when logToFile=true", async () => {
const api = buildMockApi({ ticks: [100, 101, 102, 103] });
setTimeout(() => {
api._fireConsole({
shard: "shard0",
messages: { log: ["msg0"], results: [] },
error: null,
});
api._fireConsole({
shard: "shard1",
messages: { log: ["msg1"], results: [] },
error: null,
});
}, 50);
// Verify uploadArtifact was called with two files
await monitorConsole(api, {
...BASE_OPTS,
logToFile: true,
});
expect(artifact.create().uploadArtifact).toHaveBeenCalledWith(
"screeps-console-log",
expect.arrayContaining([
expect.stringContaining("shard0_console_log.txt"),
expect.stringContaining("shard1_console_log.txt"),
]),
expect.any(String),
{ continueOnError: true },
);
});
}); });
-4
View File
@@ -67,10 +67,6 @@ inputs:
description: 'Print a progress update every N ticks when log_to_file=true (default: 10).' description: 'Print a progress update every N ticks when log_to_file=true (default: 10).'
required: false required: false
default: '10' default: '10'
rollback_on_failure:
description: 'Automatically rollback to previous code if the monitor detects failures. Requires monitor > 0. (default: false)'
required: false
default: 'false'
outputs: outputs:
saw_traceback: saw_traceback:
description: true if a JS traceback was detected during monitoring. description: true if a JS traceback was detected during monitoring.
+85 -12
View File
File diff suppressed because one or more lines are too long
+25 -82
View File
@@ -118,19 +118,17 @@ export function validateAuthentication(token, username, password) {
* @param {'ignore'|'warn'|'fail'} action * @param {'ignore'|'warn'|'fail'} action
* @param {boolean} flag - Only acts when true * @param {boolean} flag - Only acts when true
* @param {string} message - Passed to core.warning / core.setFailed * @param {string} message - Passed to core.warning / core.setFailed
* @returns {boolean} - Returns true if the action was 'fail' and the flag was true.
*/ */
export function applyOnAction(action, flag, message) { export function applyOnAction(action, flag, message) {
if (!flag) return false; if (!flag) return;
if (action === "warn") { if (action === "warn") {
core.warning(message); core.warning(message);
return false; return;
} }
if (action === "fail") { if (action === "fail") {
core.setFailed(message); core.setFailed(message);
return true;
} }
return false; // 'ignore' → no-op
} }
/** /**
@@ -181,72 +179,33 @@ export async function postCode() {
return; return;
} }
let api = new ScreepsAPI(login_arguments); let api = new ScreepsAPI(login_arguments);
if (token) {
if (!token) {
core.info(`Logging in as user ${username}`);
try {
await api.auth(username, password, login_arguments);
} catch (err) {
core.error(`Authentication error: ${err}`);
throw err;
}
}
let oldCode = null;
let rollbackOnFailure = false;
try {
rollbackOnFailure = core.getBooleanInput("rollback_on_failure");
} catch (e) {
// getBooleanInput throws if not 'true' or 'false', ignore
}
if (rollbackOnFailure) {
core.info(
`Downloading existing code from branch ${branch} for potential rollback...`,
);
try {
const getResponse = await api.code.get(branch);
if (getResponse && getResponse.ok && getResponse.modules) {
oldCode = getResponse.modules;
core.info(
`Successfully downloaded existing code (modules: ${Object.keys(oldCode).join(", ")})`,
);
} else {
core.setFailed(
`Failed to download existing code, but rollback_on_failure is enabled. Aborting deployment.`,
);
return;
}
} catch (err) {
core.setFailed(
`Error downloading existing code: ${err.message}. Aborting deployment.`,
);
return;
}
}
try {
const response = await api.code.set(branch, files_to_push); const response = await api.code.set(branch, files_to_push);
core.info(JSON.stringify(response, null, 2)); core.info(JSON.stringify(response, null, 2));
core.info(`Code set successfully to ${branch}`); core.info(`Code set successfully to ${branch}`);
} catch (err) { } else {
core.error(`Upload error: ${err}`); core.info(`Logging in as user ${username}`);
throw err; await Promise.resolve()
.then(() => api.auth(username, password, login_arguments))
.then(() => api.code.set(branch, files_to_push))
.then(() => {
core.info(`Code set successfully to ${branch}`);
})
.catch((err) => {
core.error(`Upload error: ${err}`);
throw err;
});
} }
// Console monitoring (optional) // Console monitoring (optional)
const monitorTicks = parseInt(core.getInput("monitor") || "0", 10); const monitorTicks = parseInt(core.getInput("monitor") || "0", 10);
if (monitorTicks > 0) { if (monitorTicks > 0) {
const onTraceback = core.getInput("on_traceback") || "fail";
const onErrorLog = core.getInput("on_error_log") || "warn";
const onWarningLog = core.getInput("on_warning_log") || "ignore";
const result = await monitorConsole(api, { const result = await monitorConsole(api, {
monitor: monitorTicks, monitor: monitorTicks,
logToFile: core.getBooleanInput("log_to_file"), logToFile: core.getBooleanInput("log_to_file"),
onTraceback, onTraceback: core.getInput("on_traceback") || "fail",
onErrorLog, onErrorLog: core.getInput("on_error_log") || "warn",
onWarningLog, onWarningLog: core.getInput("on_warning_log") || "ignore",
monitorInterval: parseInt(core.getInput("monitor_interval") || "10", 10), monitorInterval: parseInt(core.getInput("monitor_interval") || "10", 10),
hostname, hostname,
shard: core.getInput("shard") || undefined, shard: core.getInput("shard") || undefined,
@@ -256,37 +215,21 @@ export async function postCode() {
core.setOutput("saw_error_log", String(result.sawErrorLog)); core.setOutput("saw_error_log", String(result.sawErrorLog));
core.setOutput("saw_warning_log", String(result.sawWarningLog)); core.setOutput("saw_warning_log", String(result.sawWarningLog));
const fail1 = applyOnAction( applyOnAction(
onTraceback, core.getInput("on_traceback"),
result.sawTraceback, result.sawTraceback,
"Screeps console: traceback detected", "Screeps console: traceback detected",
); );
const fail2 = applyOnAction( applyOnAction(
onErrorLog, core.getInput("on_error_log"),
result.sawErrorLog, result.sawErrorLog,
"Screeps console: error log output detected", "Screeps console: error log output detected",
); );
const fail3 = applyOnAction( applyOnAction(
onWarningLog, core.getInput("on_warning_log"),
result.sawWarningLog, result.sawWarningLog,
"Screeps console: warning log output detected", "Screeps console: warning log output detected",
); );
const shouldFail = fail1 || fail2 || fail3;
if (shouldFail && rollbackOnFailure && oldCode) {
core.info(
"Action failed based on monitor configuration. Rolling back to previous code...",
);
try {
await api.code.set(branch, oldCode);
core.info(
`Successfully rolled back to previous code on branch ${branch}.`,
);
} catch (err) {
core.error(`Rollback failed: ${err}`);
}
}
} }
} }
+33 -50
View File
@@ -1,5 +1,5 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import { create } from "@actions/artifact"; import { DefaultArtifactClient } from "@actions/artifact";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import os from "os"; import os from "os";
@@ -88,21 +88,17 @@ export function safeDecode(text) {
/** /**
* Outputs text to the action log, splitting by newline to ensure proper display. * Outputs text to the action log, splitting by newline to ensure proper display.
* Optionally prepends a [shard] prefix.
* *
* @param {string} text * @param {string} text
* @param {"info"|"warning"|"error"} level * @param {"info"|"warning"|"error"} level
* @param {string} [shard]
*/ */
export function outputMultiline(text, level = "info", shard = null) { export function outputMultiline(text, level = "info") {
if (!text) return; if (!text) return;
const prefix = shard ? `[${shard}] ` : "";
const lines = text.split(/\r?\n/); const lines = text.split(/\r?\n/);
lines.forEach((line) => { lines.forEach((line) => {
const formattedLine = `${prefix}${line}`; if (level === "error") core.error(line);
if (level === "error") core.error(formattedLine); else if (level === "warning") core.warning(line);
else if (level === "warning") core.warning(formattedLine); else core.info(line);
else core.info(formattedLine);
}); });
} }
@@ -129,29 +125,29 @@ export async function writeLogFile(lines, filePath) {
} }
/** /**
* Uploads one or more files as a named workflow artifact using @actions/artifact. * Uploads a file as a named workflow artifact using @actions/artifact.
* Degrades gracefully to core.warning() if the runner does not support the * Degrades gracefully to core.warning() if the runner does not support the
* artifact service. * artifact service (e.g. a bare self-hosted runner without the service configured).
* *
* @param {string[]} filePaths - Absolute paths of the files to upload * @param {string} filePath - Absolute path of the file to upload
* @param {string} [artifactName="screeps-console-log"] - Artifact display name * @param {string} [artifactName="screeps-console-log"] - Artifact display name
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
export async function uploadLogArtifacts( export async function uploadLogArtifact(
filePaths, filePath,
artifactName = "screeps-console-log", artifactName = "screeps-console-log",
) { ) {
if (!filePaths || filePaths.length === 0) return;
try { try {
const client = create(); const client = new DefaultArtifactClient();
const rootDir = path.dirname(filePaths[0]); await client.uploadArtifact(
await client.uploadArtifact(artifactName, filePaths, rootDir, { artifactName,
continueOnError: true, [filePath],
}); path.dirname(filePath),
core.info(`[Monitor] Console logs uploaded as artifact '${artifactName}'.`); );
core.info(`[Monitor] Console log uploaded as artifact '${artifactName}'.`);
} catch (err) { } catch (err) {
core.warning( core.warning(
`[Monitor] Could not upload console logs as artifact: ${err.message}`, `[Monitor] Could not upload console log as artifact: ${err.message}`,
); );
} }
} }
@@ -177,12 +173,12 @@ export async function uploadLogArtifacts(
* - All stdout lines → core.info() when logToFile=false, * - All stdout lines → core.info() when logToFile=false,
* pushed to stdoutBuffer when logToFile=true. * pushed to stdoutBuffer when logToFile=true.
* *
* @param {{ data?: { messages?: { log?: string[], results?: string[] }, error?: string, shard?: string } }} event * @param {{ data?: { messages?: { log?: string[], results?: string[] }, error?: string } }} event
* @param {{ logToFile: boolean, shard?: string }} opts * @param {{ logToFile: boolean }} opts
* @param {Record<string, string[]>} shardBuffers - Mutable buffer Map used in logToFile mode * @param {string[]} stdoutBuffer - Mutable buffer used in logToFile mode
* @param {{ sawTraceback: boolean, sawErrorLog: boolean, sawWarningLog: boolean }} state * @param {{ sawTraceback: boolean, sawErrorLog: boolean, sawWarningLog: boolean }} state
*/ */
export function handleConsoleEvent(event, opts, shardBuffers, state) { export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const { logToFile, shard: targetShard } = opts; const { logToFile, shard: targetShard } = opts;
const data = event?.data ?? {}; const data = event?.data ?? {};
@@ -202,7 +198,7 @@ export function handleConsoleEvent(event, opts, shardBuffers, state) {
const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i; const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i;
logLines logLines
.filter((l) => warnPattern.test(l)) .filter((l) => warnPattern.test(l))
.forEach((l) => outputMultiline(safeDecode(l), "warning", data.shard)); .forEach((l) => outputMultiline(safeDecode(l), "warning"));
} }
// Traceback detection in log lines (Screeps sometimes sends errors here) // Traceback detection in log lines (Screeps sometimes sends errors here)
@@ -215,11 +211,9 @@ export function handleConsoleEvent(event, opts, shardBuffers, state) {
const allStdout = [...logLines, ...results].map(safeDecode); const allStdout = [...logLines, ...results].map(safeDecode);
if (allStdout.length > 0) { if (allStdout.length > 0) {
if (logToFile) { if (logToFile) {
const shard = data.shard || "default"; stdoutBuffer.push(...allStdout);
if (!shardBuffers[shard]) shardBuffers[shard] = [];
shardBuffers[shard].push(...allStdout);
} else { } else {
allStdout.forEach((l) => outputMultiline(l, "info", data.shard)); allStdout.forEach((l) => outputMultiline(l, "info"));
} }
} }
@@ -227,7 +221,7 @@ export function handleConsoleEvent(event, opts, shardBuffers, state) {
if (errorText) { if (errorText) {
state.sawErrorLog = true; state.sawErrorLog = true;
const decodedError = safeDecode(errorText); const decodedError = safeDecode(errorText);
outputMultiline(decodedError, "error", data.shard); outputMultiline(decodedError, "error");
if (detectTraceback(decodedError)) { if (detectTraceback(decodedError)) {
state.sawTraceback = true; state.sawTraceback = true;
} }
@@ -328,7 +322,7 @@ export async function monitorConsole(api, opts) {
const subscribePath = buildSubscribePath(hostname, providedShard); const subscribePath = buildSubscribePath(hostname, providedShard);
// Shared mutable state — updated by handleConsoleEvent via event listener // Shared mutable state — updated by handleConsoleEvent via event listener
const shardBuffers = {}; // { [shardName]: string[] } const stdoutBuffer = [];
const state = { const state = {
sawTraceback: false, sawTraceback: false,
sawErrorLog: false, sawErrorLog: false,
@@ -342,7 +336,7 @@ export async function monitorConsole(api, opts) {
// Step 2: connect socket + subscribe // Step 2: connect socket + subscribe
await api.socket.connect(); await api.socket.connect();
await api.socket.subscribe(subscribePath, (event) => { await api.socket.subscribe(subscribePath, (event) => {
handleConsoleEvent(event, opts, shardBuffers, state); handleConsoleEvent(event, opts, stdoutBuffer, state);
}); });
core.info( core.info(
@@ -384,24 +378,13 @@ export async function monitorConsole(api, opts) {
} }
// Step 6: artifact upload // Step 6: artifact upload
const shardKeys = Object.keys(shardBuffers); if (logToFile && stdoutBuffer.length > 0) {
if (logToFile && shardKeys.length > 0) {
const tmpDir = await fs.promises.mkdtemp( const tmpDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), "screeps-monitor-"), path.join(os.tmpdir(), "screeps-monitor-"),
); );
const filePaths = []; const tmpFile = path.join(tmpDir, "screeps_console_log.txt");
await writeLogFile(stdoutBuffer, tmpFile);
for (const sName of shardKeys) { await uploadLogArtifact(tmpFile);
const fileName =
sName === "default"
? "screeps_console_log.txt"
: `${sName}_console_log.txt`;
const tmpFile = path.join(tmpDir, fileName);
await writeLogFile(shardBuffers[sName], tmpFile);
filePaths.push(tmpFile);
}
await uploadLogArtifacts(filePaths);
} else if (logToFile) { } else if (logToFile) {
core.info( core.info(
"[Monitor] No stdout lines were collected; skipping artifact upload.", "[Monitor] No stdout lines were collected; skipping artifact upload.",
+1816 -28
View File
File diff suppressed because it is too large Load Diff
-1
View File
@@ -10,7 +10,6 @@
"build": "ncc build index.js -o dist -m --external utf-8-validate --external bufferutil" "build": "ncc build index.js -o dist -m --external utf-8-validate --external bufferutil"
}, },
"dependencies": { "dependencies": {
"@actions/artifact": "^1.1.2",
"@actions/core": "^3.0.0", "@actions/core": "^3.0.0",
"glob": "^13.0.0", "glob": "^13.0.0",
"screeps-api": "^1.7.2" "screeps-api": "^1.7.2"