#!/usr/bin/env bash # Creates a draft release on Forgejo with the apks of dist/ and dist-awg/ and their SHA256SUMS. # The pkgs project (make release) takes the apks from it, signs them and publishes the signed release. set -Eeuo pipefail SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd -P) SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" ROOT_DIR="$(dirname "$SCRIPT_DIR")" FORGEJO_URL="${FORGEJO_URL:-https://forgejo.r4ven.lan}" FORGEJO_REPO="${FORGEJO_REPO:-lans/openwrt}" FORGEJO_TOKEN="${FORGEJO_TOKEN:-}" log_info() { printf '[INFO] - %s\n' "$*"; } log_error() { printf '[ERROR] - %s\n' "$*" >&2; exit 1; } usage() { cat << EOF Usage: ${SCRIPT_NAME} Example: ${SCRIPT_NAME} v1.2.3 Environment: FORGEJO_TOKEN (required), FORGEJO_URL (default ${FORGEJO_URL}), FORGEJO_REPO (default ${FORGEJO_REPO}). Normally started by: make draft TAG= EOF } if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then usage exit 0 fi [[ $# -eq 1 ]] || { usage >&2; exit 1; } TAG="$1" [[ -n "$FORGEJO_TOKEN" ]] || log_error "FORGEJO_TOKEN is not set" [[ "$FORGEJO_URL" == https://* ]] || log_error "FORGEJO_URL must start with https://" for tool in curl jq sha256sum; do command -v "$tool" > /dev/null 2>&1 || log_error "Tool ${tool} is not installed" done shopt -s nullglob apks=("${ROOT_DIR}"/dist/*.apk "${ROOT_DIR}"/dist-awg/*.apk) [[ "${#apks[@]}" -gt 0 ]] || log_error "No apks in dist/ or dist-awg/. Run make build and make build-awg" work_dir=$(mktemp -d) trap 'rm -rf "$work_dir"' EXIT cp "${apks[@]}" "$work_dir/" (cd "$work_dir" && sha256sum ./*.apk | sed 's|\./||' > SHA256SUMS) fj() { printf 'header = "Authorization: token %s"\n' "$FORGEJO_TOKEN" | curl -fsS -K - "$@" } api="${FORGEJO_URL}/api/v1/repos/${FORGEJO_REPO}" body=$(jq -n --arg t "$TAG" '{tag_name: $t, target_commitish: "main", name: $t, draft: true}') release_id=$(fj -X POST -H "Content-Type: application/json" -d "$body" "${api}/releases" | jq -r '.id') [[ -n "$release_id" && "$release_id" != "null" ]] || log_error "The release was not created" for file in "$work_dir"/*; do fj -X POST -F "attachment=@${file}" "${api}/releases/${release_id}/assets?name=$(basename "$file")" > /dev/null log_info "Uploaded $(basename "$file")" done log_info "Draft release ${TAG} created. Next: make release REPO=${FORGEJO_REPO} TAG=${TAG} in the pkgs project"