fukuops/scripts/hbd.sh

112 lines
3.1 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
USAGE="Usage: $0 <chart-name> [version] [--oci-register <registry>]"
function usage() {
echo "$USAGE"
exit 1
}
function next_patch_version() {
local chart="$1"
local ver
ver=$(yq '.version' "$chart/Chart.yaml")
if [[ -z "$ver" || "$ver" == "null" ]]; then
echo "Error: Could not find version in $chart/Chart.yaml" >&2
exit 1
fi
IFS='.' read -r major minor patch <<< "$ver"
echo "$major.$minor.$((patch + 1))"
}
function build_and_push() {
local chart_name="$1"
local version="$2"
local oci_registry="${3:-oci://git.roboces.dev/catalin/fukuops}"
local chart_path="k8s/charts/$chart_name"
if [[ ! -d "$chart_path" ]]; then
echo "Error: Chart '$chart_name' not found at $chart_path" >&2
exit 1
fi
if [[ ! -f "$chart_path/Chart.yaml" ]]; then
echo "Error: Chart.yaml not found in $chart_path" >&2
exit 1
fi
echo "Building and pushing $chart_name version $version to $oci_registry"
local current_version
current_version=$(yq -r '.version' "$chart_path/Chart.yaml")
yq -y -i ".version = \"$version\"" "$chart_path/Chart.yaml"
mkdir -p /tmp/helm-pkg/
helm package "$chart_path" -d /tmp/helm-pkg/
helm push "/tmp/helm-pkg/${chart_name}-${version}.tgz" "$oci_registry"
yq -y -i ".version = \"$current_version\"" "$chart_path/Chart.yaml"
echo "Successfully pushed $chart_name-$version to $oci_registry"
}
function main() {
if [[ $# -lt 1 ]]; then
usage
fi
local chart_name=""
local version=""
local oci_registry="oci://git.roboces.dev/catalin/fukuops"
while [[ $# -gt 0 ]]; do
case "$1" in
--oci-registry)
if [[ -z "${2:-}" ]] || [[ "$2" == -* ]]; then
echo "Error: --oci-registry requires a registry argument" >&2
exit 1
fi
oci_registry="$2"
shift 2
;;
-h|--help)
echo "$USAGE"
echo ""
echo "Options:"
echo " --oci-registry <registry> Override default OCI registry (default: oci://git.roboces.dev/catalin/fukuops)"
exit 0
;;
-*)
echo "Error: Unknown option: $1" >&2
usage
;;
*)
if [[ -z "$chart_name" ]]; then
chart_name="$1"
elif [[ -z "$version" ]]; then
version="$1"
else
echo "Error: Too many arguments" >&2
usage
fi
shift
;;
esac
done
if [[ -z "$chart_name" ]]; then
echo "Error: Chart name is required" >&2
usage
fi
if [[ -z "$version" ]]; then
version=$(next_patch_version "k8s/charts/$chart_name")
echo "No version provided, calculated next patch version: $version"
fi
build_and_push "$chart_name" "$version" "$oci_registry"
}
main "$@"