Files
test/.gitea/actions/get-version/action.yaml
2026-04-08 21:26:18 +08:00

61 lines
2.2 KiB
YAML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Get Version
description: 从 package.json 读取版本号,与最新 tag 对比决定使用哪个版本
outputs:
version:
description: 最终确定的版本号 (带 v 前缀)
value: ${{ steps.get_version.outputs.version }}
runs:
using: composite
steps:
- name: Get version from package.json
id: get_version
shell: bash
run: |
PKG_VERSION=$(node -p "require('./package.json').version")
echo "Package.json version: ${PKG_VERSION}"
# 获取最新的tag按版本号排序
LATEST_TAG=$(git ls-remote --tags origin 'refs/tags/v*' 2>/dev/null | sed 's/.*refs\/tags\///' | sed 's/\^{}//' | sort -V | tail -n 1 || echo "")
echo "Latest tag: ${LATEST_TAG}"
# 版本比较函数
version_compare() {
# 返回: 0 = 相等, 1 = v1 > v2, 2 = v1 < v2
if [[ "$1" == "$2" ]]; then echo 0; return; fi
local IFS=.
local i v1=($1) v2=($2)
for ((i=0; i<${#v1[@]} || i<${#v2[@]}; i++)); do
local n1=${v1[i]:-0} n2=${v2[i]:-0}
if ((n1 > n2)); then echo 1; return; fi
if ((n1 < n2)); then echo 2; return; fi
done
echo 0
}
if [[ -z "${LATEST_TAG}" ]]; then
# 没有任何tag直接使用package.json版本
NEW_TAG="v${PKG_VERSION}"
echo "No existing tags, using package.json version: ${NEW_TAG}"
else
# 去掉v前缀进行比较
LATEST_VERSION="${LATEST_TAG#v}"
COMPARE_RESULT=$(version_compare "${PKG_VERSION}" "${LATEST_VERSION}")
if [[ "${COMPARE_RESULT}" == "1" ]]; then
# package.json版本更大使用它
NEW_TAG="v${PKG_VERSION}"
echo "Package.json version is greater, using: ${NEW_TAG}"
else
# 最新tag版本更大或相等在其基础上递增patch
IFS='.' read -r MAJOR MINOR PATCH <<< "${LATEST_VERSION}"
PATCH=$((PATCH + 1))
NEW_TAG="v${MAJOR}.${MINOR}.${PATCH}"
echo "Incrementing from latest tag, new version: ${NEW_TAG}"
fi
fi
echo "version=${NEW_TAG}" >> $GITHUB_OUTPUT
echo "Final Version: ${NEW_TAG}"