-
Notifications
You must be signed in to change notification settings - Fork 3
109 lines (93 loc) · 3.78 KB
/
Copy pathpython-app.yml
File metadata and controls
109 lines (93 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
name: Build and Package ImageSplitter
on:
push:
tags:
- 'v*' # 只要推送以 v 开头的 tag(如 v2.5),就会自动触发此工作流
jobs:
build:
runs-on: windows-latest
permissions:
contents: write # 确保工作流有权限创建 Release 并上传文件
steps:
# 1. 检出代码
- name: Checkout repository
uses: actions/checkout@v4
# 2. 设置 Python 环境
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
# 3. 安装依赖
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# 4. 下载并解压 tkdnd(用于拖放功能)
- name: Download and extract tkdnd
run: |
$tkdndUrl = "https://github.com/petasis/tkdnd/releases/download/tkdnd-release-test-v2.9.4-rc3/tkdnd-2.9.4-windows-x64.zip"
Invoke-WebRequest -Uri $tkdndUrl -OutFile tkdnd.zip
Expand-Archive -Path tkdnd.zip -DestinationPath tkdnd -Force
# 5. 提取 Tag 版本号并将其同步写入到 python 程序的 __version__ 字段中
- name: Extract and Sync Version to Code
shell: python
env:
TAG_NAME: ${{ github.ref_name }} # 安全地将 Tag 名(例如 v2.5)传入环境变量
run: |
import re
import os
tag = os.environ.get('TAG_NAME', '')
# 去掉前导的 'v' 得到纯版本号 (例如 2.5)
version = tag[1:] if tag.lower().startswith('v') else tag
print(f"Target version to sync: {version}")
# 写入 Github 环境变量供后续步骤使用
with open(os.environ['GITHUB_ENV'], 'a') as f:
f.write(f"VERSION={version}\n")
# 寻找 main.py 中的 __version__ 并替换值
main_path = "main.py"
if os.path.exists(main_path):
with open(main_path, 'r', encoding='utf-8') as f:
content = f.read()
# 正则匹配并替换 __version__ = "2.1" 为 __version__ = "2.5"
new_content = re.sub(r'(__version__\s*=\s*["\'])[^"\']+(["\'])', f'\\g<1>{version}\\g<2>', content, count=1)
with open(main_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Successfully synchronized version in {main_path}.")
else:
print(f"Warning: {main_path} not found.")
# 6. 使用 PyInstaller 打包应用
- name: Build with PyInstaller
run: |
pyinstaller `
--name ImageSplitter `
--onefile `
--windowed `
--icon=icon.ico `
--add-data "icon.ico;." `
--add-data "tkdnd/*;tkdnd" `
--hidden-import=ttkbootstrap `
--hidden-import=pillow_heif `
--clean `
main.py
# 7. 将打包后的 EXE 压缩为 ZIP
- name: Compress EXE to ZIP
shell: pwsh
run: |
Compress-Archive -Path "dist/ImageSplitter.exe" -DestinationPath "dist/ImageSplitter_${{ env.VERSION }}.zip"
# 8. 上传 ZIP 构建产物作为 Action 备份(可选)
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ImageSplitter_build
path: dist/ImageSplitter_${{ env.VERSION }}.zip
# 9. 创建 Release 并上传 ZIP 产物
- name: Create Release and Upload Asset
uses: softprops/action-gh-release@v2
with:
files: dist/ImageSplitter_${{ env.VERSION }}.zip
tag_name: ${{ github.ref_name }}
name: Release ${{ github.ref_name }}
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}