feat(cache): add initial nix cache shell program for using attic with actions

This commit is contained in:
Tommy 2024-11-27 18:17:24 +01:00
parent 1103d6b4ab
commit e0a81b0b26
Signed by: tommy
SSH key fingerprint: SHA256:1LWgQT3QPHIT29plS8jjXc3S1FcE/4oGvsx3Efxs6Uc

64
apps/ci-os/packages/cache/default.nix vendored Normal file
View file

@ -0,0 +1,64 @@
{ pkgs, lib, ... }:
with pkgs;
writeShellApplication rec {
name = "attic-action";
runtimeInputs = [ attic-client ];
# Define the shell script
text = ''
#!/usr/bin/env bash
set -euo pipefail
# Default values
ATTIC_ENDPOINT="${ATTIC_ENDPOINT:-https://cache.252.no}"
ATTIC_CACHE="${ATTIC_CACHE:-nix-gitops}"
ATTIC_TOKEN="${ATTIC_TOKEN:-}"
SKIP_PUSH="${SKIP_PUSH:-false}"
SKIP_USE="${SKIP_USE:-false}"
# Ensure required inputs are provided
if [[ -z "$ATTIC_ENDPOINT" || -z "$ATTIC_CACHE" ]]; then
echo "Error: Both ATTIC_ENDPOINT and ATTIC_CACHE are required."
exit 1
fi
if [[ -z "$ATTIC_TOKEN" ]]; then
echo "Warning: ATTIC_TOKEN is not set. Operations requiring authentication might fail."
fi
# Add the cache as a substituter unless SKIP_USE is true
if [[ "$SKIP_USE" != "true" ]]; then
echo "Adding $ATTIC_ENDPOINT as a substituter..."
attic use --endpoint "$ATTIC_ENDPOINT" "$ATTIC_CACHE" || {
echo "Error: Failed to add substituter."
exit 1
}
else
echo "Skipping use of cache as substituter."
fi
# Push to the cache unless SKIP_PUSH is true
if [[ "$SKIP_PUSH" != "true" ]]; then
echo "Pushing results to $ATTIC_ENDPOINT/$ATTIC_CACHE..."
attic push --endpoint "$ATTIC_ENDPOINT" "$ATTIC_CACHE" || {
echo "Error: Failed to push to cache."
exit 1
}
else
echo "Skipping cache push."
fi
echo "Attic action completed successfully."
'';
# Metadata for the package
meta = with lib; {
homepage = "https://code.252.no/tommy/containers";
description = "Binary cache server action script for CI pipelines";
license = licenses.mit;
maintainers = with maintainers; [ "tommy-skaug" ];
};
}