From 1b8a742563a767b925b6990a4e7c00d2d39f7d02 Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Mon, 18 Nov 2024 22:26:17 -0600 Subject: [PATCH] Streamline generating continue file Fixes #29 --- flake.nix | 16 +++----- nix/cache.nix | 32 ++++++++-------- nix/continue.nix | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ nix/validate.nix | 16 ++++---- 4 files changed, 128 insertions(+), 34 deletions(-) create mode 100644 nix/continue.nix diff --git a/flake.nix b/flake.nix index 3c106b7..b8e109f 100644 --- a/flake.nix +++ b/flake.nix @@ -168,19 +168,15 @@ // uploads // cibuilds // { - continue = pkgs.writeText "continue.yml" ( - builtins.replaceStrings - [ - "QEMU_MATRIX" - "VBOX_MATRIX" - "HYPERV_MATRIX" - ] - [ + continue = ( + import ./nix/continue.nix { + inherit + pkgs qemu_list vbox_list hyperv_list - ] - (builtins.readFile ./.gitlab-ci-build.yml.in) + ; + } ); cache = pkgs.callPackage (import ./nix/cache.nix) { inherit active distroFile; }; validate = pkgs.callPackage (import ./nix/validate.nix) { }; diff --git a/nix/cache.nix b/nix/cache.nix index 5ddc4ed..6b0c4c5 100644 --- a/nix/cache.nix +++ b/nix/cache.nix @@ -22,26 +22,26 @@ writeShellApplication { ]; text = '' - push="''${1:-yes}" - if [ "$push" == "yes" ]; then - mc alias set storage "$STORAGE_URL" root "$MINIO_SECRET" - fi - mkdir -p cache + push="''${1:-yes}" + if [ "$push" == "yes" ]; then + mc alias set storage "$STORAGE_URL" root "$MINIO_SECRET" + fi + mkdir -p cache '' + (concatStringsSep "\n" ( map (d: '' - url=$(echo var.iso.url | packer console -var-file="${distroFile d}" -config-type=hcl2 sources/) - sha=$(echo var.iso.checksum | packer console -var-file="${distroFile d}" -config-type=hcl2 sources/) - distro=$(echo var.distro | packer console -var-file="${distroFile d}" -config-type=hcl2 sources/) - # shellcheck disable=SC2143 - if [ -z "$(mc ls "storage/isos/cache/$distro" | grep "$sha")" ]; then - curl -L --retry 3 --retry-all-errors -o "cache/$sha.iso" "$url" - if [ "$push" == "yes" ]; then - mc put "cache/$sha.iso" "storage/isos/cache/$distro/$sha.iso" - fi - else - echo "Skipping ${d.distro} - cache/$sha.iso" + url=$(echo var.iso.url | packer console -var-file="${distroFile d}" -config-type=hcl2 sources/) + sha=$(echo var.iso.checksum | packer console -var-file="${distroFile d}" -config-type=hcl2 sources/) + distro=$(echo var.distro | packer console -var-file="${distroFile d}" -config-type=hcl2 sources/) + # shellcheck disable=SC2143 + if [ -z "$(mc ls "storage/isos/cache/$distro" | grep "$sha")" ]; then + curl -L --retry 3 --retry-all-errors -o "cache/$sha.iso" "$url" + if [ "$push" == "yes" ]; then + mc put "cache/$sha.iso" "storage/isos/cache/$distro/$sha.iso" fi + else + echo "Skipping ${d.distro} - cache/$sha.iso" + fi '') active )); } diff --git a/nix/continue.nix b/nix/continue.nix new file mode 100644 index 0000000..b4f655f --- /dev/null +++ b/nix/continue.nix @@ -0,0 +1,98 @@ +{ + pkgs, + qemu_list, + vbox_list, + hyperv_list, + ... +}: + +let + + general = { + stage = "build"; + cache = [ + { + key = { + files = [ "sources/build.pkr.hcl" ]; + }; + paths = [ "./packer_config/" ]; + } + ]; + rules = [ + { + "if" = "$CI_COMMIT_TAG"; + variables = { + EXCEPT = ""; + JOB_PREFIX = "upload"; + }; + } + { + "if" = "$CI_COMMIT_TAG =~ \"/^$/\""; + variables = { + EXCEPT = "-except=upload"; + JOB_PREIX = "ci"; + }; + } + ]; + needs = [ + { + pipeline = "$PARENT_PIPELINE_ID"; + job = "generate"; + artifacts = true; + } + ]; + script = [ + "nix run \".#\\\"\${JOB_PREIX}\${buildcmd}\\\"\" || exit 1" + ]; + }; + + variables = { + PACKER_CONFIG_DIR = "./packer_config/"; + PACKER_CACHE_DIR = "./cache/"; + PACKER_LOG = "1"; + }; + + continue = { + stages = [ + "build" + "release" + ]; + "qemu.amd64" = general // { + variables = variables // { + only = "-only=qemu.amd64"; + }; + tag = [ "qemu" ]; + parallel = { + matrix = qemu_list; + }; + before_script = [ + "echo $BUILD" + "env" + ]; + }; + "virtualbox-iso.amd64" = general // { + variables = variables // { + only = "-only=\"virtualbox-iso.amd64\""; + }; + tags = [ "vbox" ]; + parallel = { + matrix = vbox_list; + }; + }; + "hyperv-iso.amd64" = general // { + variables = variables // { + only = "-only=\"hyperv-iso.amd64\""; + }; + tags = [ "hyperv" ]; + parallel = { + matrix = hyperv_list; + }; + script = [ + "packer init sources" + "packer build -only=\"hyperv-iso.amd64\" -var-file=\"\${distro}\" -var build=\${BUILD} -var cpus=2 -var memory=4096 \${EXCEPT} sources" + "if (-not $?) { throw \"Error in build\")" + ]; + }; + }; +in +pkgs.writeText "continue.yml" (builtins.toJSON continue) diff --git a/nix/validate.nix b/nix/validate.nix index 542313d..ec7fd57 100644 --- a/nix/validate.nix +++ b/nix/validate.nix @@ -11,13 +11,13 @@ writeShellApplication { packer ]; text = '' - shopt -s globstar - if [ -e "''${PACKER_CONFIG_DIR}" ]; then - find "''${PACKER_CONFIG_DIR}" -name '*_linux_amd64' -exec chmod +x '{}' ";" - fi - packer init sources - for vars in distros/**/*.pkrvars.hcl; do - packer validate -except=upload "-var-file=''${vars}" sources/ - done + shopt -s globstar + if [ -e "''${PACKER_CONFIG_DIR}" ]; then + find "''${PACKER_CONFIG_DIR}" -name '*_linux_amd64' -exec chmod +x '{}' ";" + fi + packer init sources + for vars in distros/**/*.pkrvars.hcl; do + packer validate -except=upload "-var-file=''${vars}" sources/ + done ''; }