update-flake-lock/src/index.ts

77 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-04-26 19:19:53 +02:00
import { makeNixCommandArgs } from "./nix.js";
2024-04-26 16:55:19 +02:00
import * as actionsCore from "@actions/core";
import * as actionsExec from "@actions/exec";
2024-05-22 20:40:01 +02:00
import { DetSysAction, inputs } from "detsys-ts";
2024-04-22 00:42:23 +02:00
2024-04-26 16:55:19 +02:00
const EVENT_EXECUTION_FAILURE = "execution_failure";
2024-05-22 20:40:01 +02:00
class UpdateFlakeLockAction extends DetSysAction {
2024-04-22 00:42:23 +02:00
private commitMessage: string;
2024-04-26 19:19:53 +02:00
private nixOptions: string[];
private flakeInputs: string[];
2024-04-26 16:55:19 +02:00
private pathToFlakeDir: string | null;
2024-04-22 00:42:23 +02:00
constructor() {
2024-05-22 20:40:01 +02:00
super({
2024-04-22 00:42:23 +02:00
name: "update-flake-lock",
fetchStyle: "universal",
requireNix: "fail",
2024-05-22 20:40:01 +02:00
});
2024-04-22 00:42:23 +02:00
this.commitMessage = inputs.getString("commit-msg");
2024-05-10 00:13:07 +02:00
this.flakeInputs = inputs.getArrayOfStrings("inputs", "space");
2024-05-09 21:26:43 +02:00
this.nixOptions = inputs.getArrayOfStrings("nix-options", "space");
2024-04-26 16:55:19 +02:00
this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir");
2024-04-22 00:42:23 +02:00
}
2024-05-22 20:40:01 +02:00
async main(): Promise<void> {
await this.update();
}
// No post phase
async post(): Promise<void> {}
async update(): Promise<void> {
2024-04-26 16:55:19 +02:00
// Nix command of this form:
2024-05-09 21:47:03 +02:00
// nix ${maybe nix options} flake ${"update" or "lock"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}
// Example commands:
2024-05-06 23:45:12 +02:00
// nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lockfile-summary "updated flake.lock"
2024-05-09 21:45:38 +02:00
// nix flake update --commit-lock-file --commit-lockfile-summary "updated flake.lock"
2024-04-26 19:19:53 +02:00
const nixCommandArgs: string[] = makeNixCommandArgs(
this.nixOptions,
this.flakeInputs,
this.commitMessage,
);
2024-04-26 16:55:19 +02:00
2024-05-06 23:34:23 +02:00
actionsCore.debug(
JSON.stringify({
options: this.nixOptions,
inputs: this.flakeInputs,
message: this.commitMessage,
args: nixCommandArgs,
}),
);
2024-05-22 20:40:01 +02:00
const execOptions: actionsExec.ExecOptions = {
cwd: this.pathToFlakeDir !== null ? this.pathToFlakeDir : undefined,
};
2024-04-26 16:55:19 +02:00
2024-05-06 22:13:34 +02:00
const exitCode = await actionsExec.exec("nix", nixCommandArgs, execOptions);
2024-04-22 00:50:32 +02:00
2024-04-26 16:55:19 +02:00
if (exitCode !== 0) {
2024-05-22 20:40:01 +02:00
this.recordEvent(EVENT_EXECUTION_FAILURE, {
2024-04-26 16:55:19 +02:00
exitCode,
});
actionsCore.setFailed(`non-zero exit code of ${exitCode} detected`);
} else {
actionsCore.info(`flake.lock file was successfully updated`);
}
2024-04-22 00:50:32 +02:00
}
2024-04-22 00:42:23 +02:00
}
function main(): void {
2024-05-22 20:40:01 +02:00
new UpdateFlakeLockAction().execute();
2024-04-22 00:42:23 +02:00
}
2024-04-22 00:17:03 +02:00
main();