Publish installable desktop releases on main updates
All checks were successful
Publish desktop game builds / release (push) Successful in 4m14s
All checks were successful
Publish desktop game builds / release (push) Successful in 4m14s
This commit is contained in:
149
Scripts/release.sh
Normal file
149
Scripts/release.sh
Normal file
@@ -0,0 +1,149 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
cd -- "$(dirname -- "$0")/.."
|
||||
project_dir=$PWD
|
||||
version=$(awk '/^[[:space:]]+bundleVersion:/ { print $2; exit }' ProjectSettings/ProjectSettings.asset)
|
||||
commit=$(git rev-parse HEAD)
|
||||
if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z.-]+)?$ ]]; then
|
||||
printf 'Expected a release version in ProjectSettings.asset, got %s\n' "$version" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ -n ${GITHUB_SHA:-} && $commit != "$GITHUB_SHA" ]]; then
|
||||
printf 'The checked-out commit does not match the update that triggered this build.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
tag="v$version-${commit:0:12}"
|
||||
package="BigShipTycoon-$tag"
|
||||
out="$project_dir/Builds/Release/$tag"
|
||||
linux_archive="$package-linux-x86_64.tar.gz"
|
||||
windows_archive="$package-windows-x86_64.zip"
|
||||
assets=("$linux_archive" "$windows_archive" SHA256SUMS)
|
||||
work_dir=
|
||||
trap 'if [[ -n $work_dir ]]; then rm -rf -- "$work_dir"; fi' EXIT
|
||||
|
||||
run_unity() {
|
||||
local target=$1 player=$2 log="$out/$1.log"
|
||||
printf 'Building %s with Unity %s\n' "$target" "$editor_version"
|
||||
if ! BIG_SHIP_TYCOON_BUILD_PATH="$player" "$editor" \
|
||||
-batchmode -nographics -quit -projectPath "$project_dir" -buildTarget "$target" \
|
||||
-executeMethod BigShipTycoon.Editor.TycoonBuild.VerifyAndBuild -logFile "$log"; then
|
||||
tail -n 100 "$log" >&2
|
||||
return 1
|
||||
fi
|
||||
grep -F 'Big Ship Tycoon economy verification passed' "$log"
|
||||
grep -F 'Built Big Ship Tycoon for ' "$log"
|
||||
}
|
||||
|
||||
build_packages() {
|
||||
editor_version=$(awk '/^m_EditorVersion:/ { print $2; exit }' ProjectSettings/ProjectVersion.txt)
|
||||
local hub_root=${UNITY_HUB_ROOT:-"$HOME/Unity/Hub/Editor"}
|
||||
editor=${UNITY:-"$hub_root/$editor_version/Editor/Unity"}
|
||||
if [[ ! -x $editor ]]; then
|
||||
printf 'Install Unity %s with Linux and Windows build support or set UNITY.\n' "$editor_version" >&2
|
||||
return 1
|
||||
fi
|
||||
mkdir -p "$out"
|
||||
work_dir=$(mktemp -d "$project_dir/Builds/.release.XXXXXXXX")
|
||||
local linux_dir="$work_dir/linux/$package" windows_dir="$work_dir/windows/$package"
|
||||
run_unity Linux64 "$linux_dir/BigShipTycoon.x86_64"
|
||||
run_unity Win64 "$windows_dir/BigShipTycoon.exe"
|
||||
[[ -x $linux_dir/BigShipTycoon.x86_64 && -f $linux_dir/UnityPlayer.so && -d $linux_dir/BigShipTycoon_Data ]]
|
||||
[[ -f $windows_dir/BigShipTycoon.exe && -f $windows_dir/UnityPlayer.dll && -d $windows_dir/BigShipTycoon_Data ]]
|
||||
|
||||
printf 'Big Ship Tycoon %s\n\nExtract the entire archive, then run ./BigShipTycoon.x86_64 from this folder.\nKeep the executable and data folder together.\n' "$tag" > "$linux_dir/START-HERE.txt"
|
||||
printf 'Big Ship Tycoon %s\r\n\r\nExtract the entire ZIP, then open BigShipTycoon.exe inside this folder.\r\nKeep the executable and data folder together.\r\n' "$tag" > "$windows_dir/START-HERE.txt"
|
||||
tar -czf "$work_dir/$linux_archive" -C "$work_dir/linux" "$package"
|
||||
(cd "$work_dir/windows" && zip -q -r "$work_dir/$windows_archive" "$package")
|
||||
(cd "$work_dir" && sha256sum "$linux_archive" "$windows_archive" > SHA256SUMS)
|
||||
local asset
|
||||
for asset in "${assets[@]}"; do
|
||||
mv -- "$work_dir/$asset" "$out/$asset"
|
||||
done
|
||||
printf 'Packages ready in %s\n' "$out"
|
||||
}
|
||||
|
||||
release_api() {
|
||||
local method=$1 endpoint=$2 expected=$3
|
||||
shift 3
|
||||
http_status=$(curl --silent --show-error --connect-timeout 20 --max-time 480 \
|
||||
--header "@$work_dir/auth-header" --header 'Accept: application/json' \
|
||||
--request "$method" --output "$work_dir/response.json" --write-out '%{http_code}' \
|
||||
"$@" "$api/releases$endpoint")
|
||||
if [[ " $expected " != *" $http_status "* ]]; then
|
||||
printf 'Gitea API %s %s returned HTTP %s\n' "$method" "$endpoint" "$http_status" >&2
|
||||
jq -r '.message // empty' "$work_dir/response.json" >&2 || true
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
publish_packages() {
|
||||
: "${GITHUB_REPOSITORY:?Expected the Gitea repository name}" "${GITHUB_SERVER_URL:?Expected the Gitea server}" \
|
||||
"${RELEASE_TOKEN:?Expected the Gitea release token}"
|
||||
(cd "$out" && sha256sum --check SHA256SUMS)
|
||||
api="${GITHUB_SERVER_URL%/}/api/v1/repos/$GITHUB_REPOSITORY"
|
||||
work_dir=$(mktemp -d)
|
||||
(umask 077; printf 'Authorization: token %s\n' "$RELEASE_TOKEN" > "$work_dir/auth-header")
|
||||
|
||||
local body release id asset name old_asset_id page
|
||||
release_api GET "/tags/$tag" '200 404'
|
||||
if [[ $http_status == 200 ]]; then
|
||||
release=$(cat "$work_dir/response.json")
|
||||
else
|
||||
# Some configurations omit drafts from the single-tag endpoint.
|
||||
release=
|
||||
for ((page=1; ; page++)); do
|
||||
release_api GET "?draft=true&limit=50&page=$page" 200
|
||||
release=$(jq -c --arg tag "$tag" '.[] | select(.tag_name == $tag)' "$work_dir/response.json")
|
||||
[[ -z $release && $(jq 'length' "$work_dir/response.json") -gt 0 ]] || break
|
||||
done
|
||||
fi
|
||||
|
||||
if [[ -z $release ]]; then
|
||||
body=$(printf '%s\n\n%s\n\n%s\n' \
|
||||
"$(git log -1 --format=%s)" \
|
||||
'Download the Windows ZIP or Linux tar.gz and extract the entire archive. Run BigShipTycoon.exe on Windows or BigShipTycoon.x86_64 on Linux.' \
|
||||
"Build commit: $commit")
|
||||
jq -n --arg tag "$tag" --arg commit "$commit" --arg name "Big Ship Tycoon $tag" --arg body "$body" \
|
||||
'{tag_name: $tag, target_commitish: $commit, name: $name, body: $body, draft: true}' > "$work_dir/draft.json"
|
||||
release_api POST '' 201 --json "@$work_dir/draft.json"
|
||||
release=$(cat "$work_dir/response.json")
|
||||
fi
|
||||
|
||||
if [[ $(jq -r '.target_commitish' <<< "$release") != "$commit" ]]; then
|
||||
printf 'Release %s targets a different commit.\n' "$tag" >&2
|
||||
return 1
|
||||
fi
|
||||
if [[ $(jq -r '.draft' <<< "$release") == false ]]; then
|
||||
for name in "${assets[@]}"; do
|
||||
jq -e --arg name "$name" 'any(.assets[]?; .name == $name and .size > 0)' <<< "$release" >/dev/null
|
||||
done
|
||||
printf 'Release %s is already published.\n' "$tag"
|
||||
return
|
||||
fi
|
||||
|
||||
id=$(jq -er '.id' <<< "$release")
|
||||
for name in "${assets[@]}"; do
|
||||
asset="$out/$name"
|
||||
# Replace artifacts from an incomplete draft so the checksum file matches this build.
|
||||
old_asset_id=$(jq -r --arg name "$name" '.assets[]? | select(.name == $name) | .id' <<< "$release")
|
||||
if [[ -n $old_asset_id ]]; then
|
||||
release_api DELETE "/$id/assets/$old_asset_id" 204
|
||||
fi
|
||||
printf 'Uploading %s\n' "$name"
|
||||
release_api POST "/$id/assets" 201 --form "attachment=@$asset;type=application/octet-stream"
|
||||
jq -e --arg name "$name" --argjson size "$(stat -c %s "$asset")" \
|
||||
'.name == $name and .size == $size' "$work_dir/response.json" >/dev/null
|
||||
done
|
||||
|
||||
release_api PATCH "/$id" 200 --json '{"draft":false}'
|
||||
jq -e '.draft == false' "$work_dir/response.json" >/dev/null
|
||||
printf 'Published %s/%s/releases/tag/%s\n' "${GITHUB_SERVER_URL%/}" "$GITHUB_REPOSITORY" "$tag"
|
||||
}
|
||||
|
||||
case ${1:-} in
|
||||
build) build_packages ;;
|
||||
publish) publish_packages ;;
|
||||
*) printf 'Usage: %s build|publish\n' "$0" >&2; exit 2 ;;
|
||||
esac
|
||||
Reference in New Issue
Block a user