Files
Febe/.gitea/scripts/costruisci-immagine.sh
T
Alby96andClaude Opus 5.5 009e7d6710 CI e installatore: commenti degli script in inglese (#43)
La regola del progetto è «codice e commenti in inglese; interfaccia, documenti
e messaggi in italiano», e gli script di .gitea/ (script e flussi), di
installer/ e docker/entrypoint.sh avevano ancora i commenti in italiano
(decisione di Alberto del 29 novembre 2026). Cambiano solo i commenti: i
messaggi che una persona legge (echo, ::warning::, ::error::, nomi dei passi,
note della release) restano in italiano e nessun comando cambia. I programmi
awk e gli script finti delle prove erano già in inglese.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 12:03:13 +01:00

108 lines
6.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Builds the Febe Docker image with the buildx builder of the run (docker-container), always with the same arguments
# and the same labels: the build that is published comes entirely from the cache of the one that was tested and
# scanned, so the layers are the same.
#
# costruisci-immagine.sh builder <registro> the buildx builder of the run (BUILDER), with the
# docker-container driver and the buildkit configuration
# that says the home registry answers in HTTP (instead
# of docker/setup-buildx-action, a JavaScript action);
# pulizia-docker.sh esecuzione removes it
# costruisci-immagine.sh archivio <file.tar> <nome locale> archive of the image, for docker load and for Trivy
# costruisci-immagine.sh pubblica <nome:etichetta>... push to the REGISTRY registry by buildx, with
# REGISTRO_UTENTE and REGISTRO_TOKEN in a client
# configuration of its own, removed at the end; the
# daemon never talks to the registry (it would try HTTPS)
#
# The image is a plain manifest, without attestations (--provenance=false --sbom=false). With the attestations, which
# buildx adds by itself, it would be an index: Gitea keeps every child manifest as a version of its own, sha256:…
# without a tag, with the configuration and the layers, and removing a :sha- tag from the registry would remove only
# the index. With a plain manifest the layers belong to the tag's version, and the registry cleanup really frees them.
#
# Required variables: BUILDER, VERSIONE, GIT_COMMIT, FLUSSO (verifica or rilascio), ESECUZIONE (gitea.run_id).
# The febe.ci.* labels say which workflow, run, branch and commit built the image. They are not used by the cleanup:
# they are also in the published images and Docker copies them into every container created from those, even in a
# real Febe. That is why febe.ci.prova, the mark of test containers only (prova-immagine.sh), is never set here:
# PipelineTests checks it.
set -euo pipefail
modo="${1:?Indica il modo: builder, archivio o pubblica.}"
shift
cd "$(git rev-parse --show-toplevel)"
if [[ "$modo" == builder ]]; then
registro="${1:?Indica il registro, per esempio 192.168.30.23:3000.}"
# Only a host name (or an address) with the port: the value ends up in a TOML file.
[[ "$registro" =~ ^[A-Za-z0-9.-]+(:[0-9]+)?$ ]] || { echo "Registro non valido: «$registro»." >&2; exit 2; }
configurazione="${RUNNER_TEMP:-/tmp}/buildkitd-${BUILDER:?}.toml"
printf '[registry."%s"]\n http = true\n insecure = true\n' "$registro" > "$configurazione"
# --buildkitd-config since buildx 0.13; before, it was called --config.
opzione=--buildkitd-config
aiuto=$(docker buildx create --help 2> /dev/null) || true
[[ "$aiuto" == *--buildkitd-config* ]] || opzione=--config
docker buildx create --name "$BUILDER" --driver docker-container "$opzione" "$configurazione" --bootstrap
rm -f "$configurazione"
docker buildx inspect "$BUILDER"
exit 0
fi
comuni=(--builder "${BUILDER:?}" -f docker/Dockerfile --provenance=false --sbom=false
--build-arg "VERSION=${VERSIONE:?}" --build-arg "GIT_COMMIT=${GIT_COMMIT:?}"
--label "febe.ci.workflow=${FLUSSO:?}" --label "febe.ci.run=${ESECUZIONE:?}"
--label "febe.ci.ref=${GITHUB_REF_NAME:-}" --label "febe.ci.commit=${GITHUB_SHA:-}")
case "$modo" in
archivio)
archivio="${1:?Indica il file .tar da scrivere.}"
nome="${2:?Indica il nome locale, per esempio febe-ci:123.}"
mkdir -p "$(dirname "$archivio")"
docker buildx build "${comuni[@]}" --output "type=docker,name=$nome,dest=$archivio" .
echo "Archivio dell'immagine: $archivio ($(du -h "$archivio" | cut -f1))"
;;
pubblica)
(($# > 0)) || { echo "Indica almeno un nome:etichetta da pubblicare." >&2; exit 2; }
registro="${REGISTRY:?Manca REGISTRY, il registro su cui pubblicare.}"
[[ "$registro" =~ ^[A-Za-z0-9.-]+(:[0-9]+)?$ ]] || { echo "Registro non valido: «$registro»." >&2; exit 2; }
[[ -n "${REGISTRO_UTENTE:-}" && -n "${REGISTRO_TOKEN:-}" ]] || { echo "Mancano REGISTRO_UTENTE e REGISTRO_TOKEN." >&2; exit 2; }
etichette=()
for nome in "$@"; do
etichette+=(-t "$nome")
done
# The credentials in a client configuration entirely our own, only for this push and then removed: the runner's
# one is never touched (no jq to modify it, no credential left in its folder). buildx passes them to the builder;
# the daemon is not involved (it would try HTTPS). From the original client we keep the plugins, the contexts and
# the current context; the buildx builders are found through BUILDX_CONFIG. The name is fixed for the run, so the
# «Pulizia (sempre)» step removes it even after an interruption that skips the trap.
originale="${DOCKER_CONFIG:-$HOME/.docker}"
configurazione="${RUNNER_TEMP:-/tmp}/febe-docker-${ESECUZIONE}"
rm -rf "$configurazione"
trap 'rm -rf "$configurazione"' EXIT
(umask 077 && mkdir -p "$configurazione")
for parte in cli-plugins contexts; do
if [[ -e "$originale/$parte" ]]; then ln -s "$originale/$parte" "$configurazione/$parte"; fi
done
contesto=""
if [[ -s "$originale/config.json" ]]; then
contesto=$(bash .gitea/scripts/json.sh campi "" currentContext "$originale/config.json" 2> /dev/null) || contesto=""
[[ "$contesto" =~ ^[A-Za-z0-9][A-Za-z0-9_.+-]*$ ]] || contesto=""
fi
credenziali=$(printf '%s:%s' "$REGISTRO_UTENTE" "$REGISTRO_TOKEN" | base64 -w0)
# Only base64 characters, and the registry checked above: the JSON is written without any need for escapes.
[[ "$credenziali" =~ ^[A-Za-z0-9+/]+=*$ ]] || { echo "Credenziali del registro non codificabili." >&2; exit 2; }
(
umask 077
printf '{"auths":{"%s":{"auth":"%s"}}%s}\n' "$registro" "$credenziali" "${contesto:+,\"currentContext\":\"$contesto\"}" \
> "$configurazione/config.json"
)
unset credenziali
DOCKER_CONFIG="$configurazione" BUILDX_CONFIG="${BUILDX_CONFIG:-$originale/buildx}" \
docker buildx build "${comuni[@]}" "${etichette[@]}" --push .
printf 'Pubblicata: %s\n' "$@"
;;
*)
echo "Modo sconosciuto: «$modo» (builder, archivio o pubblica)." >&2
exit 2
;;
esac