bb64/publish.sh
2025-05-17 08:04:18 +12:00

103 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
set -e
# Publishes bb64 (both arm64 and amd64) to the Gitea Releases page for the repository.
# Increments the version number in the VERSION file by one first.
# Builds the bb64 binary for arm64 and amd64.
# Uploads the binaries to the Gitea Releases page.
# Prints a message to the user.
# Increment version
if [ ! -f version.h ]; then
echo "version.h not found!" >&2
exit 1
else
v=$(cat version.h | grep -o 'static const char \*VERSION = "[0-9.]*";' | cut -d'"' -f2)
oldv=$v
v=$((v+1))
echo "Incrementing version from $oldv to $v" >&2
echo "static const char *VERSION = \"$v\";" > version.h
fi
TAG="v$v"
# Build binaries
./build.sh
# make sure we've commited.
git add . && git commit -m "bb64 release $TAG" && git push
# Find repo info from .git/config
REPO_URL=$(git config --get remote.origin.url)
if [[ ! $REPO_URL =~ gitea ]]; then
echo "Remote origin is not a Gitea repository: $REPO_URL" >&2
exit 1
fi
# Extract base URL, owner, and repo
# Example: https://gitea.example.com/username/reponame.git
BASE_URL=$(echo "$REPO_URL" | sed -E 's#(https?://[^/]+)/.*#\1#')
OWNER=$(echo "$REPO_URL" | sed -E 's#.*/([^/]+)/[^/]+(\.git)?$#\1#')
REPO=$(echo "$REPO_URL" | sed -E 's#.*/([^/]+)(\.git)?$#\1#')
API_URL="$BASE_URL/api/v1/repos/$OWNER/$REPO"
# Check for GITEA_TOKEN_DEPLOY or GITEA_TOKEN
if [ -n "$GITEA_TOKEN_DEPLOY" ]; then
TOKEN="$GITEA_TOKEN_DEPLOY"
elif [ -n "$GITEA_TOKEN" ]; then
TOKEN="$GITEA_TOKEN"
else
echo "GITEA_TOKEN_DEPLOY or GITEA_TOKEN environment variable not set!" >&2
exit 1
fi
# Create release
RELEASE_DATA=$(cat <<EOF
{
"tag_name": "$TAG",
"name": "$TAG",
"body": "bb64 release $TAG",
"draft": false,
"prerelease": false
}
EOF
)
RELEASE_ID=$(curl -s -X POST "$API_URL/releases" \
-H "Content-Type: application/json" \
-H "Authorization: token $TOKEN" \
-d "$RELEASE_DATA" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
if [ -z "$RELEASE_ID" ]; then
echo "Failed to create release on Gitea." >&2
exit 1
fi
# Upload binaries and install.sh
for FILE in bb64.amd64 bb64.arm64 install.sh; do
if [ -f "output/$FILE" ]; then
filetoupload="output/$FILE"
elif [ -f "$FILE" ]; then
filetoupload="$FILE"
else
continue
fi
# Auto-detect content type
ctype=$(file --mime-type -b "$filetoupload")
curl -s -X POST "$API_URL/releases/$RELEASE_ID/assets?name=$FILE" \
-H "Content-Type: $ctype" \
-H "Authorization: token $TOKEN" \
--data-binary @"$filetoupload"
echo "Uploaded $FILE to release $TAG as $ctype."
done
echo "Published bb64 version $v to $REPO_URL (tag $TAG) with binaries."