mirror of
https://gitlab.com/TECHNOFAB/nix-gitlab-ci.git
synced 2025-12-12 02:00:13 +01:00
feat(module): improve ability to run jobs locally
adds the correct PATH and environment variables to run it locally similar to how it works in CI. Also split up the before_script, script and after_script with simple echo's inbetween for easier debugging
This commit is contained in:
parent
532fb8002c
commit
25e5b44a6d
1 changed files with 33 additions and 16 deletions
|
|
@ -165,6 +165,17 @@
|
||||||
prependToBeforeScript = prepend "before_script";
|
prependToBeforeScript = prepend "before_script";
|
||||||
appendToAfterScript = append "after_script";
|
appendToAfterScript = append "after_script";
|
||||||
|
|
||||||
|
# filter job's variables to either only those containing store paths
|
||||||
|
# or those that do not
|
||||||
|
filterJobVariables = nix: job:
|
||||||
|
lib.concatMapAttrs (
|
||||||
|
name: value:
|
||||||
|
lib.optionalAttrs ((lib.hasInfix "/nix/store/" value) == nix) {
|
||||||
|
${name} = value;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
(job.variables or {});
|
||||||
|
|
||||||
jobs = filterAttrsRec (n: v: v != null) config.ci.jobs;
|
jobs = filterAttrsRec (n: v: v != null) config.ci.jobs;
|
||||||
rest = filterAttrsRec (n: v: v != null) (builtins.removeAttrs config.ci ["jobs" "config"]);
|
rest = filterAttrsRec (n: v: v != null) (builtins.removeAttrs config.ci ["jobs" "config"]);
|
||||||
# this allows us to nix build this to get all the mentioned dependencies from the binary cache
|
# this allows us to nix build this to get all the mentioned dependencies from the binary cache
|
||||||
|
|
@ -173,15 +184,8 @@
|
||||||
# we can support different architectures between runners (eg. the arch of the initial runner does not matter)
|
# we can support different architectures between runners (eg. the arch of the initial runner does not matter)
|
||||||
jobsMappedForDeps =
|
jobsMappedForDeps =
|
||||||
mapAttrs (key: job: let
|
mapAttrs (key: job: let
|
||||||
variablesWithStorePaths =
|
variablesWithStorePaths = filterJobVariables true job;
|
||||||
lib.concatMapAttrs (
|
variableExports = lib.concatLines (
|
||||||
name: value:
|
|
||||||
lib.optionalAttrs (lib.hasInfix "/nix/store/" value) {
|
|
||||||
${name} = value;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
(job.variables or {});
|
|
||||||
variableExports = lib.concatMapStrings (x: "${x}\n") (
|
|
||||||
lib.mapAttrsToList (name: value: "export ${name}=\"${value}\"") variablesWithStorePaths
|
lib.mapAttrsToList (name: value: "export ${name}=\"${value}\"") variablesWithStorePaths
|
||||||
);
|
);
|
||||||
in {
|
in {
|
||||||
|
|
@ -194,9 +198,26 @@
|
||||||
jobs;
|
jobs;
|
||||||
# allows the user to directly run the script
|
# allows the user to directly run the script
|
||||||
jobsMappedForScript =
|
jobsMappedForScript =
|
||||||
mapAttrs (key: job: {
|
mapAttrs (key: job: let
|
||||||
|
variablesWithStorePaths = filterJobVariables false job;
|
||||||
|
variableExports = lib.concatLines (
|
||||||
|
lib.mapAttrsToList (name: value: "export ${name}=\"${value}\"") variablesWithStorePaths
|
||||||
|
);
|
||||||
|
in {
|
||||||
name = "gitlab-ci-job:${key}";
|
name = "gitlab-ci-job:${key}";
|
||||||
value = pkgs.writeShellScriptBin "gitlab-ci-job:${key}" (lib.strings.concatLines (job.before_script or [] ++ job.script ++ job.after_script or []));
|
value = pkgs.writeShellScriptBin "gitlab-ci-job:${key}" ''
|
||||||
|
# set up deps and environment variables containing store paths
|
||||||
|
. ${jobsMappedForDeps."gitlab-ci-job-deps:${key}"}
|
||||||
|
# normal environment variables
|
||||||
|
${variableExports}
|
||||||
|
# run before_script, script and after_script
|
||||||
|
echo -e "\e[32mRunning before_script...\e[0m"
|
||||||
|
${lib.concatLines (job.before_script or [])}
|
||||||
|
echo -e "\e[32mRunning script...\e[0m"
|
||||||
|
${lib.concatLines job.script}
|
||||||
|
echo -e "\e[32mRunning after_script...\e[0m"
|
||||||
|
${lib.concatLines (job.after_script or [])}
|
||||||
|
'';
|
||||||
})
|
})
|
||||||
jobs;
|
jobs;
|
||||||
# build the deps specific for this job before anything, this way the deps should be fetched from the cache
|
# build the deps specific for this job before anything, this way the deps should be fetched from the cache
|
||||||
|
|
@ -215,11 +236,7 @@
|
||||||
// lib.optionalAttrs job.nix.enable {
|
// lib.optionalAttrs job.nix.enable {
|
||||||
image = job.image;
|
image = job.image;
|
||||||
variables =
|
variables =
|
||||||
lib.concatMapAttrs (name: value:
|
(filterJobVariables false job)
|
||||||
lib.optionalAttrs (!lib.hasInfix "/nix/store/" value) {
|
|
||||||
${name} = value;
|
|
||||||
})
|
|
||||||
(job.variables or {})
|
|
||||||
// lib.optionalAttrs job.nix.disable-cache {
|
// lib.optionalAttrs job.nix.disable-cache {
|
||||||
NIX_CI_DISABLE_CACHE = "yes";
|
NIX_CI_DISABLE_CACHE = "yes";
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue