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