#!/usr/bin/env bash # Takes the packages (.deb, .rpm, .pkg.tar.zst, .apk) from a local directory (the dist/ of a project), adds them # to the deb, rpm, arch and apk repositories, signs the indexes with the keys from KeePassXC and uploads the site # by rsync. The release of the tag on the public Forgejo gets only metadata: links to the site and the checksums. # The canonical (and only) storage of the signed packages is the site itself. 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")" # shellcheck source=scripts/lib.sh source "${SCRIPT_DIR}/lib.sh" KDBX="${KDBX:-}" ENTRY="${ENTRY:-}" ATTACH="${ATTACH:-r4ven-pkgs.private.asc}" KDBX_PASS_CMD="${KDBX_PASS_CMD:-}" # The public Forgejo: the API address (VPN only) and a token with the right to edit releases. FORGEJO_URL="${FORGEJO_URL:-https://git.r4ven.me}" FORGEJO_TOKEN="${FORGEJO_TOKEN:-}" FORGEJO_TOKEN_CMD="${FORGEJO_TOKEN_CMD:-}" # The directory with the unsigned packages (the dist/ of a project); several directories are separated by a colon. SRC="${SRC:-}" # The site directory on the server, as an rsync target: user@host:/srv/pkgs SITE_REMOTE="${SITE_REMOTE:-}" # Optional: the ssh command for rsync (-e) and the rsync command on the server (--rsync-path, for example sudo). SITE_RSH="${SITE_RSH:-ssh}" SITE_RSYNC_PATH="${SITE_RSYNC_PATH:-rsync}" IMAGE="${IMAGE:-r4ven-pkgs-sign}" KEEP="${KEEP:-3}" # apk (OpenWrt) has its own RSA key; the release and the architecture of the .apk files are not in the files. APK_ENTRY="${APK_ENTRY:-}" APK_ATTACH="${APK_ATTACH:-r4ven-apk.pem}" APK_RELEASE="${APK_RELEASE:-25.12}" APK_ARCH="${APK_ARCH:-x86_64}" APK_IMAGE="${APK_IMAGE:-r4ven-pkgs-apk}" APK_PUBLIC_KEY="${APK_PUBLIC_KEY:-${ROOT_DIR}/keys/r4ven-apk.pub}" PUBLIC_KEY="${PUBLIC_KEY:-${ROOT_DIR}/keys/r4ven-pkgs.asc}" # For tests: a private key file instead of KeePassXC, and no upload. KEY_FILE="${KEY_FILE:-}" APK_KEY_FILE="${APK_KEY_FILE:-}" SITE_DIR="${SITE_DIR:-}" DRY_RUN="${DRY_RUN:-}" usage() { cat << EOF Usage: SRC= ${SCRIPT_NAME} Example: SRC=../korterm/dist ${SCRIPT_NAME} ivan/korterm v0.3.0 Several directories: SRC=../openwrt/dist:../openwrt/dist-awg The repository and the tag are those of the public Forgejo; push the tag there first (git push public ). Environment: SRC (required), KDBX, ENTRY, SITE_REMOTE (SITE_RSH, SITE_RSYNC_PATH), FORGEJO_TOKEN or FORGEJO_TOKEN_CMD (required; the command wins over the token), ATTACH, KDBX_PASS_CMD, KEEP (versions kept per package, default ${KEEP}), FORGEJO_URL (default ${FORGEJO_URL}). For .apk files: APK_ENTRY (required then), APK_ATTACH, APK_RELEASE (default ${APK_RELEASE}), APK_ARCH (default ${APK_ARCH}). Test mode: PUBLIC_KEY, KEY_FILE, APK_KEY_FILE, APK_PUBLIC_KEY, SITE_DIR (output tree), DRY_RUN=1. Normally started by: make release SRC= REPO= TAG= EOF } if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then usage exit 0 fi [[ $# -eq 2 ]] || { usage >&2; exit 1; } REPO="$1" TAG="$2" SECRET_DIR="" APK_SECRET_DIR="" cleanup() { trap - SIGINT SIGHUP SIGTERM SIGQUIT ERR EXIT [[ -n "$SECRET_DIR" && -d "$SECRET_DIR" ]] && rm -rf "$SECRET_DIR" [[ -n "$APK_SECRET_DIR" && -d "$APK_SECRET_DIR" ]] && rm -rf "$APK_SECRET_DIR" return 0 } # shellcheck disable=SC2154 # rc is assigned inside the trap string itself trap 'rc=$?; cleanup; exit $rc' SIGINT SIGHUP SIGTERM SIGQUIT ERR EXIT # Forgejo API call. The token goes to curl through stdin, so it is not visible in the process list. fj() { printf 'header = "Authorization: token %s"\n' "$FORGEJO_TOKEN" | curl -fsS -K - "$@" } kp() { if [[ -n "$KDBX_PASS_CMD" ]]; then bash -c "$KDBX_PASS_CMD" | keepassxc-cli "$@" else keepassxc-cli "$@" fi } tools=(docker sha256sum flock) [[ -n "$DRY_RUN" ]] || tools+=(curl jq rsync git) [[ -n "$KEY_FILE" ]] || tools+=(keepassxc-cli) for tool in "${tools[@]}"; do command -v "$tool" > /dev/null 2>&1 || log_error "Tool ${tool} is not installed" done [[ -f "$PUBLIC_KEY" ]] || log_error "Public key ${PUBLIC_KEY} not found. Run: make keygen" [[ -n "$SRC" ]] || log_error "SRC is not set. Pass the directory with the packages: make release SRC=../korterm/dist REPO=... TAG=..." IFS=: read -r -a src_dirs <<< "$SRC" for src_dir in "${src_dirs[@]}"; do [[ -d "$src_dir" ]] || log_error "SRC ${src_dir} is not a directory" done [[ "$REPO" =~ ^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$ ]] || log_error "REPO must look like owner/name" if [[ -z "$DRY_RUN" ]]; then # The command wins over FORGEJO_TOKEN: a token left in the environment may belong to another Forgejo. [[ -z "$FORGEJO_TOKEN_CMD" ]] || FORGEJO_TOKEN="$(bash -c "$FORGEJO_TOKEN_CMD")" [[ -n "$FORGEJO_TOKEN" ]] || log_error "FORGEJO_TOKEN is not set. Set it (or FORGEJO_TOKEN_CMD) in .local.mk" fi [[ "$FORGEJO_URL" == https://* ]] || log_error "FORGEJO_URL must start with https://" # The path must be absolute and at least two levels deep: rsync --delete runs on it. The form user@host:/ is for a key # that the server restricts with rrsync (see the README): there "/" is the site directory itself. [[ -n "$DRY_RUN" || "$SITE_REMOTE" =~ ^[A-Za-z0-9._@-]+:/[^/]+/[^/]+ || "$SITE_REMOTE" =~ ^[A-Za-z0-9._@-]+:/$ ]] \ || log_error "SITE_REMOTE must look like user@host:/srv/pkgs (or user@host:/ for an rrsync key)" [[ -n "$DRY_RUN" || -n "$SITE_REMOTE" ]] || log_error "SITE_REMOTE is not set. Set it in .local.mk (user@host:/srv/pkgs)" if [[ -z "$KEY_FILE" ]]; then [[ -n "$KDBX" ]] || log_error "KDBX is not set. Copy .local.mk.example to .local.mk and fill it in" [[ -f "$KDBX" ]] || log_error "Database ${KDBX} not found" [[ -n "$ENTRY" ]] || log_error "ENTRY is not set. Set it in .local.mk" fi api="${FORGEJO_URL}/api/v1/repos/${REPO}" # Before anything is signed: the packages must come from the commit of the tag, and that tag must be the same # one on the public Forgejo. The local checks come first (they are cheap and need no network). if [[ -z "$DRY_RUN" ]]; then src_repo=$(git -C "${src_dirs[0]}" rev-parse --show-toplevel 2> /dev/null) \ || log_error "SRC ${src_dirs[0]} is not inside a git checkout, so the commit of the packages cannot be checked" for src_dir in "${src_dirs[@]}"; do [[ "$(git -C "$src_dir" rev-parse --show-toplevel 2> /dev/null)" == "$src_repo" ]] \ || log_error "The SRC directories belong to different git checkouts" done tag_commit=$(git -C "$src_repo" rev-parse -q --verify "refs/tags/${TAG}^{commit}") \ || log_error "Tag ${TAG} is not in the checkout ${src_repo}. Create it there and build from it" [[ "$(git -C "$src_repo" rev-parse HEAD)" == "$tag_commit" ]] \ || log_error "The checkout ${src_repo} is not at the tag ${TAG}: the packages may come from another commit" [[ -z "$(git -C "$src_repo" status --porcelain --untracked-files=no)" ]] \ || log_error "The checkout ${src_repo} has uncommitted changes: the packages may not match the tag ${TAG}" tag_json=$(fj "${api}/tags/${TAG}" 2> /dev/null) \ || log_error "Tag ${TAG} is not found in ${REPO} on ${FORGEJO_URL} (or the API is unreachable: is the VPN on?). Push the tag first: git push public ${TAG}" [[ "$(jq -r '.commit.sha // empty' <<< "$tag_json")" == "$tag_commit" ]] \ || log_error "Tag ${TAG} in ${REPO} on ${FORGEJO_URL} points at another commit than the local tag (${tag_commit})" fi work_dir="${ROOT_DIR}/.work" dist_dir="${work_dir}/releases/${REPO//\//_}_${TAG}" site_dir="${SITE_DIR:-${work_dir}/site-mirror}" # Two runs at once would mix the working trees and upload a half-built site. mkdir -p "$work_dir" exec {lock_fd}> "${work_dir}/.lock" flock -n "$lock_fd" || log_error "Another make release is running (lock: ${work_dir}/.lock)" rm -rf "$dist_dir" "$site_dir" mkdir -p "$dist_dir" "$site_dir" # 1. The unsigned packages from SRC. SHA256SUMS is made here: it makes the signing step write the signed release files. shopt -s nullglob for src_dir in "${src_dirs[@]}"; do for file in "$src_dir"/*.deb "$src_dir"/*.rpm "$src_dir"/*.pkg.tar.zst "$src_dir"/*.apk; do [[ "$(basename "$file")" =~ ^[A-Za-z0-9][A-Za-z0-9+._~-]*$ ]] || log_error "Invalid file name: $(basename "$file")" [[ -f "$file" ]] || continue cp "$file" "$dist_dir/" done done apks=("$dist_dir"/*.apk) packages=("$dist_dir"/*.deb "$dist_dir"/*.rpm "$dist_dir"/*.pkg.tar.zst "${apks[@]}") [[ "${#packages[@]}" -gt 0 ]] || log_error "${SRC} has no .deb, .rpm, .pkg.tar.zst or .apk files" if [[ "${#apks[@]}" -gt 0 && -z "$APK_KEY_FILE" ]]; then [[ -n "$APK_ENTRY" ]] || log_error "There are .apk files: set APK_ENTRY (the KeePassXC entry with the apk key) in .local.mk" fi [[ "${#apks[@]}" -eq 0 || -f "$APK_PUBLIC_KEY" ]] || log_error "Public key ${APK_PUBLIC_KEY} not found" (cd "$dist_dir" && sha256sum -- "${packages[@]##*/}" > SHA256SUMS) # 2. The current site tree, to keep the packages already published. if [[ -n "$DRY_RUN" ]]; then log_info "Dry run: starting a new site tree" else # The first run: make sure the target directory exists (an empty upload creates it, and deletes nothing). empty_dir=$(mktemp -d) rsync -r --mkpath -e "$SITE_RSH" --rsync-path "$SITE_RSYNC_PATH" "${empty_dir}/" "${SITE_REMOTE}/" rmdir "$empty_dir" log_info "Downloading the current site from ${SITE_REMOTE}" rsync -rt --no-links -e "$SITE_RSH" --rsync-path "$SITE_RSYNC_PATH" "${SITE_REMOTE}/" "${site_dir}/" fi install -m644 "$PUBLIC_KEY" "${site_dir}/r4ven-pkgs.asc" install -m644 "${ROOT_DIR}"/build/site/* "${site_dir}/" if [[ "${#apks[@]}" -gt 0 ]]; then install -Dm644 "$APK_PUBLIC_KEY" "${site_dir}/apk/r4ven-apk.pub" fi # The address of the project for the landing page. apk_project_url="${FORGEJO_URL}/${REPO}" # 3. apk: its own key, its own container (Alpine with apk-tools 3), no GPG key inside. if [[ "${#apks[@]}" -gt 0 ]]; then docker build -q -f "${ROOT_DIR}/build/Dockerfile.apk" -t "$APK_IMAGE" "${ROOT_DIR}/build" > /dev/null APK_SECRET_DIR=$(mktemp -d /dev/shm/r4ven-apk-sign.XXXXXX) if [[ -n "$APK_KEY_FILE" ]]; then install -m600 "$APK_KEY_FILE" "${APK_SECRET_DIR}/r4ven-apk.pem" else kp attachment-export -q "$KDBX" "$APK_ENTRY" "$APK_ATTACH" "${APK_SECRET_DIR}/r4ven-apk.pem" chmod 600 "${APK_SECRET_DIR}/r4ven-apk.pem" fi docker run --rm \ --network none --cap-drop ALL --security-opt no-new-privileges --tmpfs /tmp:rw,mode=1777 \ --user "$(id -u):$(id -g)" -e HOME=/tmp -e KEEP="$KEEP" -e APK_RELEASE="$APK_RELEASE" -e APK_ARCH="$APK_ARCH" \ -e APK_PROJECT_URL="$apk_project_url" \ -v "${SCRIPT_DIR}/sign_apk.sh:/scripts/sign_apk.sh:ro" \ -v "${SCRIPT_DIR}/lib.sh:/scripts/lib.sh:ro" \ -v "${APK_SECRET_DIR}:/keys:ro" \ -v "${site_dir}:/site" \ -v "${dist_dir}:/dist:ro" \ "$APK_IMAGE" /scripts/sign_apk.sh rm -rf "$APK_SECRET_DIR" APK_SECRET_DIR="" fi # 4. The images first (network), then the private key: it goes to tmpfs for the time of the signing only. docker build -q -f "${ROOT_DIR}/build/Dockerfile.sign" -t "$IMAGE" "${ROOT_DIR}/build" > /dev/null SECRET_DIR=$(mktemp -d /dev/shm/r4ven-pkgs-sign.XXXXXX) if [[ -n "$KEY_FILE" ]]; then install -m600 "$KEY_FILE" "${SECRET_DIR}/private.asc" else kp attachment-export -q "$KDBX" "$ENTRY" "$ATTACH" "${SECRET_DIR}/private.asc" chmod 600 "${SECRET_DIR}/private.asc" fi # The container runs as the same user, so the key stays readable for that user only. # 5. Add the packages, build the indexes and sign in the container. docker run --rm \ --network none --cap-drop ALL --security-opt no-new-privileges --tmpfs /tmp:rw,mode=1777 \ --user "$(id -u):$(id -g)" -e HOME=/tmp -e KEEP="$KEEP" \ -v "${SCRIPT_DIR}/sign_repo.sh:/scripts/sign_repo.sh:ro" \ -v "${SCRIPT_DIR}/lib.sh:/scripts/lib.sh:ro" \ -v "${SECRET_DIR}:/keys:ro" \ -v "${site_dir}:/site" \ -v "${dist_dir}:/dist" \ "$IMAGE" /scripts/sign_repo.sh rm -rf "$SECRET_DIR" SECRET_DIR="" # 6. The landing page lists the newest version of every package from catalog.json. python3 -I "${ROOT_DIR}/build/gen_index.py" "$site_dir" # 7. Publish. The release carries only metadata and links (the packages live on the site), then the site goes up, # then the draft goes public. signed_dir="${dist_dir}/signed" [[ -f "${signed_dir}/SHA256SUMS.asc" ]] || log_error "The signing step did not write ${signed_dir}/SHA256SUMS.asc" if [[ -n "$DRY_RUN" ]]; then log_info "Dry run: nothing is pushed. Signed tree: ${site_dir}, signed release files: ${signed_dir}" exit 0 fi log_info "Updating the release at ${FORGEJO_URL}/${REPO}" rel_json=$(fj "${api}/releases/tags/${TAG}" 2> /dev/null || true) if [[ -z "$rel_json" ]]; then rel_json=$(fj "${api}/releases?draft=true&limit=50" | jq -c --arg t "$TAG" 'map(select(.tag_name == $t)) | first // empty') fi if [[ -z "$rel_json" ]]; then create_body=$(jq -n --arg t "$TAG" '{tag_name: $t, name: $t, draft: true}') rel_json=$(fj -X POST -H "Content-Type: application/json" -d "$create_body" "${api}/releases") fi rel_id=$(jq -r '.id' <<< "$rel_json") rel_draft=$(jq -r '.draft' <<< "$rel_json") # Remove assets left by the old publication flow. This migration is deliberately # repeated on every run, so a partially cleaned release is repaired safely. while IFS=$'\t' read -r asset_id asset_name; do case "$asset_name" in *.deb | *.rpm | *.pkg.tar.zst | *.apk | *.sig | SHA256SUMS | SHA256SUMS.asc) fj -X DELETE "${api}/releases/${rel_id}/assets/${asset_id}" > /dev/null ;; *) ;; esac done < <(fj "${api}/releases/${rel_id}" | jq -r '.assets[] | [.id, .name] | @tsv') # The body points at the canonical package storage and carries the checksums; no binary assets are attached. release_body=$(jq -n \ --arg site "https://pkgs.r4ven.me/" \ --arg src "${FORGEJO_URL}/${REPO}/src/tag/${TAG}" \ --rawfile sums "${signed_dir}/SHA256SUMS" \ '{body: ("Packages: " + $site + "\n\nRepository instructions: " + $site + "\n\nSource: " + $src + "\n\n```\n" + $sums + "```\n")}') fj -X PATCH -H "Content-Type: application/json" -d "$release_body" "${api}/releases/${rel_id}" > /dev/null log_info "Uploading the site to ${SITE_REMOTE}" # --delay-updates puts the new files in place at the end of the transfer; --delete-delay removes the old ones after that. rsync -rt --delay-updates --delete-delay --max-delete=1000 -e "$SITE_RSH" --rsync-path "$SITE_RSYNC_PATH" "${site_dir}/" "${SITE_REMOTE}/" if [[ "$rel_draft" == "true" ]]; then fj -X PATCH -H "Content-Type: application/json" -d '{"draft": false}' "${api}/releases/${rel_id}" > /dev/null log_info "The release is published" fi log_info "Done: ${REPO} ${TAG} is published and signed"