Compare commits

..

10 commits

Author SHA1 Message Date
Skryta Istota
8a752f92cf Merge branch 'fix-job-patch' into 'main'
fix: weird jobs patching

See merge request TECHNOFAB/nix-gitlab-ci!15
2025-12-03 19:06:02 +01:00
Skryta Istota
65b90ecd65
rollback(cilib_test) Synchronized the test file of the Ci library
Updated the continuous integration test file to be compatible with the newer main branch.
2025-12-03 18:35:36 +01:00
Skryta Istota
c8f51c73ed
fix(job) Fixed premature filtering of variables
Fixed excessive deletion of environment variables containing paths to nix store package files,
causing them to not be exported in Giltab's CI environment,
plus removed duplicate tests for moving environment variables.
2025-12-03 17:35:36 +01:00
Skryta Istota
8336e6d083
tests(modules_test) Extended test definition for modules
Extended the definition of tests for the continuous integration modules,
allowing for more in-depth testing of odd behavior of some configuration generators.
2025-12-03 16:55:05 +01:00
Skryta Istota
0ef6f4d2ff
rollback(jobPatched) Restored some features for patching jobs for ci
Tests for this filtering of environment variables in ci have also been rollback.
2025-12-03 16:35:45 +01:00
Skryta Istota
fb234b80bf
chore(ci) Added job to check correct passing of variables
Append a test for the correctness of transfer and creation of environment variables in ci,
and added the required packages to run the remaining jobs in the terminal.
2025-12-02 15:35:56 +01:00
Skryta Istota
d8e55a05b2
fix(nixfmt) Formatted modified files to default style
The style of files modified in this branch has been corrected.
2025-12-02 15:15:15 +01:00
59f8bd169a
chore: bump version 2025-12-02 15:12:28 +01:00
97fb4fafc3
fix(jobPatched): handle non-nix jobs correctly
fix mkJobPatched removing `cache` and `variables` from non-nix jobs

See !15 for more
2025-12-02 15:10:35 +01:00
1c9e7c77c5
chore: add test and docs for handling nix store paths in global variables 2025-12-02 15:09:27 +01:00
7 changed files with 139 additions and 188 deletions

View file

@ -9,3 +9,14 @@ This project provides a Nix flake module that allows you to generate your `.gitl
- **Modularity:** Define and manage your CI configurations in a structured and modular way using Nix modules, making it easier to share and reuse CI logic across multiple projects. - **Modularity:** Define and manage your CI configurations in a structured and modular way using Nix modules, making it easier to share and reuse CI logic across multiple projects.
This documentation will guide you through setting up and using Nix GitLab CI for your projects. This documentation will guide you through setting up and using Nix GitLab CI for your projects.
## Warnings
To save you from frantically searching these docs if something doesn't work as expected, here are the most important warnings ;)
!!! warning
Do not put Nix store paths into global/pipeline variables. They will simply be passed through,
resulting in bad portability (if two runners have different archs for example, one cannot find the path).
If you need any Nix store path in env variables, always do it on the job level, there
it will automatically be computed at runtime, thus will always work no matter which runner it runs on.

View file

@ -1 +1 @@
3.0.1 3.1.0

View file

@ -3,7 +3,7 @@
helpers, helpers,
}: let }: let
inherit (lib) toList optionalAttrs optional; inherit (lib) toList optionalAttrs optional;
inherit (helpers) prependToBeforeScript appendToAfterScript; inherit (helpers) prependToBeforeScript appendToAfterScript filterJobVariables;
in in
{ {
key, key,
@ -11,13 +11,16 @@ in
pipelineName, pipelineName,
nixConfig, nixConfig,
}: }:
if ! nixConfig.enable then job else if ! nixConfig.enable
then job
else
(builtins.removeAttrs job ["variables" "cache"]) (builtins.removeAttrs job ["variables" "cache"])
// (prependToBeforeScript ["source setup_nix_ci \"gitlab-ci:pipeline:${pipelineName}:job-deps:${key}\""] job) // (prependToBeforeScript ["source setup_nix_ci \"gitlab-ci:pipeline:${pipelineName}:job-deps:${key}\""] job)
// (appendToAfterScript ["finalize_nix_ci"] job) // (appendToAfterScript ["finalize_nix_ci"] job)
// (let // (let
variables = job.variables or {} // variables =
optionalAttrs nixConfig.enableRunnerCache { (filterJobVariables false job)
// optionalAttrs nixConfig.enableRunnerCache {
NIX_CI_CACHE_STRATEGY = "runner"; NIX_CI_CACHE_STRATEGY = "runner";
}; };
in in
@ -26,8 +29,9 @@ in
inherit variables; inherit variables;
}) })
// (let // (let
cache = (toList (job.cache or [])) ++ cache =
(optional nixConfig.enableRunnerCache { (toList (job.cache or []))
++ (optional nixConfig.enableRunnerCache {
key = nixConfig.runnerCacheKey; key = nixConfig.runnerCacheKey;
paths = [".nix-cache/"]; paths = [".nix-cache/"];
}); });

View file

@ -646,7 +646,7 @@ in rec {
}; };
depsDrv = cilib.mkJobDeps { depsDrv = cilib.mkJobDeps {
key = name; key = name;
job = config.finalConfig; inherit job;
nixConfig = config.nix; nixConfig = config.nix;
}; };
runnerDrv = cilib.mkJobRun { runnerDrv = cilib.mkJobRun {

View file

@ -1,5 +1,5 @@
{inputs, ...}: let {inputs, ...}: let
inherit (inputs) cilib; inherit (inputs) cilib pkgs;
in in
cilib.mkCI { cilib.mkCI {
config.soonix = { config.soonix = {
@ -58,9 +58,25 @@ in
}; };
}; };
pipelines."default" = { pipelines."default" = {
stages = ["test" "build" "deploy"]; stages = ["check" "test" "build" "deploy"];
variables = {
EXAMPLE = "empty";
CURL = toString pkgs.curl;
};
jobs = { jobs = {
"check" = {
stage = "check";
script = [
"set -euo pipefail"
"echo EXAMPLE=$EXAMPLE CURL=$CURL SAMPLE=$SAMPLE HELLO=$HELLO"
];
variables = {
SAMPLE = "working";
HELLO = toString pkgs.hello;
};
};
"test" = { "test" = {
nix.deps = with pkgs; [coreutils nix];
stage = "test"; stage = "test";
script = [ script = [
"nix run .#tests -- --junit=junit.xml" "nix run .#tests -- --junit=junit.xml"
@ -73,6 +89,7 @@ in
}; };
"docs" = { "docs" = {
stage = "build"; stage = "build";
nix.deps = with pkgs; [coreutils nix];
script = [ script = [
# sh # sh
'' ''

View file

@ -56,28 +56,26 @@
} }
{ {
name = "jobPatched nix disabled"; name = "jobPatched nix disabled";
expected = {};
actual = mkJobPatched {
key = "test";
pipelineName = "test";
job = {};
nixConfig.enable = false;
};
}
{
name = "jobPatched nix disabled with variables and cache";
expected = { expected = {
cache = { variables."HELLO" = "world";
name = "some"; cache = [{key = "example";}];
paths = [ "this" ];
};
variables = {
BASH = toString pkgs.bash;
TEST = "work";
};
}; };
actual = mkJobPatched { actual = mkJobPatched {
key = "test"; key = "test";
pipelineName = "test"; pipelineName = "test";
job = { job = {
cache = { variables."HELLO" = "world";
name = "some"; cache = [{key = "example";}];
paths = [ "this" ];
};
variables = {
BASH = toString pkgs.bash;
TEST = "work";
};
}; };
nixConfig.enable = false; nixConfig.enable = false;
}; };
@ -87,28 +85,11 @@
expected = { expected = {
after_script = ["finalize_nix_ci"]; after_script = ["finalize_nix_ci"];
before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:test:job-deps:test\""]; before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:test:job-deps:test\""];
variables = {
BASH = toString pkgs.bash;
TEST = "work";
};
cache = [{
name = "some";
paths = [ "this" ];
}];
}; };
actual = mkJobPatched { actual = mkJobPatched {
key = "test"; key = "test";
pipelineName = "test"; pipelineName = "test";
job = { job = {};
cache = {
name = "some";
paths = [ "this" ];
};
variables = {
BASH = toString pkgs.bash;
TEST = "work";
};
};
nixConfig = { nixConfig = {
enable = true; enable = true;
enableRunnerCache = false; enableRunnerCache = false;
@ -120,32 +101,18 @@
expected = { expected = {
after_script = ["finalize_nix_ci"]; after_script = ["finalize_nix_ci"];
before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:test:job-deps:test\""]; before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:test:job-deps:test\""];
cache = [{ cache = [
name = "some"; {
paths = [ "this" ];
}{
key = "test"; key = "test";
paths = [".nix-cache/"]; paths = [".nix-cache/"];
}]; }
variables = { ];
NIX_CI_CACHE_STRATEGY = "runner"; variables."NIX_CI_CACHE_STRATEGY" = "runner";
BASH = toString pkgs.bash;
TEST = "work";
};
}; };
actual = mkJobPatched { actual = mkJobPatched {
key = "test"; key = "test";
pipelineName = "test"; pipelineName = "test";
job = { job = {};
cache = {
name = "some";
paths = [ "this" ];
};
variables = {
BASH = toString pkgs.bash;
TEST = "work";
};
};
nixConfig = { nixConfig = {
enable = true; enable = true;
enableRunnerCache = true; enableRunnerCache = true;
@ -194,108 +161,46 @@
''; '';
} }
{ {
name = "handle store paths in variables without nix config"; name = "ignore store paths in variables with nix disabled";
expected = { expected = {
stages = ["test"]; stages = ["test"];
variables = {
EXAMPLE = "empty";
CURL = toString pkgs.curl;
};
test = { test = {
stage = "test"; stage = "test";
before_script = [ "./init" ]; variables."TEST" = "${pkgs.hello}";
script = [ "echo Hello World!" ];
after_script = [ "./clean" ];
cache = {
key = "simple";
paths = [ "~/random/" ];
};
variables = {
SAMPLE = "working";
HELLO = toString pkgs.hello;
}; };
}; };
}; actual =
actual = (mkPipeline { (mkPipeline {
name = "test"; name = "test";
nixConfig.enable = false; nixConfig.enable = false;
pipeline = { pipeline = {
stages = ["test"]; stages = ["test"];
variables = {
EXAMPLE = "empty";
CURL = toString pkgs.curl;
};
jobs.test = { jobs.test = {
stage = "test"; stage = "test";
before_script = [ "./init" ]; variables."TEST" = "${pkgs.hello}";
script = [ "echo Hello World!" ];
after_script = [ "./clean" ];
cache = {
key = "simple";
paths = [ "~/random/" ];
};
variables = {
SAMPLE = "working";
HELLO = toString pkgs.hello;
};
}; };
}; };
}).finalConfig; }).finalConfig;
} }
{ {
name = "handle store paths in variables with nix config"; # it doesn't make much sense to have any nix store path in variables, but we ignore it for global variables
name = "ignore store paths in global variables";
expected = { expected = {
stages = [ "test" ];
variables = { variables = {
EXAMPLE = "empty"; HELLO = "world";
CURL = toString pkgs.curl; CURL = toString pkgs.curl;
}; };
test = {
stage = "test";
before_script = [ "source setup_nix_ci \"gitlab-ci:pipeline:test:job-deps:test\"" "./init" ];
script = [ "echo Hello World!" ];
after_script = [ "./clean" "finalize_nix_ci" ];
cache = [{
key = "simple";
paths = [ "~/random/" ];
}{
key = "random";
paths = [ ".nix-cache/" ];
}];
variables = {
SAMPLE = "working";
HELLO = toString pkgs.hello;
NIX_CI_CACHE_STRATEGY = "runner";
}; };
}; actual =
}; (mkPipeline {
actual = (mkPipeline {
name = "test"; name = "test";
nixConfig = { nixConfig.enable = true;
enable = true;
enableRunnerCache = true;
runnerCacheKey = "random";
};
pipeline = { pipeline = {
stages = [ "test" ];
variables = { variables = {
EXAMPLE = "empty"; HELLO = "world";
CURL = toString pkgs.curl; CURL = toString pkgs.curl;
}; };
jobs.test = { jobs = {};
stage = "test";
before_script = [ "./init" ];
script = [ "echo Hello World!" ];
after_script = [ "./clean" ];
cache = {
key = "simple";
paths = [ "~/random/" ];
};
variables = {
SAMPLE = "working";
HELLO = toString pkgs.hello;
};
};
}; };
}).finalConfig; }).finalConfig;
} }

View file

@ -10,9 +10,17 @@
simplePipeline = cilib.mkCI { simplePipeline = cilib.mkCI {
pipelines."test" = { pipelines."test" = {
stages = ["test"]; stages = ["test"];
variables = {
EXAMPLE = "empty";
CURL = toString pkgs.curl;
};
jobs."test" = { jobs."test" = {
stage = "test"; stage = "test";
script = ["echo hello world"]; script = ["echo hello world"];
variables = {
SAMPLE = "working";
HELLO = toString pkgs.hello;
};
}; };
}; };
}; };
@ -30,21 +38,29 @@
(cilib.mkCI {}).packages; (cilib.mkCI {}).packages;
} }
{ {
name = "simple pipeline"; name = "simple pipeline final config";
expected = { expected = {
stages = [".pre" "test" ".post"]; stages = [".pre" "test" ".post"];
variables = {
EXAMPLE = "empty";
CURL = toString pkgs.curl;
};
"test" = { "test" = {
image = "$NIX_CI_IMAGE"; image = "$NIX_CI_IMAGE";
stage = "test"; stage = "test";
before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:test:job-deps:test\""]; before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:test:job-deps:test\""];
script = ["echo hello world"]; script = ["echo hello world"];
after_script = ["finalize_nix_ci"]; after_script = ["finalize_nix_ci"];
variables = {
SAMPLE = "working";
#HELLO = toString pkgs.hello;
};
}; };
}; };
actual = simplePipeline.pipelines."test".finalConfig; actual = simplePipeline.pipelines."test".finalConfig;
} }
{ {
name = "simple pipeline yaml"; name = "simple pipeline json";
type = "script"; type = "script";
script = let script = let
package = simplePipeline.packages."gitlab-ci:pipeline:test"; package = simplePipeline.packages."gitlab-ci:pipeline:test";
@ -56,25 +72,23 @@
assert_file_contains ${package} 'gitlab-ci:pipeline:test:job-deps:test' assert_file_contains ${package} 'gitlab-ci:pipeline:test:job-deps:test'
assert_file_contains ${package} 'finalize_nix_ci' assert_file_contains ${package} 'finalize_nix_ci'
assert_file_contains ${package} 'echo hello world' assert_file_contains ${package} 'echo hello world'
assert_file_contains ${package} '"EXAMPLE":"empty"'
assert_file_contains ${package} '"SAMPLE":"working"'
assert_file_contains ${package} '"CURL":"/nix/store/.*-curl-.*"'
''; '';
} }
{ {
name = "dont fail on store paths"; name = "simple pipeline deps drv";
type = "script"; type = "script";
script = let script = let
package = package = simplePipeline.packages."gitlab-ci:pipeline:test:job-deps:test";
(cilib.mkCI {
pipelines."test" = {
variables.EXAMPLE = "${pkgs.hello}";
};
}).packages."gitlab-ci:pipeline:test";
in in
# sh # sh
'' ''
${ntlib.helpers.path [pkgs.gnugrep]} ${ntlib.helpers.path [pkgs.gnugrep]}
${ntlib.helpers.scriptHelpers} ${ntlib.helpers.scriptHelpers}
assert_file_contains ${package} '[".pre",".post"]' assert_file_contains ${package} ':$PATH'
assert_file_contains ${package} '"EXAMPLE":"/nix/store/.*-hello-.*"' assert_file_contains ${package} 'HELLO="/nix/store/.*-hello-.*"'
''; '';
} }
]; ];