From e1094d96a2828ea0d9410136bf9e4474bc20a3c8 Mon Sep 17 00:00:00 2001 From: Overlord Date: Sat, 3 Oct 2026 18:36:33 +0200 Subject: [PATCH] Add CI pipeline: automated builds and releases for versioned tags. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/release.yml | 34 ++++++ .gitignore | 1 + README.md | 18 ++- ci/release.sh | 213 +++++++++++++++++++++++++++++++++++ 4 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 .gitea/workflows/release.yml create mode 100755 ci/release.sh diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml new file mode 100644 index 0000000..bfec4d2 --- /dev/null +++ b/.gitea/workflows/release.yml @@ -0,0 +1,34 @@ +# Builds the mod for every v* tag and publishes it as a release with lua_mod_automator.zip +# attached, so .../releases/download/latest/lua_mod_automator.zip always serves the newest one. +# Pre-release tags (containing "-") are built and tested but not released (see ci/release.sh). +# Uses the repository secret RELEASE_TOKEN (a token with "repository: Read and Write") when it +# exists, else the job's automatic token. +name: release + +on: + push: + tags: + - "v*" + +jobs: + release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + # release.sh reads the v* tag on HEAD + fetch-depth: 0 + + - uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4.9.1 + with: + distribution: temurin + java-version: "25" + + # The build runs without the token; only the publish step gets it. + - name: Build + run: ci/release.sh build + + - name: Publish + env: + GITEA_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITEA_TOKEN }} + run: ci/release.sh publish diff --git a/.gitignore b/.gitignore index 4d10ad4..e122e0a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ !README.md /lib .*/ +!.gitea/ build/ out/ diff --git a/README.md b/README.md index 3fdb323..ba9d5bd 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,18 @@ Players keep the official, unmodified client; the mod runs on the server only. ## Installation -1. Unpack `lua_mod_automator-.zip` into `/mods/`, which gives - `/mods/lua_mod_automator/`. +1. Download the newest release and unpack it into `/mods/`, which gives + `/mods/lua_mod_automator/`: + + ```shell + curl --fail --location --output lua_mod_automator.zip \ + https://git.security-command.org/Overlord/LuaModAutomator/releases/download/latest/lua_mod_automator.zip + unzip -o lua_mod_automator.zip -d /mods/ + ``` + + Every release also carries `lua_mod_automator-.zip` for a fixed version. To update, + remove `lua_mod_automator.jar` and `lib/` from the mod's folder first and unpack the new zip; + `config.toml` is not part of the zip and stays. 2. Create `/mods/lua_mod_automator/config.toml` with at least the collection: ```toml @@ -152,6 +162,10 @@ is idle. To try it with a collection, copy `build/mod/lua_mod_automator/` into a directory (or set `zomboidForge.deployDirectory` and run `./gradlew deployMod`) and add a `config.toml` there. +Pushing a tag `v` that matches `version` in `build.gradle.kts` builds the mod and +publishes it as a release (`.gitea/workflows/release.yml`, `ci/release.sh`). Tags containing +`-` (e.g. `v0.2.0-rc.1`) are built and tested but not released. + ## License [GNU Affero General Public License v3.0](LICENSE) diff --git a/ci/release.sh b/ci/release.sh new file mode 100755 index 0000000..960e781 --- /dev/null +++ b/ci/release.sh @@ -0,0 +1,213 @@ +#!/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-.zip, and as lua_mod_automator.zip, the stable +# name behind ///releases/download/latest/lua_mod_automator.zip. +# +# A pre-release tag (containing "-", e.g. v0.2.0-rc.1) 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 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 +} + +# 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 + + : "${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 "$@"