#!/usr/bin/env bash set -euo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" repo_name="${ALPINE_REPO_NAME:-local}" alpine_version="${ALPINE_VERSION:-3.23}" 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 printf 'Publishing %d APK files to %s\n' "${#apks[@]}" "${upload_url}" for apk in "${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