#!/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" # The image says what it was made from (its labels): only what comes from the commit of the tag is published as that tag. # Without the tag here, as in the CI, the repository on Gitea says its commit: the line with ^{} for an annotated tag. commit=$(git -C "$root" rev-parse --verify --quiet "refs/tags/$tag^{commit}") || { found=$(git -C "$root" ls-remote origin "refs/tags/$tag" "refs/tags/$tag^{}") || fail "non riesco a leggere i tag del repository." commit=$(printf '%s\n' "$found" | sort -k 2 | tail -n 1 | cut -f 1) } [ -n "$commit" ] || fail "il tag $tag non c'è, né qui né nel repository: crealo prima di pubblicare." label() { docker image inspect --format "{{ index .Config.Labels \"org.opencontainers.image.$1\" }}" "$image" } built=$(label version) || fail "non trovo l'immagine $image." # The image built by hand for vX.Y.Z-something says X.Y.Z, the version of Directory.Build.props: the commit tells them apart. [ "$built" = "$version" ] || [ "$built" = "$expected" ] || fail "l'immagine $image dice di essere la versione '$built', non $version: ricostruiscila." source=$(label revision) || fail "non trovo l'immagine $image." [ "$source" = "$commit" ] || fail "l'immagine $image viene dal commit '$source', non da $commit: ricostruiscila dal commit del tag, senza modifiche in sospeso." # 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)}."