From 60513cb920235b71f5f22e63b950f402fabc84d9 Mon Sep 17 00:00:00 2001 From: pooneyy <85266337+pooneyy@users.noreply.github.com> Date: Mon, 6 Oct 2025 21:40:27 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(workflow):=20enha?= =?UTF-8?q?nce=20version=20extraction=20and=20workflow=20reliability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add type hints to functions for better code clarity - fix version extraction logic to correctly access version from dictionary - improve error handling in workflow with better debugging information - update workflow to use pull request head ref for accurate file detection - add fallback to 'latest' version when extraction fails - enhance logging with old and new version display - rename script reference from .sh to .py in workflow step - modify checkout action to use head ref instead of default --- .github/workflows/renovate-app-version.py | 14 +++++++------- .github/workflows/renovate-app-version.yml | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.github/workflows/renovate-app-version.py b/.github/workflows/renovate-app-version.py index 2ed4480f2..0fd651dff 100644 --- a/.github/workflows/renovate-app-version.py +++ b/.github/workflows/renovate-app-version.py @@ -5,7 +5,7 @@ import shutil import sys import yaml -def extract_version_from_string(input_string): +def extract_version_from_string(input_string) -> dict: """ 从字符串中提取版本号,支持镜像名和文件夹名 @@ -66,7 +66,7 @@ def extract_version_from_string(input_string): return {"success": False, "version": original_candidate} -def replace_version_in_dirname(old_ver_dir, new_version): +def replace_version_in_dirname(old_ver_dir : str, new_version : str) -> str: """ 将旧版本文件夹名中的版本号替换为新版本号 @@ -80,7 +80,7 @@ def replace_version_in_dirname(old_ver_dir, new_version): version_info = extract_version_from_string(old_ver_dir) if version_info["success"]: - old_version = version_info["original"] + old_version = version_info["version"] new_ver_dir = old_ver_dir.replace(old_version, new_version) return new_ver_dir else: @@ -107,10 +107,10 @@ def main(): image = services[first_service].get('image', '') print(f"该服务的镜像: {image}") - new_version = extract_version_from_string(image) - print(f"版本号: {new_version}") - - old_version = extract_version_from_string(old_ver_dir) + new_version = extract_version_from_string(image).get("version", "latest") + old_version = extract_version_from_string(old_ver_dir).get("version", "latest") + print(f"旧版本号: {old_version}") + print(f"新版本号: {new_version}") new_ver_dir = replace_version_in_dirname(old_ver_dir, new_version) if old_version != new_version: old_path = f"apps/{app_name}/{old_ver_dir}" diff --git a/.github/workflows/renovate-app-version.yml b/.github/workflows/renovate-app-version.yml index cf075a6f7..a50807104 100644 --- a/.github/workflows/renovate-app-version.yml +++ b/.github/workflows/renovate-app-version.yml @@ -46,6 +46,7 @@ jobs: uses: actions/checkout@v5.0.0 with: fetch-depth: 0 + ref: ${{ github.head_ref }} - uses: actions/setup-python@main with: @@ -60,9 +61,21 @@ jobs: - name: Get list of updated files by the last commit in this branch separated by space id: updated-files run: | - echo "files=$(git diff-tree --no-commit-id --name-only -r ${{ github.sha }} | tr '\n' ' ')" >> $GITHUB_OUTPUT + # 当触发工作流的事件为 pull_request 时,github.sha 值为 github.ref 分支上的最后一次合并提交 + files=$(git diff-tree --no-commit-id --name-only -r ${{ github.event.pull_request.head.sha }} | tr '\n' ' ') + if [ -z "$files" ]; then + echo "❌ 错误:没有检测到文件变更" + echo "github.sha = ${{ github.sha }}" + echo "github.event.pull_request.head.sha = ${{ github.event.pull_request.head.sha }}" + echo "当前分支最新的提交SHA: $(git rev-parse HEAD)" + echo "💡 最近三次提交:" + echo " $(git log -3)" + exit 1 + else + echo "files=$files" >> $GITHUB_OUTPUT + fi - - name: Run renovate-app-version.sh on updated files + - name: Run renovate-app-version.py on updated files run: | IFS=' ' read -ra files <<< "${{ steps.updated-files.outputs.files }}"