Files
LuaModAutomator/ci/release.sh
T

226 lines
7.3 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Builds the mod for the v* git tag on HEAD and publishes it as a Gitea release, with the zip
# attached twice: as lua_mod_automator-<version>.zip, and as lua_mod_automator.zip, the stable
# name behind <server>/<owner>/<repo>/releases/download/latest/lua_mod_automator.zip.
#
# A pre-release tag (containing "-", e.g. v0.2.0-rc.1), or a tag on a commit that is not on
# master, is built and tested but not released. The tag must match the version in
# build.gradle.kts.
#
# Usage:
# ci/release.sh build downloads the game jar and builds the mod (tests and checkMod included)
# ci/release.sh publish creates the release (or reuses the tag's release) and uploads the zip
#
# Environment:
# GAME_JAR projectzomboid.jar to build against (build); default: downloaded from
# the Steam branch PZ_BRANCH (default: public) with DepotDownloader
# GITEA_TOKEN token with write access to the repository's releases (publish)
# GITHUB_API_URL Gitea API, e.g. https://git.security-command.org/api/v1 (publish; set by Gitea Actions)
# GITHUB_REPOSITORY owner/repo (publish; set by Gitea Actions)
set -o errexit
set -o nounset
set -o pipefail
shopt -s inherit_errexit
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
readonly REPO_ROOT
readonly MOD_ID="lua_mod_automator"
readonly RELEASE_BRANCH="master"
readonly WORK_DIR="${REPO_ROOT}/build/ci"
# DepotDownloader downloads single files of a depot; steamcmd only whole apps. Same release
# and checksum as ZomboidForge's image build.
readonly DEPOT_DOWNLOADER_URL="https://github.com/SteamRE/DepotDownloader/releases/download/DepotDownloader_3.4.0/DepotDownloader-linux-x64.zip"
readonly DEPOT_DOWNLOADER_SHA256="a999dec66b4850fc961bd50366696d23c2d0fad7b18790e6a5647b2f19097a53"
# Project Zomboid Dedicated Server: app and its depot with java/projectzomboid.jar.
readonly PZ_APP="380870"
readonly PZ_DEPOT="380871"
log() {
echo "[release] $*" >&2
}
fail() {
log "$*"
exit 1
}
# Whether HEAD is on the release branch, as fetched from origin.
on_release_branch() {
git -C "${REPO_ROOT}" merge-base --is-ancestor HEAD "origin/${RELEASE_BRANCH}" 2> /dev/null
}
# Prints the v* tag on HEAD, or nothing.
release_tag() {
git -C "${REPO_ROOT}" describe --tags --exact-match --match 'v*' 2> /dev/null || true
}
run_gradle() {
"${REPO_ROOT}/gradlew" --no-daemon --console=plain --project-dir "${REPO_ROOT}" "$@"
}
project_version() {
run_gradle --quiet properties --property version | sed --quiet 's/^version: //p'
}
# Downloads java/projectzomboid.jar of the dedicated server and prints its path.
download_game_jar() {
local branch="${PZ_BRANCH:-public}"
local tools="${WORK_DIR}/depotdownloader"
local game="${WORK_DIR}/game"
mkdir --parents "${tools}" "${game}"
log "Downloading DepotDownloader"
curl --fail --silent --show-error --location --output "${tools}/depotdownloader.zip" "${DEPOT_DOWNLOADER_URL}"
echo "${DEPOT_DOWNLOADER_SHA256} ${tools}/depotdownloader.zip" | sha256sum --check --quiet
unzip -q -o "${tools}/depotdownloader.zip" -d "${tools}"
chmod +x "${tools}/DepotDownloader"
log "Downloading projectzomboid.jar of the Steam branch '${branch}'"
echo "java/projectzomboid.jar" > "${tools}/files"
"${tools}/DepotDownloader" -app "${PZ_APP}" -depot "${PZ_DEPOT}" -branch "${branch}" -filelist "${tools}/files" -dir "${game}" >&2
echo "${game}/java/projectzomboid.jar"
}
build() {
local tag="$1"
local version="${tag#v}"
local project
local game_jar="${GAME_JAR:-}"
project="$(project_version)"
if [[ "${project}" != "${version}" ]]; then
fail "The tag '${tag}' does not match the version '${project}' in build.gradle.kts."
fi
if [[ -z "${game_jar}" ]]; then
game_jar="$(download_game_jar)"
fi
log "Building ${MOD_ID} ${version} against '${game_jar}'"
run_gradle "-Pzomboidforge.gameJar=${game_jar}" build
}
# Calls the Gitea API; prints the response body. Extra arguments go to curl.
api() {
local method="$1"
local path="$2"
shift 2
curl --fail --silent --show-error \
--request "${method}" \
--header "Authorization: token ${GITEA_TOKEN}" \
--header "Accept: application/json" \
"$@" \
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}${path}"
}
# Prints the id of the tag's release, created when it does not exist yet.
release_id() {
local tag="$1"
local status
local body
status="$(curl --silent --output /dev/null --write-out '%{http_code}' \
--header "Authorization: token ${GITEA_TOKEN}" \
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/tags/${tag}")"
case "${status}" in
200)
log "Release '${tag}' exists; replacing its files"
api GET "/releases/tags/${tag}" | jq --raw-output '.id'
return
;;
404)
;;
*)
fail "Cannot read the releases of '${GITHUB_REPOSITORY}' (HTTP ${status}); is GITEA_TOKEN valid for it?"
;;
esac
body="$(jq --null-input \
--arg tag "${tag}" \
--arg name "Release ${tag} - Stable, Automated" \
--arg body "$(date --utc +%Y-%m-%d)"$'\n'"$(git -C "${REPO_ROOT}" rev-parse HEAD)" \
'{tag_name: $tag, name: $name, body: $body, draft: false, prerelease: false}')"
log "Creating release '${tag}'"
api POST "/releases" --header "Content-Type: application/json" --data "${body}" | jq --raw-output '.id'
}
# Uploads ${file} as ${name}, replacing an attached file of that name.
upload() {
local id="$1"
local file="$2"
local name="$3"
local existing
existing="$(api GET "/releases/${id}/assets" | jq --raw-output --arg name "${name}" '.[] | select(.name == $name) | .id')"
if [[ -n "${existing}" ]]; then
api DELETE "/releases/${id}/assets/${existing}" > /dev/null
fi
api POST "/releases/${id}/assets?name=${name}" --form "attachment=@${file}" > /dev/null
log "Attached '${name}'"
}
publish() {
local tag="$1"
local version="${tag#v}"
local zip="${REPO_ROOT}/build/distributions/${MOD_ID}-${version}.zip"
local id
if [[ "${version}" == *-* ]]; then
log "${tag} is a pre-release tag; nothing is released."
return
fi
if ! on_release_branch; then
log "${tag} is not on ${RELEASE_BRANCH}; nothing is released."
return
fi
: "${GITEA_TOKEN:?GITEA_TOKEN is not set}"
: "${GITHUB_API_URL:?GITHUB_API_URL is not set}"
: "${GITHUB_REPOSITORY:?GITHUB_REPOSITORY is not set}"
[[ -f "${zip}" ]] || fail "'${zip}' does not exist; run 'ci/release.sh build' first."
id="$(release_id "${tag}")"
upload "${id}" "${zip}" "${MOD_ID}-${version}.zip"
upload "${id}" "${zip}" "${MOD_ID}.zip"
log "Released ${tag}: ${GITHUB_SERVER_URL:-}/${GITHUB_REPOSITORY}/releases/tag/${tag}"
}
main() {
local step="${1:-}"
local tag
tag="$(release_tag)"
if [[ -z "${tag}" ]]; then
fail "HEAD has no v* git tag (not a release commit, or a shallow clone without tags?); nothing to release."
fi
case "${step}" in
build)
build "${tag}"
;;
publish)
publish "${tag}"
;;
*)
log "Usage: ci/release.sh build|publish"
exit 2
;;
esac
}
main "$@"