allocatr/deploy-if-new-tag.sh
2025-06-20 18:08:25 +08:00

45 lines
1.3 KiB
Bash
Executable File
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.

#!/bin/bash
set -e
SITE_FILE="application/extra/site.php"
# 获取本地当前 tag假设是当前 HEAD 所在 tag
current_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.0.0")
# 拉取最新 tags
git fetch --tags
# 获取远程最新 tag
latest_tag=$(git tag --sort=-v:refname | head -n 1)
echo "当前 tag: ${current_tag}"
echo "远程最新 tag: ${latest_tag}"
# 函数:将 tag 转换成整数用于比较
version_to_int() {
local IFS=.
read -r major minor patch <<< "$1"
printf "%03d%03d%03d" "$major" "$minor" "$patch"
}
if [ "$(version_to_int "$latest_tag")" -gt "$(version_to_int "$current_tag")" ]; then
echo "🚀 检测到新版本,准备更新为 $latest_tag"
# 拉取代码并 checkout 到该 tag注意如果是部署环境建议 --force
git checkout -f "tags/$latest_tag"
# 更新 site.php 文件中的版本号
if [ -f "$SITE_FILE" ]; then
sed -i.bak "s/'version' => '.*'/'version' => '${latest_tag}'/" "$SITE_FILE"
rm "$SITE_FILE.bak"
echo "✅ 已更新 $SITE_FILE 为版本 $latest_tag"
else
echo "⚠️ 找不到 $SITE_FILE,未更新版本号"
fi
echo "✅ 部署完成,新版本:$latest_tag"
else
echo "🟢 当前已是最新版本,无需更新"
fi