Add flake-dirs input

This commit is contained in:
Luc Perkins 2024-05-23 15:16:12 -03:00
parent 222f041780
commit 6a1287939f
No known key found for this signature in database
GPG key ID: 16DB1108FB591835
4 changed files with 67 additions and 7 deletions

View file

@ -23,6 +23,11 @@ inputs:
path-to-flake-dir:
description: "The path of the directory containing `flake.nix` file within your repository. Useful when `flake.nix` cannot reside at the root of your repository."
required: false
flake-dirs:
description: |
A space-separated list of directories containing `flake.nix` files within your repository. Useful when you have multiple flakes in your repository.
required: false
default: ""
pr-title:
description: "The title of the PR to be created"
required: false
@ -164,6 +169,7 @@ runs:
INPUT_INPUTS: ${{ inputs.inputs }}
INPUT_NIX-OPTIONS: ${{ inputs.nix-options }}
INPUT_PATH-TO-FLAKE-DIR: ${{ inputs.path-to-flake-dir }}
INPUT_FLAKE-DIRS: ${{ inputs.flake-dirs }}
INPUT_PR-ASSIGNEES: ${{ inputs.pr-assignees }}
INPUT_PR-BODY: ${{ inputs.pr-body }}
INPUT_PR-LABELS: ${{ inputs.pr-labels }}

28
dist/index.js vendored
View file

@ -94778,6 +94778,10 @@ var UpdateFlakeLockAction = class extends DetSysAction {
this.flakeInputs = inputs_exports.getArrayOfStrings("inputs", "space");
this.nixOptions = inputs_exports.getArrayOfStrings("nix-options", "space");
this.pathToFlakeDir = inputs_exports.getStringOrNull("path-to-flake-dir");
this.flakeDirs = inputs_exports.getArrayOfStrings("flake-dirs", "space");
if (this.flakeDirs !== null && this.flakeDirs.length > 0 && this.pathToFlakeDir !== "") {
throw new Error("Both path-to-flake-dir and flake-dirs is defined");
}
}
async main() {
await this.update();
@ -94786,6 +94790,19 @@ var UpdateFlakeLockAction = class extends DetSysAction {
async post() {
}
async update() {
if (this.flakeDirs !== null && this.flakeDirs.length > 0) {
core.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);
}
}
async updateFlake(directory) {
core.debug(`Running flake lock update in directory ${directory}`);
const nixCommandArgs = makeNixCommandArgs(
this.nixOptions,
this.flakeInputs,
@ -94793,6 +94810,7 @@ var UpdateFlakeLockAction = class extends DetSysAction {
);
core.debug(
JSON.stringify({
directory,
options: this.nixOptions,
inputs: this.flakeInputs,
message: this.commitMessage,
@ -94800,16 +94818,20 @@ var UpdateFlakeLockAction = class extends DetSysAction {
})
);
const execOptions = {
cwd: this.pathToFlakeDir !== null ? this.pathToFlakeDir : void 0
cwd: directory
};
const exitCode = await exec.exec("nix", nixCommandArgs, execOptions);
if (exitCode !== 0) {
this.recordEvent(EVENT_EXECUTION_FAILURE, {
exitCode
});
core.setFailed(`non-zero exit code of ${exitCode} detected`);
core.setFailed(
`non-zero exit code of ${exitCode} detected while updating directory ${directory}`
);
} else {
core.info(`flake.lock file was successfully updated`);
core.info(
`flake.lock file in ${directory} was successfully updated`
);
}
}
};

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -10,6 +10,7 @@ class UpdateFlakeLockAction extends DetSysAction {
private nixOptions: string[];
private flakeInputs: string[];
private pathToFlakeDir: string | null;
private flakeDirs: string[] | null;
constructor() {
super({
@ -22,6 +23,16 @@ class UpdateFlakeLockAction extends DetSysAction {
this.flakeInputs = inputs.getArrayOfStrings("inputs", "space");
this.nixOptions = inputs.getArrayOfStrings("nix-options", "space");
this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir");
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");
}
}
async main(): Promise<void> {
@ -32,6 +43,22 @@ class UpdateFlakeLockAction extends DetSysAction {
async post(): Promise<void> {}
async update(): Promise<void> {
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}`);
// Nix command of this form:
// nix ${maybe nix options} flake ${"update" or "lock"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}
// Example commands:
@ -45,6 +72,7 @@ class UpdateFlakeLockAction extends DetSysAction {
actionsCore.debug(
JSON.stringify({
directory,
options: this.nixOptions,
inputs: this.flakeInputs,
message: this.commitMessage,
@ -53,7 +81,7 @@ class UpdateFlakeLockAction extends DetSysAction {
);
const execOptions: actionsExec.ExecOptions = {
cwd: this.pathToFlakeDir !== null ? this.pathToFlakeDir : undefined,
cwd: directory,
};
const exitCode = await actionsExec.exec("nix", nixCommandArgs, execOptions);
@ -62,9 +90,13 @@ class UpdateFlakeLockAction extends DetSysAction {
this.recordEvent(EVENT_EXECUTION_FAILURE, {
exitCode,
});
actionsCore.setFailed(`non-zero exit code of ${exitCode} detected`);
actionsCore.setFailed(
`non-zero exit code of ${exitCode} detected while updating directory ${directory}`,
);
} else {
actionsCore.info(`flake.lock file was successfully updated`);
actionsCore.info(
`flake.lock file in ${directory} was successfully updated`,
);
}
}
}