Files
faerro-kb/scripts/publish-image.sh
T

56 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
image="gitea.faerro.it/andrea/faerro-kb"
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
if ! command -v docker >/dev/null 2>&1; then
echo "Docker is required to build and publish the image." >&2
exit 1
fi
if ! command -v git >/dev/null 2>&1; then
echo "Git is required to determine the release version." >&2
exit 1
fi
tag="$(git -C "$repo_root" describe --tags --exact-match HEAD 2>/dev/null || true)"
version="${tag#v}"
if [[ ! "$version" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then
echo "The checked-out commit must have a SemVer tag (for example, 0.0.2 or v0.0.2)." >&2
exit 1
fi
cd "$repo_root"
docker build --tag "$image:$version" --tag "$image:latest" .
registry_username="${GITEA_USERNAME:-${DOCKER_USERNAME:-}}"
registry_token="${GITEA_TOKEN:-${DOCKER_PASSWORD:-}}"
unset GITEA_TOKEN DOCKER_PASSWORD
env_file="$repo_root/.env"
if [[ -f "$env_file" ]]; then
while IFS= read -r line || [[ -n "$line" ]]; do
line="${line%$'\r'}"
if [[ "$line" =~ ^GITEA_USERNAME=(.*)$ ]]; then
registry_username="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^GITEA_TOKEN=(.*)$ ]]; then
registry_token="${BASH_REMATCH[1]}"
fi
done < "$env_file"
fi
if [[ -n "$registry_username" || -n "$registry_token" ]]; then
if [[ -z "$registry_username" || -z "$registry_token" ]]; then
echo "Set both GITEA_USERNAME and GITEA_TOKEN in .env to log in automatically." >&2
exit 1
fi
if ! printf '%s' "$registry_token" | docker login --username "$registry_username" --password-stdin gitea.faerro.it; then
unset registry_token
exit 1
fi
unset registry_token
fi
docker push "$image:$version"
docker push "$image:latest"
echo "Published $image:$version and $image:latest"