Files
hercules-hyperion/scripts/lib/common.sh
Yvan Janssens 95186bbb94
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Generalize Debian distro CI packaging
2026-05-05 11:54:48 +02:00

120 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
log() {
printf '[%s] %s\n' "$(basename "$0")" "$*"
}
die() {
printf '[%s] %s\n' "$(basename "$0")" "$*" >&2
exit 1
}
detect_debian_distribution_id() {
local os_release="/etc/os-release"
local distro_id=""
local lsb_id=""
if [[ -r "$os_release" ]]; then
# shellcheck disable=SC1090
. "$os_release"
distro_id="${ID:-}"
fi
if [[ -n "$distro_id" ]]; then
case "$distro_id" in
ubuntu|debian)
printf '%s\n' "$distro_id"
return 0
;;
esac
fi
if command -v lsb_release >/dev/null 2>&1; then
lsb_id="$(lsb_release -is | tr '[:upper:]' '[:lower:]')"
case "$lsb_id" in
ubuntu|debian)
printf '%s\n' "$lsb_id"
return 0
;;
esac
fi
die "Unable to determine Debian/Ubuntu distribution ID from $os_release"
}
detect_debian_distribution() {
local os_release="/etc/os-release"
local distro_id=""
local version_codename=""
local ubuntu_codename=""
if [[ -r "$os_release" ]]; then
# shellcheck disable=SC1090
. "$os_release"
distro_id="${ID:-}"
version_codename="${VERSION_CODENAME:-}"
ubuntu_codename="${UBUNTU_CODENAME:-}"
fi
case "$distro_id" in
ubuntu)
if [[ -n "$ubuntu_codename" ]]; then
printf '%s\n' "$ubuntu_codename"
return 0
fi
;;
debian)
if [[ -n "$version_codename" ]]; then
printf '%s\n' "$version_codename"
return 0
fi
;;
esac
if [[ -n "$version_codename" ]]; then
printf '%s\n' "$version_codename"
return 0
fi
if command -v lsb_release >/dev/null 2>&1; then
lsb_release -cs
return 0
fi
die "Unable to determine Debian/Ubuntu distribution codename from $os_release"
}
readonly REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
readonly BUILD_CONFIG_FILE="${BUILD_CONFIG_FILE:-$REPO_ROOT/build.config}"
readonly DEFAULT_GITEA_BASE_URL="https://git.cyber.gent"
readonly DEFAULT_DEBIAN_DISTRIBUTION="$(detect_debian_distribution)"
readonly DEFAULT_DEBIAN_COMPONENT="main"
readonly DEFAULT_CI_WORK_ROOT="$REPO_ROOT/.ci-work"
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Required command not found: $1"
}
require_env() {
local name="$1"
[[ -n "${!name:-}" ]] || die "Required environment variable is missing: $name"
}
load_build_config() {
[[ -f "$BUILD_CONFIG_FILE" ]] || die "Build config not found: $BUILD_CONFIG_FILE"
set -a
# shellcheck disable=SC1090
. "$BUILD_CONFIG_FILE"
set +a
[[ -n "${REPO:-}" ]] || die "REPO is missing in $BUILD_CONFIG_FILE"
[[ -n "${BRANCH:-}" ]] || die "BRANCH is missing in $BUILD_CONFIG_FILE"
}
ci_work_root() {
printf '%s\n' "${CI_WORK_ROOT:-$DEFAULT_CI_WORK_ROOT}"
}