diff --git a/deploy-if-new-tag.sh b/deploy-if-new-tag.sh new file mode 100755 index 0000000..91e75ea --- /dev/null +++ b/deploy-if-new-tag.sh @@ -0,0 +1,44 @@ +#!/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