#!/usr/bin/env bash # CI artifacts, instead of actions/upload-artifact (a JavaScript action, which needs Node): the files go to the Gitea # package registry, generic package febe-ci, one version per run: # # -ci. from the verifica workflow (like Febe-Setup--ci..exe) # -rilascio. from the rilascio workflow # # with from Directory.Build.props and the run number (NUMERO). They are downloaded from the package # page on Gitea (owner ▸ Packages ▸ febe-ci). The Monday cleanup (full pulizia-docker.sh) removes the versions created # more than 14 days earlier, as the artifact expiry used to do. # # .gitea/scripts/artefatti.sh verifica|rilascio ... # # Variables: REGISTRY_TOKEN (token with «package: read and write», only in the curl header, never printed), GITEA_URL # (the server address, gitea.server_url), PROPRIETARIO, NUMERO. Without REGISTRY_TOKEN a warning and no file uploaded: # the job stays green, as for the image. A missing file or a refusal by the registry fails the step. A file with the # same name already uploaded (a new attempt of the same run) is replaced. set -euo pipefail flusso="${1:-}" case "$flusso" in verifica) suffisso=ci ;; rilascio) suffisso=rilascio ;; *) echo "Uso: artefatti.sh verifica|rilascio ..." >&2; exit 2 ;; esac shift (($# > 0)) || { echo "Indica almeno un file da caricare." >&2; exit 2; } cd "$(git rev-parse --show-toplevel)" numero="${NUMERO:-}" [[ "$numero" =~ ^[0-9]+$ ]] || { echo "Manca NUMERO, il numero dell'esecuzione, o non è un numero: «$numero»." >&2; exit 2; } v=$(sed -n 's:.*\(.*\).*:\1:p' Directory.Build.props | head -n 1) versione="$v-$suffisso.$numero" [[ "$versione" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?-(ci|rilascio)\.[0-9]+$ ]] || { echo "Versione degli artefatti non valida: «$versione»." >&2; exit 2; } for file in "$@"; do [[ -f "$file" ]] || { echo "Manca l'artefatto $file." >&2; exit 1; } [[ "$(basename "$file")" =~ ^[0-9A-Za-z._-]+$ ]] || { echo "Nome di file non adatto a un pacchetto: «$(basename "$file")»." >&2; exit 2; } done if [[ -z "${REGISTRY_TOKEN:-}" ]]; then echo "::warning::Manca il segreto REGISTRY_TOKEN: gli artefatti ($*) non vanno sul registro dei pacchetti. Aggiungilo (token con package: read and write) per averli per 14 giorni." exit 0 fi proprietario="${PROPRIETARIO:?Manca PROPRIETARIO.}" base="${GITEA_URL:?Manca GITEA_URL.}" base="${base%/}/api/packages/$proprietario/generic/febe-ci/$versione" # One call to the registry: the HTTP code, 000 if Gitea does not answer. The token is only in the curl header. chiedi() { local codice codice=$(curl -sS --max-time 900 -H "Authorization: token $REGISTRY_TOKEN" -o /dev/null -w '%{http_code}' "$@") || true echo "${codice:-000}" } for file in "$@"; do nome="$(basename "$file")" codice=$(chiedi -X PUT --upload-file "$file" "$base/$nome") if [[ "$codice" == 409 ]]; then codice=$(chiedi -X DELETE "$base/$nome") [[ "$codice" == 204 ]] || { echo "Il registro non toglie la copia precedente di $nome: risponde $codice." >&2; exit 1; } codice=$(chiedi -X PUT --upload-file "$file" "$base/$nome") fi [[ "$codice" == 201 ]] || { echo "Il registro non accetta $nome: risponde $codice." >&2; exit 1; } echo "$nome caricato ($(du -h "$file" | cut -f1))" done echo "Artefatti: ${GITEA_URL%/}/$proprietario/-/packages/generic/febe-ci/$versione"