From 5e2db8e65565a57aa31f1aa6726b884b2e43486a Mon Sep 17 00:00:00 2001 From: Kat Inskip Date: Fri, 18 Oct 2024 18:23:41 -0400 Subject: [PATCH] feat: actions test --- ci/flake-cron.nix | 2 +- packages/nf-actions-test/actions-test.sh | 85 ++++++++++++++++++++++++ packages/nf-actions-test/default.nix | 22 ++++++ packages/nf-update/default.nix | 1 + 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 packages/nf-actions-test/actions-test.sh create mode 100644 packages/nf-actions-test/default.nix diff --git a/ci/flake-cron.nix b/ci/flake-cron.nix index 84f79f93..70d296cf 100644 --- a/ci/flake-cron.nix +++ b/ci/flake-cron.nix @@ -38,7 +38,7 @@ with lib; { name = "flake update build"; order = 500; run = "nix run .#nf-update"; - env = { + env = { CACHIX_SIGNING_KEY = "\${{ secrets.CACHIX_SIGNING_KEY }}"; DISCORD_WEBHOOK_LINK = "\${{ secrets.DISCORD_WEBHOOK_LINK }}"; NF_UPDATE_GIT_COMMIT = "1"; diff --git a/packages/nf-actions-test/actions-test.sh b/packages/nf-actions-test/actions-test.sh new file mode 100644 index 00000000..8a4d545a --- /dev/null +++ b/packages/nf-actions-test/actions-test.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +set -eu + +if [[ ${GITHUB_ACTIONS-} = true && ${RUNNER_NAME-} = "Github Actions"* ]]; then + # low disk space available on public runners... + echo "enabled GC between builds due to restricted disk space..." >&2 + export NF_ACTIONS_TEST_GC=1 +fi + +NIX_BUILD_ARGS=( + --show-trace +) +NIX_BUILD_ARGS_ASYNC=() + +init_nfargs() { + nflinksuffix="$1" + shift + + nfargs=( + "${NIX_BUILD_ARGS[@]}" + ) + + if [[ -n "${NF_ACTIONS_TEST_OUTLINK-}" || -n "${NF_UPDATE_CACHIX_PUSH-}" ]]; then + nfargs+=( + -o "${NF_ACTIONS_TEST_OUTLINK-result}$nflinksuffix" + ) + else + nfargs+=( + --no-link + ) + fi +} + +nfgc() { + if [[ -n ${NF_ACTIONS_TEST_GC-} ]]; then + if [[ -n ${NF_UPDATE_CACHIX_PUSH-} ]]; then + cachix push gensokyo-infrastructure "./${NF_ACTIONS_TEST_OUTLINK-result}$nflinksuffix"*/ + rm -f "./${NF_ACTIONS_TEST_OUTLINK-result}$nflinksuffix"* + fi + nix-collect-garbage -d + fi +} + +for nfsystem in "${NF_NIX_SYSTEMS[@]}"; do + nfinstallable="${NF_CONFIG_ROOT}#nixosConfigurations.${nfsystem}.config.system.build.toplevel" + init_nfargs "-$nfsystem" + + nfwarn= + if [[ " ${NF_NIX_SYSTEMS_WARN[*]} " = *" $nfsystem "* ]]; then + nfwarn=1 + fi + + if [[ -n ${NF_ACTIONS_TEST_ASYNC-} && -z $nfwarn ]]; then + NIX_BUILD_ARGS_ASYNC+=("$nfinstallable") + continue + fi + + echo "building ${nfsystem}..." >&2 + echo >&2 + + nfbuildexit=0 + nix build "$nfinstallable" \ + "${nfargs[@]}" \ + "$@" || nfbuildexit=$? + + if [[ $nfbuildexit -ne 0 ]]; then + if [[ -n $nfwarn ]]; then + echo "build failure allowed for ${nfsystem}, ignoring..." >&2 + continue + fi + exit $nfbuildexit + fi + + nfgc +done + +if [[ -n ${NF_ACTIONS_TEST_ASYNC-} ]]; then + init_nfargs "" + nix build \ + "${nfargs[@]}" \ + "${NIX_BUILD_ARGS_ASYNC[@]}" \ + "$@" + + nfgc +fi diff --git a/packages/nf-actions-test/default.nix b/packages/nf-actions-test/default.nix new file mode 100644 index 00000000..cd9187f9 --- /dev/null +++ b/packages/nf-actions-test/default.nix @@ -0,0 +1,22 @@ +{ + wrapShellScriptBin, + pkgs, + inputs +}: let + exports = '' + export NF_CONFIG_ROOT=''${NF_CONFIG_ROOT-${toString ../.}} + ''; + exportsSystems = let + inherit (inputs.self) systems; + nixosSystems = set.filter (_: system: system.ci.enable) systems; + warnSystems = set.filter (_: system: system.ci.allowFailure) nixosSystems; + toSystems = systems: string.concatMapSep " " string.escapeShellArg (set.keys systems); + in '' + NF_NIX_SYSTEMS=(${toSystems nixosSystems}) + NF_NIX_SYSTEMS_WARN=(${toSystems warnSystems}) + ''; +in pkgs.writeShellScriptBin "nf-actions-test" '' + ${exports} + ${exportsSystems} + source ${./actions-test.sh} +'' diff --git a/packages/nf-update/default.nix b/packages/nf-update/default.nix index e3988259..9cd0c0f5 100644 --- a/packages/nf-update/default.nix +++ b/packages/nf-update/default.nix @@ -8,5 +8,6 @@ wrapShellScriptBin "nf-update" ./update.sh { cachix jq curl + nf-actions-test ]; }