|
| 1 | +name: Build and Release LAN File Server |
| 2 | + |
| 3 | +# 触发条件: |
| 4 | +# 1. 推送带有v*标签的提交 |
| 5 | +# 2. 手动触发 |
| 6 | +on: |
| 7 | + push: |
| 8 | + tags: |
| 9 | + - 'v*' |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + version: |
| 13 | + description: 'Release version (e.g., v1.0.0)' |
| 14 | + required: false |
| 15 | + branch: |
| 16 | + description: 'Branch to build from' |
| 17 | + required: false |
| 18 | + default: 'main' |
| 19 | + |
| 20 | +jobs: |
| 21 | + build: |
| 22 | + runs-on: windows-latest |
| 23 | + timeout-minutes: 30 |
| 24 | + |
| 25 | + steps: |
| 26 | + # 1. 检出代码 |
| 27 | + - name: Checkout code |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + ref: ${{ github.event.inputs.branch || github.ref }} |
| 31 | + |
| 32 | + # 2. 设置Python环境 |
| 33 | + - name: Set up Python |
| 34 | + uses: actions/setup-python@v4 |
| 35 | + with: |
| 36 | + python-version: '3.10' |
| 37 | + architecture: 'x64' |
| 38 | + |
| 39 | + # 3. 安装依赖 |
| 40 | + - name: Install dependencies |
| 41 | + run: | |
| 42 | + python -m pip install --upgrade pip |
| 43 | + pip install -r requirements.txt |
| 44 | + |
| 45 | + # 4. 安装PyInstaller和其他打包依赖 |
| 46 | + - name: Install PyInstaller and packaging tools |
| 47 | + run: | |
| 48 | + pip install pyinstaller>=5.0.0 watchdog>=2.1.0 |
| 49 | + |
| 50 | + # 5. 创建PyInstaller配置文件 |
| 51 | + - name: Create PyInstaller spec file |
| 52 | + run: | |
| 53 | + echo "Creating PyInstaller spec file..." |
| 54 | + pyinstaller --name "LANFileServer" \ |
| 55 | + --onefile \ |
| 56 | + --windowed \ |
| 57 | + --add-data "static;static" \ |
| 58 | + --icon=none \ |
| 59 | + --hidden-import=config \ |
| 60 | + --hidden-import=color_logger \ |
| 61 | + --exclude-module=tkinter \ |
| 62 | + server.py |
| 63 | + |
| 64 | + # 6. 构建exe文件 |
| 65 | + - name: Build executable |
| 66 | + run: | |
| 67 | + echo "Building executable..." |
| 68 | + pyinstaller LANFileServer.spec |
| 69 | + continue-on-error: false |
| 70 | + |
| 71 | + # 7. 验证构建输出 |
| 72 | + - name: Verify build output |
| 73 | + run: | |
| 74 | + echo "Verifying build output..." |
| 75 | + if (-not (Test-Path "dist\LANFileServer.exe")) { |
| 76 | + Write-Error "Build failed: LANFileServer.exe not found" |
| 77 | + exit 1 |
| 78 | + } |
| 79 | + Get-Item "dist\LANFileServer.exe" | Select-Object Name, Length, CreationTime |
| 80 | + $fileHash = Get-FileHash "dist\LANFileServer.exe" -Algorithm SHA256 |
| 81 | + Write-Host "SHA256 Hash: $($fileHash.Hash)" |
| 82 | + |
| 83 | + # 8. 生成版本信息 |
| 84 | + - name: Generate version info |
| 85 | + id: version_info |
| 86 | + run: | |
| 87 | + $tag = $env:GITHUB_REF_NAME |
| 88 | + if ([string]::IsNullOrEmpty($tag)) { |
| 89 | + $tag = ${{ github.event.inputs.version || 'v0.0.0' }} |
| 90 | + } |
| 91 | + echo "version=$tag" >> $env:GITHUB_OUTPUT |
| 92 | + echo "exe_file=dist\LANFileServer.exe" >> $env:GITHUB_OUTPUT |
| 93 | + |
| 94 | + # 9. 创建GitHub Release并上传资产 |
| 95 | + - name: Create Release and Upload Asset |
| 96 | + uses: ncipollo/release-action@v1 |
| 97 | + with: |
| 98 | + tag: ${{ steps.version_info.outputs.version }} |
| 99 | + name: Release ${{ steps.version_info.outputs.version }} |
| 100 | + body: | |
| 101 | + ## What's Changed |
| 102 | + |
| 103 | + - Python version: 3.10 |
| 104 | + - Built on ${{ runner.os }} |
| 105 | + - Python version: ${{ steps.setup-python.outputs.python-version }} |
| 106 | + artifacts: "${{ steps.version_info.outputs.exe_file }}" |
| 107 | + artifactContentType: "application/octet-stream" |
| 108 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 109 | + draft: false |
| 110 | + prerelease: ${{ contains(steps.version_info.outputs.version, 'beta') || contains(steps.version_info.outputs.version, 'alpha') }} |
| 111 | + |
| 112 | + # 10. 清理构建文件 |
| 113 | + - name: Cleanup build files |
| 114 | + if: always() |
| 115 | + run: | |
| 116 | + Remove-Item -Path "build" -Recurse -Force -ErrorAction SilentlyContinue |
| 117 | + Remove-Item -Path "__pycache__" -Recurse -Force -ErrorAction SilentlyContinue |
| 118 | + Remove-Item -Path "*.spec" -Force -ErrorAction SilentlyContinue |
| 119 | + |
| 120 | + # 11. 上传构建日志作为artifact |
| 121 | + - name: Upload build logs |
| 122 | + if: always() |
| 123 | + uses: actions/upload-artifact@v4 |
| 124 | + with: |
| 125 | + name: build-logs |
| 126 | + path: | |
| 127 | + dist/ |
| 128 | + LANFileServer.spec |
| 129 | + retention-days: 7 |
| 130 | + |
| 131 | + # 12. 部署文档(可选) |
| 132 | + deploy-docs: |
| 133 | + runs-on: ubuntu-latest |
| 134 | + needs: build |
| 135 | + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') |
| 136 | + steps: |
| 137 | + - name: Checkout code |
| 138 | + uses: actions/checkout@v4 |
| 139 | + |
| 140 | + - name: Deploy documentation |
| 141 | + run: | |
| 142 | + echo "Documentation deployment would happen here" |
| 143 | + # 这里可以添加文档部署步骤,如部署到GitHub Pages |
| 144 | +
|
| 145 | +# 13. 工作流状态通知(可选) |
| 146 | +# 可以添加通知步骤,如发送Slack消息或邮件通知 |
| 147 | +# - name: Send notification |
| 148 | +# if: always() |
| 149 | +# uses: 8398a7/action-slack@v3 |
| 150 | +# with: |
| 151 | +# status: ${{ job.status }} |
| 152 | +# fields: repo,message,commit,author,action,eventName,ref,workflow,job |
| 153 | +# env: |
| 154 | +# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 155 | + |
| 156 | +# 14. 构建矩阵测试(可选) |
| 157 | +# 可以添加多版本Python和多平台测试 |
| 158 | +# strategy: |
| 159 | +# matrix: |
| 160 | +# python-version: [3.8, 3.9, 3.10, 3.11] |
| 161 | +# architecture: [x64, x86] |
| 162 | + |
| 163 | +# 15. 缓存依赖(可选) |
| 164 | +# - name: Cache dependencies |
| 165 | +# uses: actions/cache@v4 |
| 166 | +# with: |
| 167 | +# path: ~\AppData\Local\pip\Cache |
| 168 | +# key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} |
| 169 | +# restore-keys: | |
| 170 | +# ${{ runner.os }}-pip- |
| 171 | +name: Build and Release LAN File Server |
| 172 | + |
| 173 | +# 触发条件: |
| 174 | +# 1. 推送带有v*标签的提交 |
| 175 | +# 2. 手动触发 |
| 176 | +on: |
| 177 | + push: |
| 178 | + tags: |
| 179 | + - 'v*' |
| 180 | + workflow_dispatch: |
| 181 | + inputs: |
| 182 | + version: |
| 183 | + description: 'Release version (e.g., v1.0.0)' |
| 184 | + required: false |
| 185 | + branch: |
| 186 | + description: 'Branch to build from' |
| 187 | + required: false |
| 188 | + default: 'main' |
| 189 | + |
| 190 | +jobs: |
| 191 | + build: |
| 192 | + runs-on: windows-latest |
| 193 | + timeout-minutes: 30 |
| 194 | + |
| 195 | + steps: |
| 196 | + # 1. 检出代码 |
| 197 | + - name: Checkout code |
| 198 | + uses: actions/checkout@v4 |
| 199 | + with: |
| 200 | + ref: ${{ github.event.inputs.branch || github.ref }} |
| 201 | + |
| 202 | + # 2. 设置Python环境 |
| 203 | + - name: Set up Python |
| 204 | + uses: actions/setup-python@v4 |
| 205 | + with: |
| 206 | + python-version: '3.10' |
| 207 | + architecture: 'x64' |
| 208 | + |
| 209 | + # 3. 安装依赖 |
| 210 | + - name: Install dependencies |
| 211 | + run: | |
| 212 | + python -m pip install --upgrade pip |
| 213 | + pip install -r requirements.txt |
| 214 | + |
| 215 | + # 4. 安装PyInstaller和其他打包依赖 |
| 216 | + - name: Install PyInstaller and packaging tools |
| 217 | + run: | |
| 218 | + pip install pyinstaller>=5.0.0 watchdog>=2.1.0 |
| 219 | + |
| 220 | + # 5. 创建PyInstaller配置文件 |
| 221 | + - name: Create PyInstaller spec file |
| 222 | + run: | |
| 223 | + echo "Creating PyInstaller spec file..." |
| 224 | + pyinstaller --name "LANFileServer" \ |
| 225 | + --onefile \ |
| 226 | + --windowed \ |
| 227 | + --add-data "static;static" \ |
| 228 | + --icon=none \ |
| 229 | + --hidden-import=config \ |
| 230 | + --hidden-import=color_logger \ |
| 231 | + --exclude-module=tkinter \ |
| 232 | + server.py |
| 233 | + |
| 234 | + # 6. 构建exe文件 |
| 235 | + - name: Build executable |
| 236 | + run: | |
| 237 | + echo "Building executable..." |
| 238 | + pyinstaller LANFileServer.spec |
| 239 | + continue-on-error: false |
| 240 | + |
| 241 | + # 7. 验证构建输出 |
| 242 | + - name: Verify build output |
| 243 | + run: | |
| 244 | + echo "Verifying build output..." |
| 245 | + if (-not (Test-Path "dist\LANFileServer.exe")) { |
| 246 | + Write-Error "Build failed: LANFileServer.exe not found" |
| 247 | + exit 1 |
| 248 | + } |
| 249 | + Get-Item "dist\LANFileServer.exe" | Select-Object Name, Length, CreationTime |
| 250 | + $fileHash = Get-FileHash "dist\LANFileServer.exe" -Algorithm SHA256 |
| 251 | + Write-Host "SHA256 Hash: $($fileHash.Hash)" |
| 252 | + |
| 253 | + # 8. 生成版本信息 |
| 254 | + - name: Generate version info |
| 255 | + id: version_info |
| 256 | + run: | |
| 257 | + $tag = $env:GITHUB_REF_NAME |
| 258 | + if ([string]::IsNullOrEmpty($tag)) { |
| 259 | + $tag = "${{ github.event.inputs.version || 'v0.0.0' }}" |
| 260 | + } |
| 261 | + echo "version=$tag" >> $env:GITHUB_OUTPUT |
| 262 | + echo "exe_file=dist\LANFileServer.exe" >> $env:GITHUB_OUTPUT |
| 263 | + |
| 264 | + # 9. 创建GitHub Release并上传资产 |
| 265 | + - name: Create Release and Upload Asset |
| 266 | + uses: ncipollo/release-action@v1 |
| 267 | + with: |
| 268 | + tag: ${{ steps.version_info.outputs.version }} |
| 269 | + name: Release ${{ steps.version_info.outputs.version }} |
| 270 | + body: | |
| 271 | + ## What's Changed |
| 272 | + |
| 273 | + - Auto-generated release from GitHub Actions |
| 274 | + - Built on ${{ runner.os }} |
| 275 | + - Python version: ${{ steps.setup-python.outputs.python-version }} |
| 276 | + artifacts: "${{ steps.version_info.outputs.exe_file }}" |
| 277 | + artifactContentType: "application/octet-stream" |
| 278 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 279 | + draft: false |
| 280 | + prerelease: ${{ contains(steps.version_info.outputs.version, 'beta') || contains(steps.version_info.outputs.version, 'alpha') }} |
| 281 | + |
| 282 | + # 10. 清理构建文件 |
| 283 | + - name: Cleanup build files |
| 284 | + if: always() |
| 285 | + run: | |
| 286 | + Remove-Item -Path "build" -Recurse -Force -ErrorAction SilentlyContinue |
| 287 | + Remove-Item -Path "__pycache__" -Recurse -Force -ErrorAction SilentlyContinue |
| 288 | + Remove-Item -Path "*.spec" -Force -ErrorAction SilentlyContinue |
| 289 | + |
| 290 | + # 11. 上传构建日志作为artifact |
| 291 | + - name: Upload build logs |
| 292 | + if: always() |
| 293 | + uses: actions/upload-artifact@v4 |
| 294 | + with: |
| 295 | + name: build-logs |
| 296 | + path: | |
| 297 | + dist/ |
| 298 | + LANFileServer.spec |
| 299 | + retention-days: 7 |
| 300 | + |
| 301 | + # 12. 部署文档(可选) |
| 302 | + deploy-docs: |
| 303 | + runs-on: ubuntu-latest |
| 304 | + needs: build |
| 305 | + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') |
| 306 | + steps: |
| 307 | + - name: Checkout code |
| 308 | + uses: actions/checkout@v4 |
| 309 | + |
| 310 | + - name: Deploy documentation |
| 311 | + run: | |
| 312 | + echo "Documentation deployment would happen here" |
| 313 | + # 这里可以添加文档部署步骤,如部署到GitHub Pages |
| 314 | +
|
| 315 | +# 13. 工作流状态通知(可选) |
| 316 | +# 可以添加通知步骤,如发送Slack消息或邮件通知 |
| 317 | +# - name: Send notification |
| 318 | +# if: always() |
| 319 | +# uses: 8398a7/action-slack@v3 |
| 320 | +# with: |
| 321 | +# status: ${{ job.status }} |
| 322 | +# fields: repo,message,commit,author,action,eventName,ref,workflow,job |
| 323 | +# env: |
| 324 | +# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 325 | + |
| 326 | +# 14. 构建矩阵测试(可选) |
| 327 | +# 可以添加多版本Python和多平台测试 |
| 328 | +# strategy: |
| 329 | +# matrix: |
| 330 | +# python-version: [3.8, 3.9, 3.10, 3.11] |
| 331 | +# architecture: [x64, x86] |
| 332 | + |
| 333 | +# 15. 缓存依赖(可选) |
| 334 | +# - name: Cache dependencies |
| 335 | +# uses: actions/cache@v4 |
| 336 | +# with: |
| 337 | +# path: ~\AppData\Local\pip\Cache |
| 338 | +# key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} |
| 339 | +# restore-keys: | |
| 340 | +# ${{ runner.os }}-pip- |
0 commit comments