From d73485322345297b321fcfd02c618b88c1f2d85e Mon Sep 17 00:00:00 2001 From: technofab Date: Sat, 22 Mar 2025 21:50:33 +0100 Subject: [PATCH] chore(scripts): improve cache strategy handling, see #21 --- scripts/finalize_nix_ci.sh | 4 ++++ scripts/setup_nix_ci.sh | 5 +++++ templates/nix-gitlab-ci.yml | 11 ++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/scripts/finalize_nix_ci.sh b/scripts/finalize_nix_ci.sh index e1200c2..698b81f 100644 --- a/scripts/finalize_nix_ci.sh +++ b/scripts/finalize_nix_ci.sh @@ -6,6 +6,10 @@ echo -e "\\e[0Ksection_start:`date +%s`:finalize_nix_ci[collapsed=true]\\r\\e[0K /tmp/nix-store-before /tmp/nix-store-after) COUNT=$(${pkgs.busybox}/bin/wc -l <<<"$NEW_PATHS") + if [[ "$NIX_CI_CACHE_STRATEGY" == "auto" ]]; then + export NIX_CI_CACHE_STRATEGY="${NIX_CI_RUNNER_CACHE_STRATEGY:-${NIX_CI_DEFAULT_CACHE_STRATEGY:-none}}"; + fi + if [ -z "$NIX_CI_DISABLE_CACHE" ]; then echo -e "\\e[0Ksection_start:`date +%s`:cache_push[collapsed=true]\\r\\e[0KPushing $COUNT new store paths to cache ($NIX_CI_CACHE_STRATEGY)" echo $NEW_PATHS | { diff --git a/scripts/setup_nix_ci.sh b/scripts/setup_nix_ci.sh index d91e17d..1f2af66 100644 --- a/scripts/setup_nix_ci.sh +++ b/scripts/setup_nix_ci.sh @@ -1,6 +1,11 @@ echo -e "\\e[0Ksection_start:`date +%s`:nix_setup[collapsed=true]\\r\\e[0KSetting up Nix CI" nix path-info --all > /tmp/nix-store-before + if [[ "$NIX_CI_CACHE_STRATEGY" == "auto" ]]; then + export NIX_CI_CACHE_STRATEGY="${NIX_CI_RUNNER_CACHE_STRATEGY:-${NIX_CI_DEFAULT_CACHE_STRATEGY:-none}}"; + echo "NIX_CI_CACHE_STRATEGY was set to auto, selected '$NIX_CI_CACHE_STRATEGY' for this job" + fi + if [ -z "$NIX_CI_DISABLE_CACHE" ]; then echo -e "\\e[0Ksection_start:`date +%s`:cache_setup[collapsed=true]\\r\\e[0KConfiguring cache ($NIX_CI_CACHE_STRATEGY)" case "$NIX_CI_CACHE_STRATEGY" in diff --git a/templates/nix-gitlab-ci.yml b/templates/nix-gitlab-ci.yml index b00ac79..cc87788 100644 --- a/templates/nix-gitlab-ci.yml +++ b/templates/nix-gitlab-ci.yml @@ -3,9 +3,14 @@ spec: cache_strategy: type: string description: | - (empty for auto) | none | runner | cachix | attic - Sets the default strategy and will be overridden by $NIX_CI_CACHE_STRATEGY - default: "none" + auto (default) | none | runner | cachix | attic + Sets the default caching strategy. + - "auto": dynamically selects the best strategy for every job based on env variables + - "none": disables caching + - "runner", "cachix" & "attic": forces every job to use this strategy + + Can be overridden by setting NIX_CI_CACHE_STRATEGY in the pipeline variables. + default: "auto" cache_files: type: array description: |