♻️ refactor(workflow): enhance version extraction and workflow reliability
- 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
This commit is contained in:
parent
deae7397a8
commit
60513cb920
|
|
@ -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}"
|
||||
|
|
|
|||
|
|
@ -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 }}"
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue