Releases on Gitea had nothing to install, so getting remus onto a Steam machine meant building the bundle and copying it across by hand. After a bump and a push with tags, make publish builds the Flatpak bundle for the tagged commit and creates the release, with the changelog entry as its notes and remus-X.Y.Z.flatpak attached. Every check runs before the slow build: a clean tree, HEAD tagged with the Cargo.toml version, a token, the same tag on the remote and no release for it yet. The token is read from GITEA_TOKEN or ~/.config/gitea/token and passed to curl in a header file, so it is never on a command line. If the upload fails, the release is deleted so the next run starts clean.
120 lines
4.6 KiB
Bash
Executable File
120 lines
4.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Publishes the tagged version on HEAD as a Gitea release with the Flatpak
|
|
# bundle attached. Run by `make publish`, after a bump and a push with tags.
|
|
#
|
|
# Every check runs before the slow Flatpak build, so a refusal costs seconds.
|
|
#
|
|
# Environment, set by the Makefile:
|
|
# VERSION version in Cargo.toml
|
|
# GITEA_URL e.g. https://gitea.anshorei.me
|
|
# GITEA_REPO owner/name, e.g. Fettleshed/Remus
|
|
# GITEA_REMOTE git remote that holds the tags
|
|
# GITEA_TOKEN or GITEA_TOKEN_FILE API token with write:repository
|
|
|
|
set -euo pipefail
|
|
|
|
fail() {
|
|
echo "error: $*" >&2
|
|
exit 1
|
|
}
|
|
|
|
for tool in curl jq git awk; do
|
|
command -v "$tool" >/dev/null || fail "$tool is not installed"
|
|
done
|
|
[ -n "${VERSION:-}" ] || fail "no version found in the [package] table of Cargo.toml"
|
|
tag="v$VERSION"
|
|
api="${GITEA_URL%/}/api/v1/repos/$GITEA_REPO"
|
|
asset="remus-$VERSION.flatpak"
|
|
|
|
# The bundle is built from the working tree, so it has to be the tagged commit.
|
|
if [ -n "$(git status --porcelain)" ]; then
|
|
echo "error: working tree is dirty, and the bundle would not be what $tag names:" >&2
|
|
git status --short >&2
|
|
exit 1
|
|
fi
|
|
head_tags=$(git tag --points-at HEAD --list 'v*')
|
|
if ! grep -qxF "$tag" <<<"$head_tags"; then
|
|
if [ -n "$head_tags" ]; then
|
|
fail "HEAD is tagged $(paste -sd' ' <<<"$head_tags"), but Cargo.toml says $VERSION"
|
|
fi
|
|
fail "HEAD is not tagged $tag. Check out the tag, or bump first with make patch, minor or major."
|
|
fi
|
|
|
|
# The token goes to curl in a header file, so it is never on a command line
|
|
# or in the output.
|
|
if [ -n "${GITEA_TOKEN:-}" ]; then
|
|
token=$GITEA_TOKEN
|
|
elif [ -n "${GITEA_TOKEN_FILE:-}" ] && [ -r "$GITEA_TOKEN_FILE" ]; then
|
|
token=$(tr -d '[:space:]' <"$GITEA_TOKEN_FILE")
|
|
else
|
|
fail "no Gitea token. Set GITEA_TOKEN, or put one in ${GITEA_TOKEN_FILE:-a file named by GITEA_TOKEN_FILE}. It needs write:repository."
|
|
fi
|
|
[ -n "$token" ] || fail "the Gitea token is empty"
|
|
tmp=$(mktemp -d)
|
|
trap 'rm -rf "$tmp"' EXIT
|
|
(umask 077 && printf 'Authorization: token %s\n' "$token" >"$tmp/auth")
|
|
unset token GITEA_TOKEN
|
|
|
|
# Calls the API. Prints the body and returns the HTTP status in $status.
|
|
call() {
|
|
local method=$1 url=$2
|
|
shift 2
|
|
status=$(curl -sS -o "$tmp/body" -w '%{http_code}' -X "$method" -H @"$tmp/auth" "$@" "$url") ||
|
|
fail "could not reach $GITEA_URL"
|
|
}
|
|
|
|
# The local tag object has to be the one on the remote, or the release would
|
|
# point at a tag that names another commit, or at none.
|
|
remote_tag=$(git ls-remote --tags "$GITEA_REMOTE" "refs/tags/$tag" | cut -f1) ||
|
|
fail "could not list the tags on $GITEA_REMOTE"
|
|
[ -n "$remote_tag" ] || fail "$tag is not on $GITEA_REMOTE yet. Push it first: git push --follow-tags $GITEA_REMOTE"
|
|
[ "$remote_tag" = "$(git rev-parse "refs/tags/$tag")" ] ||
|
|
fail "$tag on $GITEA_REMOTE is not the local $tag. Find out which one is right before publishing."
|
|
|
|
notes=$(awk -v h="## $tag " '
|
|
index($0, h) == 1 { found = 1; next }
|
|
found && /^## / { exit }
|
|
found { print }
|
|
' CHANGELOG.md | sed -e '/./,$!d')
|
|
[ -n "$notes" ] || fail "CHANGELOG.md has no text under '## $tag'"
|
|
|
|
call GET "$api"
|
|
case $status in
|
|
200) ;;
|
|
401 | 403) fail "Gitea refused the token ($status). It needs write:repository on $GITEA_REPO." ;;
|
|
404) fail "no repository $GITEA_REPO on $GITEA_URL, or the token cannot see it" ;;
|
|
*) fail "checking $GITEA_REPO returned HTTP $status: $(cat "$tmp/body")" ;;
|
|
esac
|
|
call GET "$api/releases/tags/$tag"
|
|
case $status in
|
|
404) ;;
|
|
200) fail "$tag is already published: $(jq -r .html_url "$tmp/body"). Delete that release on Gitea to publish it again." ;;
|
|
*) fail "checking for an existing release returned HTTP $status: $(cat "$tmp/body")" ;;
|
|
esac
|
|
|
|
echo "Building the Flatpak bundle for $tag"
|
|
"${MAKE:-make}" --no-print-directory flatpak-bundle
|
|
cp target/flatpak/remus.flatpak "target/flatpak/$asset"
|
|
|
|
body=$(jq -n --arg tag "$tag" --arg notes "$notes" \
|
|
'{tag_name: $tag, name: $tag, body: $notes, draft: false, prerelease: false}')
|
|
call POST "$api/releases" -H 'Content-Type: application/json' --data "$body"
|
|
[ "$status" = 201 ] || fail "creating the release returned HTTP $status: $(cat "$tmp/body")"
|
|
id=$(jq -r .id "$tmp/body")
|
|
url=$(jq -r .html_url "$tmp/body")
|
|
|
|
call POST "$api/releases/$id/assets?name=$asset" -F "attachment=@target/flatpak/$asset"
|
|
if [ "$status" != 201 ]; then
|
|
echo "error: uploading $asset returned HTTP $status: $(cat "$tmp/body")" >&2
|
|
# A release without its bundle would block the next try, so take it back.
|
|
call DELETE "$api/releases/$id"
|
|
if [ "$status" = 204 ]; then
|
|
echo "Removed the release again, so make publish can be rerun." >&2
|
|
else
|
|
echo "Could not remove the release ($status). Delete $url by hand before rerunning." >&2
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
echo "Published $tag with $asset: $url"
|