diff --git a/.forgejo/upload-rpms.sh b/.forgejo/upload-rpms.sh new file mode 100755 index 00000000..30bbf34b --- /dev/null +++ b/.forgejo/upload-rpms.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# Uploads the RPMs a build downloaded (its dnf cache, kept by +# Containerfile.in's cache mounts) to this forge's RPM registry, one group +# per upstream repository: fedora, updates, terra, copr-ublue-os-packages... +# Each group is a dnf repository at +# https://forge.quinefoundation.com/api/packages/quine-global-labs/rpm/. +# +# Usage: upload-rpms.sh CACHE_DIR (a libdnf5 cache: one directory per +# repository, -/packages/*.rpm) +# +# Needs REGISTRY_USER and REGISTRY_TOKEN (package write access). Packages +# the registry already has are skipped (409). Best effort: a failed upload +# is a warning, not a failure. +set -euo pipefail + +cache=$1 +api=${RPM_API:-https://forge.quinefoundation.com/api/packages/quine-global-labs/rpm} + +# The token goes to curl in a file, not on its command line. +netrc=$(mktemp) +trap 'rm -f "$netrc"' EXIT +chmod 600 "$netrc" +printf 'machine %s login %s password %s\n' \ + "$(sed -E 's|^https?://([^/]+).*|\1|' <<<"$api")" "$REGISTRY_USER" "$REGISTRY_TOKEN" >"$netrc" + +upload() { # group rpm + local code + code=$(curl -sS -o /dev/null -w '%{http_code}' --retry 3 --netrc-file "$netrc" \ + --upload-file "$2" "$api/$1/upload") || code=000 + case $code in + 201) echo "new $1 ${2##*/}" ;; + 409) echo "had $1 ${2##*/}" ;; + *) echo "::warning::$code uploading ${2##*/} to $1" ;; + esac +} +export -f upload +export api netrc + +for dir in "$cache"/*/; do + id=$(basename "$dir") + id=${id%-*} # the hash libdnf5 appends + [[ $id == @* ]] && continue # @commandline: local files, not a repository + # copr:copr.fedorainfracloud.org:ublue-os:packages -> copr-ublue-os-packages + group=$(sed -E 's/^copr:[^:]+:/copr-/; s/:/-/g' <<<"$id") + find "$dir" -name '*.rpm' -printf "$group\0%p\0" +done | xargs -0 -n 2 -P 4 bash -c 'upload "$@"' _ | + tee /dev/stderr | awk '{n[$1]++} END {for (k in n) printf "%s: %d\n", k, n[k]}'