🔧 chore(workflow): enhance version update process with better error handling
- add commits counter to track successful commits - implement directory existence checks before operations - add git reset and proper file staging with git rm and git add - check for actual file changes before committing - improve error messages and exit conditions - add success status reporting only when commits are pushed
This commit is contained in:
parent
218ffaa84a
commit
265a899c2d
|
|
@ -104,10 +104,10 @@ def safe_rename_directory(old_path: str, new_path: str) -> bool:
|
||||||
|
|
||||||
def write_version_file(file_path: str, version: str) -> bool:
|
def write_version_file(file_path: str, version: str) -> bool:
|
||||||
"""
|
"""
|
||||||
写入版本文件
|
写入版本标记文件
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
file_path (str): 版本文件路径
|
file_path (str): 版本标记文件路径
|
||||||
version (str): 版本号
|
version (str): 版本号
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
|
|
@ -117,10 +117,10 @@ def write_version_file(file_path: str, version: str) -> bool:
|
||||||
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
||||||
with open(file_path, 'w') as f:
|
with open(file_path, 'w') as f:
|
||||||
f.write(version)
|
f.write(version)
|
||||||
print(f"✓ 版本文件已更新: {file_path}")
|
print(f"✓ 版本标记文件已更新: {file_path}")
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"✗ 写入版本文件失败: {e}")
|
print(f"✗ 写入版本标记文件失败: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
@ -154,10 +154,10 @@ def main():
|
||||||
new_path = f"apps/{app_name}/{new_ver_dir}"
|
new_path = f"apps/{app_name}/{new_ver_dir}"
|
||||||
|
|
||||||
if safe_rename_directory(old_path, new_path):
|
if safe_rename_directory(old_path, new_path):
|
||||||
# 更新版本文件
|
# 更新版本标记文件
|
||||||
version_file = f"apps/{app_name}/{old_version}.version"
|
version_file = f"apps/{app_name}/{old_version}.version"
|
||||||
if not write_version_file(version_file, new_version):
|
if not write_version_file(version_file, new_version):
|
||||||
print("版本文件更新失败,但目录重命名成功")
|
print("版本标记文件更新失败,但目录重命名成功")
|
||||||
else:
|
else:
|
||||||
print("错误: 目录重命名失败")
|
print("错误: 目录重命名失败")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ jobs:
|
||||||
- name: Commit & Push Changes
|
- name: Commit & Push Changes
|
||||||
run: |
|
run: |
|
||||||
IFS=' ' read -ra files <<< "${{ steps.updated-files.outputs.files }}"
|
IFS=' ' read -ra files <<< "${{ steps.updated-files.outputs.files }}"
|
||||||
|
commits_counter=0
|
||||||
for file in "${files[@]}"; do
|
for file in "${files[@]}"; do
|
||||||
if [[ $file == *"docker-compose.yml"* ]]; then
|
if [[ $file == *"docker-compose.yml"* ]]; then
|
||||||
app_name=$(echo $file | cut -d'/' -f 2)
|
app_name=$(echo $file | cut -d'/' -f 2)
|
||||||
|
|
@ -99,7 +99,36 @@ jobs:
|
||||||
if [ -f "apps/$app_name/${old_version}.version" ]; then
|
if [ -f "apps/$app_name/${old_version}.version" ]; then
|
||||||
new_version=$(cat "apps/$app_name/${old_version}.version")
|
new_version=$(cat "apps/$app_name/${old_version}.version")
|
||||||
rm -f "apps/$app_name/${old_version}.version"
|
rm -f "apps/$app_name/${old_version}.version"
|
||||||
git add "apps/$app_name/*" && git commit -m "🔧 chore($app_name): update app version from $old_version to $new_version" --no-verify && git push
|
echo "处理: $app_name - $old_version → $new_version"
|
||||||
|
# 检查目录是否存在
|
||||||
|
if [ ! -d "apps/$app_name/$old_version" ]; then
|
||||||
|
echo "✅ 旧目录不存在: apps/$app_name/$old_version"
|
||||||
|
fi
|
||||||
|
if [ -d "apps/$app_name/$new_version" ]; then
|
||||||
|
echo "✅ 新目录存在: apps/$app_name/$new_version"
|
||||||
|
else
|
||||||
|
echo "⚠️ 新目录不存在: apps/$app_name/$new_version"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
git reset
|
||||||
|
git rm -r "apps/$app_name/$old_version"
|
||||||
|
git add "apps/$app_name/$new_version"
|
||||||
|
if git diff --cached --quiet; then
|
||||||
|
echo "⚠️ 没有检测到文件变更,跳过提交"
|
||||||
|
else
|
||||||
|
git commit -m "🔧 chore($app_name): update app version from $old_version to $new_version" --no-verify
|
||||||
|
echo "✅ 已提交: $old_version → $new_version"
|
||||||
|
((commits_counter++))
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "⚠️ 没有找到版本标记文件: apps/$app_name/${old_version}.version"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ $commits_counter -gt 0 ]; then
|
||||||
|
echo "推送 $commits_counter 个提交到远程仓库..."
|
||||||
|
git push
|
||||||
gh api --silent --method POST -H "Accept: application/vnd.github+json" \
|
gh api --silent --method POST -H "Accept: application/vnd.github+json" \
|
||||||
-H "X-GitHub-Api-Version: 2022-11-28" \
|
-H "X-GitHub-Api-Version: 2022-11-28" \
|
||||||
/repos/${{ github.repository }}/statuses/$(git show -s --format=%H) \
|
/repos/${{ github.repository }}/statuses/$(git show -s --format=%H) \
|
||||||
|
|
@ -107,9 +136,9 @@ jobs:
|
||||||
-f 'target_url=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}' \
|
-f 'target_url=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}' \
|
||||||
-f 'description=CI/CD' \
|
-f 'description=CI/CD' \
|
||||||
-f 'context=${{ github.workflow}}'
|
-f 'context=${{ github.workflow}}'
|
||||||
|
else
|
||||||
|
echo "没有提交需要推送"
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
check-labels:
|
check-labels:
|
||||||
name: Check labels
|
name: Check labels
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue