mirror of
https://gitlab.com/TECHNOFAB/nix-gitlab-ci.git
synced 2025-12-12 02:00:13 +01:00
Merge branch 'fix-job-patch' into 'main'
fix: weird jobs patching See merge request TECHNOFAB/nix-gitlab-ci!15
This commit is contained in:
commit
8de4594629
5 changed files with 195 additions and 61 deletions
|
|
@ -55,3 +55,5 @@ stages:
|
||||||
- build-images
|
- build-images
|
||||||
- build
|
- build
|
||||||
- trigger
|
- trigger
|
||||||
|
variables:
|
||||||
|
NIX_CI_IMAGE: $CI_REGISTRY_IMAGE/nix-ci:$CI_COMMIT_SHORT_SHA
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ in rec {
|
||||||
filterJobVariables = shouldContain: job:
|
filterJobVariables = shouldContain: job:
|
||||||
concatMapAttrs (
|
concatMapAttrs (
|
||||||
name: value:
|
name: value:
|
||||||
optionalAttrs ((hasInfix "/nix/store/" value) == shouldContain) {
|
optionalAttrs ((hasInfix builtins.storeDir value) == shouldContain) {
|
||||||
${name} = value;
|
${name} = value;
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
helpers,
|
helpers,
|
||||||
}: let
|
}: let
|
||||||
inherit (lib) toList optionalAttrs optional;
|
inherit (lib) toList optionalAttrs optional;
|
||||||
inherit (helpers) prependToBeforeScript appendToAfterScript filterJobVariables;
|
inherit (helpers) prependToBeforeScript appendToAfterScript;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
key,
|
key,
|
||||||
|
|
@ -11,16 +11,13 @@ in
|
||||||
pipelineName,
|
pipelineName,
|
||||||
nixConfig,
|
nixConfig,
|
||||||
}:
|
}:
|
||||||
(builtins.removeAttrs job ["variables" "cache"])
|
if ! nixConfig.enable then job else
|
||||||
// (optionalAttrs nixConfig.enable (
|
(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
|
||||||
// optionalAttrs nixConfig.enable (
|
variables = job.variables or {} //
|
||||||
(let
|
optionalAttrs nixConfig.enableRunnerCache {
|
||||||
variables =
|
|
||||||
(filterJobVariables false job)
|
|
||||||
// optionalAttrs nixConfig.enableRunnerCache {
|
|
||||||
NIX_CI_CACHE_STRATEGY = "runner";
|
NIX_CI_CACHE_STRATEGY = "runner";
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
|
|
@ -29,15 +26,13 @@ in
|
||||||
inherit variables;
|
inherit variables;
|
||||||
})
|
})
|
||||||
// (let
|
// (let
|
||||||
cache =
|
cache = (toList (job.cache or [])) ++
|
||||||
(toList (job.cache or []))
|
(optional nixConfig.enableRunnerCache {
|
||||||
++ (optional nixConfig.enableRunnerCache {
|
|
||||||
key = nixConfig.runnerCacheKey;
|
key = nixConfig.runnerCacheKey;
|
||||||
paths = [".nix-cache/"];
|
paths = [ ".nix-cache/" ];
|
||||||
});
|
});
|
||||||
in
|
in
|
||||||
# filter empty cache
|
# filter empty cache
|
||||||
optionalAttrs (cache != []) {
|
optionalAttrs (cache != []) {
|
||||||
inherit cache;
|
inherit cache;
|
||||||
})
|
})
|
||||||
)
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ in
|
||||||
# the child pipeline can then use the built images to test them
|
# the child pipeline can then use the built images to test them
|
||||||
extraData = {
|
extraData = {
|
||||||
stages = ["build-images" "build" "trigger"];
|
stages = ["build-images" "build" "trigger"];
|
||||||
|
variables.NIX_CI_IMAGE = "$CI_REGISTRY_IMAGE/nix-ci:$CI_COMMIT_SHORT_SHA";
|
||||||
"build:image" = {
|
"build:image" = {
|
||||||
stage = "build-images";
|
stage = "build-images";
|
||||||
parallel.matrix = [
|
parallel.matrix = [
|
||||||
|
|
|
||||||
|
|
@ -56,24 +56,59 @@
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
name = "jobPatched nix disabled";
|
name = "jobPatched nix disabled";
|
||||||
expected = {};
|
expected = {
|
||||||
|
cache = {
|
||||||
|
name = "some";
|
||||||
|
paths = [ "this" ];
|
||||||
|
};
|
||||||
|
variables = {
|
||||||
|
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.enable = false;
|
nixConfig.enable = false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
name = "jobPatched without runner cache";
|
name = "jobPatched without runner cache";
|
||||||
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;
|
||||||
|
|
@ -83,20 +118,34 @@
|
||||||
{
|
{
|
||||||
name = "jobPatched with runner cache";
|
name = "jobPatched with runner cache";
|
||||||
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 = {
|
||||||
variables."NIX_CI_CACHE_STRATEGY" = "runner";
|
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;
|
||||||
|
|
@ -129,7 +178,7 @@
|
||||||
# sh
|
# sh
|
||||||
''
|
''
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
${ntlib.helpers.path [pkgs.jq pkgs.gnugrep pkgs.coreutils]}
|
${ntlib.helpers.path (with pkgs; [jq gnugrep coreutils])}
|
||||||
echo "two keys, one json one pretty"
|
echo "two keys, one json one pretty"
|
||||||
jq 'keys | length == 2' "${pipeline}" | grep -q true
|
jq 'keys | length == 2' "${pipeline}" | grep -q true
|
||||||
echo "key[0] is exactly 'gitlab-ci:pipeline:test'"
|
echo "key[0] is exactly 'gitlab-ci:pipeline:test'"
|
||||||
|
|
@ -145,20 +194,107 @@
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
name = "handle store paths in variables";
|
name = "handle store paths in variables without nix config";
|
||||||
expected = {
|
expected = {
|
||||||
stages = ["test"];
|
stages = [ "test" ];
|
||||||
test.stage = "test";
|
variables = {
|
||||||
|
EXAMPLE = "empty";
|
||||||
|
CURL = toString pkgs.curl;
|
||||||
};
|
};
|
||||||
actual =
|
test = {
|
||||||
(mkPipeline {
|
stage = "test";
|
||||||
|
before_script = [ "./init" ];
|
||||||
|
script = [ "echo Hello World!" ];
|
||||||
|
after_script = [ "./clean" ];
|
||||||
|
cache = {
|
||||||
|
key = "simple";
|
||||||
|
paths = [ "~/random/" ];
|
||||||
|
};
|
||||||
|
variables = {
|
||||||
|
SAMPLE = "working";
|
||||||
|
HELLO = toString pkgs.hello;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
actual = (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";
|
||||||
variables."TEST" = "${pkgs.hello}";
|
before_script = [ "./init" ];
|
||||||
|
script = [ "echo Hello World!" ];
|
||||||
|
after_script = [ "./clean" ];
|
||||||
|
cache = {
|
||||||
|
key = "simple";
|
||||||
|
paths = [ "~/random/" ];
|
||||||
|
};
|
||||||
|
variables = {
|
||||||
|
SAMPLE = "working";
|
||||||
|
HELLO = toString pkgs.hello;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}).finalConfig;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "handle store paths in variables with nix config";
|
||||||
|
expected = {
|
||||||
|
stages = [ "test" ];
|
||||||
|
variables = {
|
||||||
|
EXAMPLE = "empty";
|
||||||
|
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 {
|
||||||
|
name = "test";
|
||||||
|
nixConfig = {
|
||||||
|
enable = true;
|
||||||
|
enableRunnerCache = true;
|
||||||
|
runnerCacheKey = "random";
|
||||||
|
};
|
||||||
|
pipeline = {
|
||||||
|
stages = [ "test" ];
|
||||||
|
variables = {
|
||||||
|
EXAMPLE = "empty";
|
||||||
|
CURL = toString pkgs.curl;
|
||||||
|
};
|
||||||
|
jobs.test = {
|
||||||
|
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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue