#!/bin/sh # Publishes an image already built and tried (SPEC 15.5, ADR 0002 and 0020). # Usage: publish.sh # From the environment: REGISTRY, REGISTRY_USER, REGISTRY_TOKEN (write:package). The token reaches docker only on its # input and stays in a temporary folder removed at the end. set -eu image="$1" tag="$2" root=$(cd "$(dirname "$0")/../.." && pwd) fail() { echo "Pubblicazione non riuscita: $1" >&2 exit 1 } printf '%s\n' "$tag" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?$' || fail "il tag '$tag' non รจ una versione (vX.Y.Z)." version=${tag#v} expected=$(sed -n 's:.*\(.*\).*:\1:p' "$root/Directory.Build.props") [ "${version%%-*}" = "$expected" ] || fail "il tag $tag non corrisponde alla versione $expected di Directory.Build.props." [ -n "${REGISTRY:-}" ] || fail "manca REGISTRY." [ -n "${REGISTRY_USER:-}" ] || fail "manca REGISTRY_USER." [ -n "${REGISTRY_TOKEN:-}" ] || fail "manca il segreto REGISTRY_TOKEN (token Gitea con il permesso write:package)." name="$REGISTRY/balbosoft/anubis" # latest moves only for the highest plain version among the tags of the repository. latest="" case "$version" in *-*) ;; *) tags=$(git -C "$root" ls-remote --tags --refs origin 'v*') || fail "non riesco a leggere i tag del repository." newest=$(printf '%s\n' "$tags" "x refs/tags/$tag" | sed -n 's:.*refs/tags/v\([0-9]*\.[0-9]*\.[0-9]*\)$:\1:p' | sort -t. -k1,1n -k2,2n -k3,3n | tail -n 1) if [ "$newest" = "$version" ]; then latest=1; fi ;; esac DOCKER_CONFIG=$(mktemp -d) export DOCKER_CONFIG trap 'rm -rf "$DOCKER_CONFIG"' EXIT printf '%s' "$REGISTRY_TOKEN" | docker login "$REGISTRY" --username "$REGISTRY_USER" --password-stdin docker tag "$image" "$name:$version" docker push "$name:$version" if [ -n "$latest" ]; then docker tag "$image" "$name:latest" docker push "$name:latest" fi echo "Pubblicata $name:$version${latest:+ (e latest)}."