SimpleAdmin/release.sh

112 lines
5 KiB
Bash
Executable file

#!/usr/bin/env bash
# Build our CS2-SimpleAdmin fork, package it as one tarball and publish it as a Forgejo release.
#
# ./release.sh <tag> e.g. ./release.sh v1.8.2b-zio1
#
# Commit first: the plugin is rebuilt here from the committed source, so the release always matches
# the tag. The tag is created on HEAD and pushed to origin if it doesn't exist yet. Re-running with
# the same tag replaces that release's attachment.
#
# Asset: SimpleAdmin-<tag>.tar.gz, laid out like the server's game/csgo/ - extract it there:
# tar -xzf SimpleAdmin-<tag>.tar.gz -C /srv/cs2/game/csgo
#
# addons/counterstrikesharp/plugins/CS2-SimpleAdmin/ the plugin, its libraries, lang/, migrations
# addons/counterstrikesharp/shared/CS2-SimpleAdminApi/ the shared API (unchanged from upstream)
#
# The whole plugin, packaged the way upstream's CI packages its release, so nothing of upstream's
# has to be installed first. Not included: the optional FunCommands and StealthModule modules, and
# the NickFox007 dependencies (AnyBaseLib, PlayerSettings, MenuManager), which pre.sh installs from
# their own releases. No configs/: the database settings stay the server's.
set -euo pipefail
FORGEJO_URL="https://git.zio.sh"
OWNER="cs2"
REPO="SimpleAdmin"
# Placeholder - replace, or set FORGEJO_TOKEN in the environment instead. Needs write:repository.
# Only uploading needs it; downloading from the public repo doesn't. Don't commit a real token.
FORGEJO_TOKEN="${FORGEJO_TOKEN:-CHANGE_ME_FORGEJO_TOKEN}"
TAG="${1:-}"
if [[ -z "$TAG" ]]; then
echo "usage: $0 <tag> (e.g. v1.8.2b-zio1)" >&2
exit 1
fi
if [[ "$FORGEJO_TOKEN" == "CHANGE_ME_FORGEJO_TOKEN" ]]; then
echo "Set FORGEJO_TOKEN (edit release.sh or export it) first." >&2
exit 1
fi
for tool in podman jq curl git; do
command -v "$tool" >/dev/null || { echo "'$tool' is required." >&2; exit 1; }
done
cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
if [[ -n "$(git status --porcelain --untracked-files=no)" ]]; then
echo "Tracked files have uncommitted changes; commit them so the tag matches the release." >&2
exit 1
fi
# The tag should name the version the plugin reports in `css_plugins list`.
version="$(sed -n 's/.*ModuleVersion => "\(.*\)";.*/\1/p' CS2-SimpleAdmin/CS2-SimpleAdmin.cs)"
if [[ "${TAG#v}" != "$version" ]]; then
echo "Tag $TAG doesn't match ModuleVersion $version in CS2-SimpleAdmin/CS2-SimpleAdmin.cs." >&2
exit 1
fi
# --- Build -----------------------------------------------------------------------
OUT="release-out"
STAGE="$OUT/stage"
CSS_DIR="addons/counterstrikesharp"
rm -rf CS2-SimpleAdmin/bin CS2-SimpleAdmin/obj CS2-SimpleAdminApi/bin CS2-SimpleAdminApi/obj "$OUT"
# dotnet build straight into the staged layout, as upstream's CI does: the csproj sets
# PublishTrimmed, which a plugin mustn't be. CounterStrikeSharp.API.dll is kept out by the csproj
# (ExcludeAssets=runtime); the server already provides it.
podman run --rm -v "$(pwd)":/src:Z -w /src mcr.microsoft.com/dotnet/sdk:10.0 sh -c "
dotnet build CS2-SimpleAdmin/CS2-SimpleAdmin.csproj -c Release -o '/src/$STAGE/$CSS_DIR/plugins/CS2-SimpleAdmin' &&
dotnet build CS2-SimpleAdminApi/CS2-SimpleAdminApi.csproj -c Release -o '/src/$STAGE/$CSS_DIR/shared/CS2-SimpleAdminApi'"
if find "$STAGE" -name CounterStrikeSharp.API.dll | grep -q .; then
echo "CounterStrikeSharp.API.dll ended up in the build output; not packaging it." >&2
exit 1
fi
# --- Package (layout of game/csgo/) ------------------------------------------------
TARBALL="SimpleAdmin-${TAG}.tar.gz"
tar -czf "$OUT/$TARBALL" -C "$STAGE" addons
rm -rf "$STAGE"
echo "Packaged:"
tar -tzf "$OUT/$TARBALL"
# --- Tag ---------------------------------------------------------------------------
if ! git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
git tag -a "$TAG" -m "$TAG"
fi
git push origin "refs/tags/$TAG"
# --- Release -----------------------------------------------------------------------
API="$FORGEJO_URL/api/v1/repos/$OWNER/$REPO"
AUTH=(-H "Authorization: token $FORGEJO_TOKEN")
release_json="$(curl -fsS "${AUTH[@]}" "$API/releases/tags/$TAG" 2>/dev/null || true)"
if [[ -z "$release_json" ]]; then
body="$(git log -1 --format=%B "$TAG")"
release_json="$(jq -n --arg tag "$TAG" --arg body "$body" \
'{tag_name: $tag, name: $tag, body: $body, draft: false, prerelease: false}' |
curl -fsS "${AUTH[@]}" -H "Content-Type: application/json" -X POST --data @- "$API/releases")"
echo "Created release $TAG"
else
echo "Release $TAG already exists, replacing its attachment"
fi
release_id="$(jq -r '.id' <<<"$release_json")"
existing_id="$(jq -r --arg n "$TARBALL" '.assets[]? | select(.name == $n) | .id' <<<"$release_json")"
if [[ -n "$existing_id" ]]; then
curl -fsS "${AUTH[@]}" -X DELETE "$API/releases/$release_id/assets/$existing_id" >/dev/null
fi
curl -fsS "${AUTH[@]}" -X POST -F "attachment=@$OUT/$TARBALL" \
"$API/releases/$release_id/assets?name=$TARBALL" >/dev/null
echo "Uploaded $TARBALL"
echo
echo "Download URL:"
echo " $FORGEJO_URL/$OWNER/$REPO/releases/download/$TAG/$TARBALL"