openhab-docker/helper-functions

245 lines
7.7 KiB
Bash

#!/bin/bash
set -eo pipefail
BUILDER=""
DISTRO_REPO="${DISTRO_REPO:=openhab/openhab-distro}"
VERSIONS=""
get_released_versions_from_tags() {
git ls-remote --refs --tags "https://github.com/${DISTRO_REPO}.git" | grep -E '.+/tags/[0-9]+\.[0-9]+\.[0-9]+(\.(M|RC)[0-9]+)?$' | sed -E 's#.+/tags/(.+)#\1#g' || echo ""
}
get_snapshot_versions_from_poms() {
local branch_names="$(git ls-remote --refs --heads "https://github.com/${DISTRO_REPO}.git" | grep -E '.+/heads/(main)$' | sed -E 's#.+/heads/(.+)#\1#g')"
for branch_name in $branch_names; do
curl -sS "https://raw.githubusercontent.com/${DISTRO_REPO}/${branch_name}/pom.xml" | grep -E '^ <version>' | grep 'SNAPSHOT' | sed -E 's#.+<version>(.+)-SNAPSHOT</version>#\1-snapshot#g' || echo ""
done
}
get_versions() {
(get_released_versions_from_tags && get_snapshot_versions_from_poms) | sort --unique --version-sort
}
VERSIONS=$(get_versions)
# Supported base images
bases() {
echo "debian alpine"
}
docker_repo() {
echo "${DOCKER_REPO:=openhab/openhab}"
}
# Supported Docker platforms
platforms() {
local version="$1"
local base="$2"
if [[ "$version" =~ ^4.*$ ]] && [ "$base" == "debian" ]; then
echo "linux/amd64,linux/arm64,linux/arm/v7"
else
# There are no linux/arm/v7 images for openHAB 5 (or newer) because this platform is no longer supported.
# There are no linux/arm/v7 Alpine images for openHAB 3 (or newer) because the required openjdk package is unavailable for this platform.
echo "linux/amd64,linux/arm64"
fi
}
tags() {
local version="$1"
local base="$2"
local tags=()
if [ "$base" == "debian" ]; then
tags+=("$(docker_repo):$version")
fi
tags+=("$(docker_repo):$version-$base")
if [ "$version" == "$(last_stable_version)" ]; then
if [ "$base" == "debian" ]; then
tags+=("$(docker_repo):latest")
fi
tags+=("$(docker_repo):latest-$base")
fi
milestone_maturity_version="$(last_milestone_version)"
if [ "$milestone_maturity_version" == "" ]; then
milestone_maturity_version="$(last_stable_version)"
fi
if [ "$version" == "$milestone_maturity_version" ]; then
if [ "$base" == "debian" ]; then
tags+=("$(docker_repo):milestone")
fi
tags+=("$(docker_repo):milestone-$base")
fi
if [ "$version" == "$(last_snapshot_version)" ]; then
if [ "$base" == "debian" ]; then
tags+=("$(docker_repo):snapshot")
fi
tags+=("$(docker_repo):snapshot-$base")
fi
echo $(IFS=' '; echo "${tags[*]}")
}
last_stable_version() {
grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' <<< $VERSIONS | tail -n 1
}
stable_versions() {
grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' <<< $VERSIONS
}
last_stable_minor_versions() {
local minor_versions=$(grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' <<< $VERSIONS | sed -E 's/^([0-9]+\.[0-9]+).+/\1/' | sort --unique --version-sort)
for minor_version in $minor_versions; do
stable_versions | grep -E "^$minor_version" | tail -n 1
done
}
snapshot_versions() {
grep -E '^[0-9]+\.[0-9]+\.[0-9]+-snapshot$' <<< $VERSIONS || echo ""
}
last_snapshot_version() {
grep -E '^[0-9]+\.[0-9]+\.[0-9]+-snapshot$' <<< $VERSIONS | tail -n 1 || echo ""
}
next_stable_version() {
sed 's/-snapshot//' <<< $(last_snapshot_version)
}
milestone_versions() {
grep -E "$(next_stable_version)\.(M|RC)[0-9]+$" <<< $VERSIONS | tail -n 3 || echo ""
}
last_milestone_version() {
grep -E "$(next_stable_version)\.(M|RC)[0-9]+$" <<< $VERSIONS | tail -n 1 || echo ""
}
generate_readme_versions() {
local text="$1"
local versions="$(echo "$2" | sort --reverse --version-sort | head -n 2)"
if [ "$versions" != "" ]; then
echo "* $text"
for version in $versions; do
case $version in
*-snapshot) echo " * \`$version\`";;
*) echo " * \`$version\` ([Release notes](https://github.com/openhab/openhab-distro/releases/tag/$version))";;
esac
done
fi
}
update_readme() {
local file=README.md
if [ "$(last_stable_version)" == "" ]; then
echo "Cannot update $file because the last stable version is unknown!"
exit 1
fi
local generate="false"
while IFS= read -r line
do
if [[ $line =~ ^\*\ \*\*Stable:\*\*.+$ ]]; then
generate="true"
else
if [ "$generate" == "true" ]; then
if [ "$line" == "" ]; then
generate="false"
generate_readme_versions \
'**Stable:** Thoroughly tested semi-annual official releases of openHAB. Use the stable version for your production environment if you do not need the latest enhancements and prefer a robust system.' \
"$(last_stable_minor_versions)"
generate_readme_versions \
'**Milestone:** Intermediary releases of the next openHAB version which are released about once a month. They include recently added features and bugfixes and are a good compromise between the current stable version and the bleeding-edge and potentially unstable snapshot version.' \
"$(milestone_versions)"
generate_readme_versions \
'**Snapshot:** Usually 1 or 2 days old and include the latest code. Use these for testing out very recent changes using the latest code. Be aware that some snapshots might be unstable so use these in production at your own risk!' \
"$(snapshot_versions)"
echo
fi
else
echo "$line"
fi
fi
done < $file > $file.new && mv $file.new $file
sed -i "s#version-[0-9]*\.[0-9]*\.[0-9]*-blue#version-$(last_stable_version)-blue#g" $file
sed -i "s#openhab/tags?name=[0-9]*\.[0-9]*\.[0-9]*#openhab/tags?name=$(last_stable_version)#g" $file
sed -i "s#openhab/openhab:[0-9]*\.[0-9]*\.[0-9]*#openhab/openhab:$(last_stable_version)#g" $file
sed -i "s#OPENHAB_VERSION=[0-9]*\.[0-9]*\.[0-9]*#OPENHAB_VERSION=$(last_stable_version)#g" $file
}
validate_readme_constraints() {
local count=$(wc -m <README.md)
if [ $count -gt 25000 ]; then
echo "README.md contains $count characters which exceeds the 25000 character limit of Docker Hub" >&2
exit 1
else
echo "README.md contains $count characters which is below the 25000 character limit of Docker Hub"
fi
}
update_dockerhub_readme() {
if [ "$(docker info 2>&1 | grep 'pushrm: Push Readme to container registry' | wc -l)" -eq "0" ]; then
# The pushrm plugin is available from https://github.com/christian-korneck/docker-pushrm
echo "Failed to update README for $(docker_repo) on Docker Hub (pushrm Docker CLI plugin not installed)" >&2
exit 1
fi
if docker pushrm $(docker_repo); then
echo "Successfully updated README for $(docker_repo) on Docker Hub"
else
echo "Failed to update README for $(docker_repo) on Docker Hub" >&2
exit 1
fi
}
prepare_builder() {
if [ "$BUILDER" == "" ]; then
docker run --privileged --rm tonistiigi/binfmt:qemu-v8.1.5 --install all &> /dev/null
(docker buildx inspect builder &> /dev/null && echo -e "\nReusing existing builder") || \
(docker buildx create --name builder --use &> /dev/null && echo -e "\nCreated builder")
BUILDER="builder"
fi
}
build() {
prepare_builder
local openhab_version="${1/SNAPSHOT/snapshot}"
local dist="$2"
local push="$3"
local java_version=""
case $openhab_version in
4.*) java_version="17";;
*) java_version="21";;
esac
local build_arg_options="--build-arg BUILD_DATE=$(date +"%Y-%m-%dT%H:%M:%SZ") --build-arg VCS_REF=$(git rev-parse HEAD) --build-arg JAVA_VERSION=$java_version --build-arg OPENHAB_VERSION=$openhab_version"
local tags=$(tags $openhab_version $dist)
local tag_options=${tags//$(docker_repo)/--tag $(docker_repo)}
local output_options="--output type=image,oci-mediatypes=false,push=$([ "$push" == "--push" ] && echo true || echo false)"
local build_options="$build_arg_options --platform $(platforms $openhab_version $dist) $tag_options $output_options --progress plain --provenance=false"
local dockerfile_path="./$dist"
local build_command="docker buildx build $build_options $dockerfile_path"
echo
echo "Building openHAB $openhab_version $dist Docker image"
echo
set -x
$build_command
{ set +x; } 2> /dev/null
echo
}