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:
technofab 2024-10-13 18:34:07 +02:00
parent 532fb8002c
commit 25e5b44a6d

View file

@ -165,6 +165,17 @@
prependToBeforeScript = prepend "before_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;
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
@ -173,15 +184,8 @@
# we can support different architectures between runners (eg. the arch of the initial runner does not matter)
jobsMappedForDeps =
mapAttrs (key: job: let
variablesWithStorePaths =
lib.concatMapAttrs (
name: value:
lib.optionalAttrs (lib.hasInfix "/nix/store/" value) {
${name} = value;
}
)
(job.variables or {});
variableExports = lib.concatMapStrings (x: "${x}\n") (
variablesWithStorePaths = filterJobVariables true job;
variableExports = lib.concatLines (
lib.mapAttrsToList (name: value: "export ${name}=\"${value}\"") variablesWithStorePaths
);
in {
@ -194,9 +198,26 @@
jobs;
# allows the user to directly run the script
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}";
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;
# 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 {
image = job.image;
variables =
lib.concatMapAttrs (name: value:
lib.optionalAttrs (!lib.hasInfix "/nix/store/" value) {
${name} = value;
})
(job.variables or {})
(filterJobVariables false job)
// lib.optionalAttrs job.nix.disable-cache {
NIX_CI_DISABLE_CACHE = "yes";
};