#!/bin/bash # Publish a darthvader release to Gitea: creates (or reuses) the tag/release and # uploads the given files as attachments, plus a SHA256SUMS of them. # # scripts/release.sh v0.1.0-244ef5d9 ../darthvader/dist-win-installers/*.exe ... # # The token is read from $GITEA_TOKEN, else from the file in $GITEA_TOKEN_FILE # (default ~/.gitea-token). It is passed to curl on stdin, so it never appears # in the command line, the process list or this repository. set -euo pipefail GITEA_URL=${GITEA_URL:-https://git.sa7aux.se} OWNER=${GITEA_OWNER:-bjorne} REPO=${GITEA_REPO:-darthvader-dist} TOKEN_FILE=${GITEA_TOKEN_FILE:-$HOME/.gitea-token} TAG=${1:-} shift || true if [ -z "$TAG" ] || [ $# -eq 0 ]; then echo "usage: $0 [file...]" >&2 exit 1 fi TOKEN=${GITEA_TOKEN:-} if [ -z "$TOKEN" ]; then [ -r "$TOKEN_FILE" ] || { echo "no token: set GITEA_TOKEN or put one in $TOKEN_FILE" >&2; exit 1; } TOKEN=$(tr -d ' \t\r\n' < "$TOKEN_FILE") fi API="$GITEA_URL/api/v1/repos/$OWNER/$REPO" # curl reads the auth header from stdin (never argv); "$@" after it is the request api() { printf 'header = "Authorization: token %s"\n' "$TOKEN" | curl -sS --fail-with-body --config - "$@"; } # Checksums next to the files, over the basenames only WORK=$(mktemp -d) trap 'rm -rf "$WORK"' EXIT for f in "$@"; do [ -r "$f" ] || { echo "missing file: $f" >&2; exit 1; } (cd "$(dirname "$f")" && sha256sum "$(basename "$f")") >> "$WORK/SHA256SUMS" done echo "release $TAG -> $OWNER/$REPO" ID=$(api "$API/releases/tags/$TAG" 2>/dev/null | grep -oE '"id":[0-9]+' | head -1 | cut -d: -f2 || true) if [ -z "$ID" ]; then ID=$(api -X POST -H "Content-Type: application/json" \ -d "{\"tag_name\":\"$TAG\",\"name\":\"darthvader $TAG\",\"body\":\"See CHANGELOG.md\",\"draft\":false,\"prerelease\":false}" \ "$API/releases" | grep -oE '"id":[0-9]+' | head -1 | cut -d: -f2) echo " created release id=$ID" else echo " reusing release id=$ID" fi [ -n "$ID" ] || { echo "could not create or find the release" >&2; exit 1; } for f in "$@" "$WORK/SHA256SUMS"; do name=$(basename "$f") echo " uploading $name ($(du -h "$f" | cut -f1))" api -X POST -F "attachment=@$f" "$API/releases/$ID/assets?name=$name" > /dev/null done echo "done: $GITEA_URL/$OWNER/$REPO/releases/tag/$TAG"