#!/bin/bash set -euo pipefail readonly LINE="==================================================" readonly SUBLINE="--------------------------------------------------" section() { printf '\n%s\n%s\n%s\n' "$LINE" "$1" "$SUBLINE" } info() { printf '[info] %s\n' "$1" } fail() { printf '[error] %s\n' "$1" >&2 exit 1 } require_env() { local name="$1" local value="$2" if [ -z "$value" ] || [ "$value" = "null" ]; then fail "$name 未設定" fi printf '%s=%s\n' "$name" "$value" } write_output() { printf 'version=%s\n' "$1" >> "$GITHUB_OUTPUT" } section "參數檢查" require_env "GITEA_SERVER_URL" "${GITEA_SERVER_URL:-}" require_env "GITEA_REPOSITORY" "${GITEA_REPOSITORY:-}" if [ -n "${RUNNER_TOKEN:-}" ] && [ "${RUNNER_TOKEN:-}" != "null" ]; then printf 'RUNNER_TOKEN=%s\n' '***' else printf 'RUNNER_TOKEN=%s\n' '未提供' fi IS_BETA="${IS_BETA:-false}" if [ "$IS_BETA" = "null" ] || [ -z "$IS_BETA" ]; then IS_BETA="false" fi printf 'IS_BETA=%s\n' "$IS_BETA" section "取得版本資料" RELEASE_URL="$GITEA_SERVER_URL/api/v1/repos/$GITEA_REPOSITORY/releases" info "RELEASE_URL=$RELEASE_URL" if [ -n "${RUNNER_TOKEN:-}" ] && [ "${RUNNER_TOKEN:-}" != "null" ]; then info "使用授權 token 取得 release" RELEASE_JSON="$(curl -fsS -H "Authorization: token $RUNNER_TOKEN" "$RELEASE_URL")" else info "使用匿名請求取得 release" RELEASE_JSON="$(curl -fsS "$RELEASE_URL")" fi LATEST_VERSION="$( printf '%s' "$RELEASE_JSON" | jq -r ' [ .[] | select(.tag_name | test("-beta\\.") | not) | .tag_name ][0] // "v0.0.0" ' | sed 's/^v//' )" if [ -z "$LATEST_VERSION" ] || [ "$LATEST_VERSION" = "null" ]; then LATEST_VERSION="0.0.0" fi info "LATEST_VERSION=$LATEST_VERSION" section "計算版本號" IFS='.' read -r MAJOR MINOR PATCH <<< "$LATEST_VERSION" MAJOR="${MAJOR:-0}" MINOR="${MINOR:-0}" PATCH="${PATCH:-0}" PATCH=$((PATCH + 1)) if [ "$PATCH" -ge 10 ]; then PATCH=0 MINOR=$((MINOR + 1)) fi if [ "$MINOR" -ge 10 ]; then MINOR=0 MAJOR=$((MAJOR + 1)) fi NEW_VERSION="$MAJOR.$MINOR.$PATCH" if [ "$IS_BETA" = "true" ]; then BETA="$( printf '%s' "$RELEASE_JSON" | jq -r --arg prefix "v$NEW_VERSION-beta." ' [ .[] | select(.tag_name | startswith($prefix)) | .tag_name | ltrimstr($prefix) | tonumber ] | if length > 0 then max else 0 end ' )" BETA=$((BETA + 1)) NEW_VERSION="$NEW_VERSION-beta.$BETA" fi info "NEW_VERSION=$NEW_VERSION" write_output "$NEW_VERSION"