Files
alpine-packages/scripts/apk/publish-gitea.sh
Joachim Schlöffel 85122866ac
Build Alpine Packages / build-and-publish (v3.24, 3.24) (push) Successful in 18s
fix publisher bug for multiarch packages
2026-07-13 03:06:06 +02:00

119 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
# shellcheck source=scripts/apk/package-lib.sh
. "${repo_root}/scripts/apk/package-lib.sh"
repo_name="${ALPINE_REPO_NAME:-local}"
alpine_version="${ALPINE_VERSION:-3.24}"
arches="${ALPINE_ARCHES:-x86_64 aarch64}"
branch="${ALPINE_REGISTRY_BRANCH:-v${alpine_version}}"
repository="${ALPINE_REGISTRY_REPOSITORY:-${PACKAGE_NAME:-main}}"
instance_url="${INSTANCE_URL:-}"
owner="${PACKAGE_OWNER:-}"
user="${PACKAGE_USER:-}"
token="${PACKAGE_TOKEN:-}"
allow_conflicts="${GITEA_APK_ALLOW_CONFLICTS:-1}"
require_env() {
local name="$1"
local value="$2"
if [[ -z "${value}" ]]; then
printf 'missing required environment variable: %s\n' "${name}" >&2
exit 2
fi
}
require_env INSTANCE_URL "${instance_url}"
require_env PACKAGE_OWNER "${owner}"
require_env PACKAGE_USER "${user}"
require_env PACKAGE_TOKEN "${token}"
instance_url="${instance_url%/}"
upload_url="${instance_url}/api/packages/${owner}/alpine/${branch}/${repository}"
package_root="${repo_root}/packages/${repo_name}"
if [[ ! -d "${package_root}" ]]; then
printf 'missing local package repository: %s\n' "${package_root}" >&2
printf 'run: mise run apk:build-all\n' >&2
exit 1
fi
shopt -s nullglob
apks=("${package_root}"/*/*.apk)
shopt -u nullglob
if [[ "${#apks[@]}" -eq 0 ]]; then
printf 'no APK files found under %s\n' "${package_root}" >&2
exit 1
fi
declare -A expected_apks=()
while IFS= read -r package_dir; do
apk_validate_package_dir "${package_dir}"
for arch in ${arches}; do
if ! apk_package_supports_arch "${package_dir}" "${arch}"; then
continue
fi
while IFS= read -r apk_name; do
expected_apks["${package_root}/${arch}/${apk_name}"]=1
done < <(apk_package_apk_names "${package_dir}" "${arch}")
done
done < <(apk_all_package_dirs "${repo_root}")
current_apks=()
stale_count=0
for apk in "${apks[@]}"; do
if [[ -z "${expected_apks[${apk}]:-}" ]]; then
printf 'Skipping stale local APK: %s\n' "${apk#"${repo_root}/"}"
stale_count=$((stale_count + 1))
continue
fi
current_apks+=("${apk}")
done
if [[ "${#current_apks[@]}" -eq 0 ]]; then
printf 'no current APK files found under %s\n' "${package_root}" >&2
exit 1
fi
printf 'Publishing %d APK files to %s\n' "${#current_apks[@]}" "${upload_url}"
if [[ "${stale_count}" -ne 0 ]]; then
printf 'Skipping %d stale local APK file(s)\n' "${stale_count}"
fi
for apk in "${current_apks[@]}"; do
filename="$(basename "${apk}")"
status="$(
curl --silent --show-error --location \
--user "${user}:${token}" \
--upload-file "${apk}" \
--write-out '%{http_code}' \
--output /tmp/gitea-apk-publish-response \
"${upload_url}"
)"
case "${status}" in
201)
printf 'published: %s\n' "${filename}"
;;
409)
if [[ "${allow_conflicts}" == "1" ]]; then
printf 'already exists: %s\n' "${filename}"
else
printf 'conflict: %s already exists\n' "${filename}" >&2
cat /tmp/gitea-apk-publish-response >&2
exit 1
fi
;;
*)
printf 'publish failed for %s: HTTP %s\n' "${filename}" "${status}" >&2
cat /tmp/gitea-apk-publish-response >&2
exit 1
;;
esac
done