Skip to content

ci(build): 优化 GitHub Actions 构建流程 #12

ci(build): 优化 GitHub Actions 构建流程

ci(build): 优化 GitHub Actions 构建流程 #12

Workflow file for this run

name: Build Application
on:
push:
branches: [ main ]
tags: [ 'v*' ]
pull_request:
branches: [ main ]
# 更新权限配置
permissions:
contents: write
discussions: write
pull-requests: write
issues: write
jobs:
build:
name: Build on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, macos-latest, ubuntu-latest]
python-version: ['3.8']
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
# 创建必要的文件和目录
- name: Create necessary files
shell: bash # 使用 bash 确保跨平台一致性
run: |
# 创建 .env 文件(如果不存在)
if [ ! -f ".env" ]; then
echo "Creating .env file..."
echo "SILICON_API_KEY=dummy_key" > .env
fi
# 创建目录(如果不存在)
for dir in "assets" "temp"; do
if [ ! -d "$dir" ]; then
echo "Creating $dir directory..."
mkdir -p "$dir"
fi
done
# 显示文件结构
echo "Current file structure:"
ls -la
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pyinstaller
# Linux 系统安装 ImageMagick
- name: Install ImageMagick (Linux)
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y imagemagick
sudo apt-get install -y librsvg2-bin
convert --version
sudo sed -i 's/<policy domain="coder" rights="none" pattern="SVG" \/>/<policy domain="coder" rights="read|write" pattern="SVG" \/>/' /etc/ImageMagick-6/policy.xml
# macOS 系统安装 ImageMagick
- name: Install ImageMagick (macOS)
if: runner.os == 'macOS'
run: |
brew install imagemagick librsvg
magick --version
# Windows 系统安装 ImageMagick
- name: Install ImageMagick (Windows)
if: runner.os == 'Windows'
run: |
choco install imagemagick.app -y
refreshenv
magick --version
- name: Generate Icons
shell: bash
run: |
chmod +x assets/create_icons.sh
if [ "$RUNNER_OS" == "Linux" ]; then
sed -i 's/magick convert/convert/g' assets/create_icons.sh
convert --version
convert -list format
else
magick --version
magick -list format
fi
./assets/create_icons.sh
- name: Build (Windows)
if: runner.os == 'Windows'
run: |
# 确保文件存在
echo "Checking files..."
dir .env
dir assets
# 构建
pyinstaller --noconsole --add-data ".env;." --add-data "assets/*;assets" --icon "assets/icon.ico" --name "Text2Voice" main.py
- name: Build (macOS)
if: runner.os == 'macOS'
run: |
# 确保文件存在
echo "Checking files..."
ls -la .env
ls -la assets
# 构建
pyinstaller --noconsole --add-data ".env:." --add-data "assets/*:assets" --icon "assets/icon.icns" --name "Text2Voice" main.py
- name: Build (Linux)
if: runner.os == 'Linux'
run: |
# 确保文件存在
echo "Checking files..."
ls -la .env
ls -la assets
# 构建
pyinstaller --noconsole --add-data ".env:." --add-data "assets/*:assets" --icon "assets/icon.png" --name "Text2Voice" main.py
- name: Create DMG (macOS)
if: runner.os == 'macOS'
run: |
brew install create-dmg
create-dmg \
--volname "Text2Voice" \
--window-pos 200 120 \
--window-size 800 400 \
--icon-size 100 \
--icon "Text2Voice.app" 200 190 \
--hide-extension "Text2Voice.app" \
--app-drop-link 600 185 \
"Text2Voice.dmg" \
"dist/"
- name: Create Release
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v2
with:
name: Release ${{ github.ref_name }}
body: |
## Text2Voice Release ${{ github.ref_name }}
### 下载
- [Windows 版本 (Text2Voice-windows-latest.zip)](https://github.com/xdlee110/text2voice/releases/download/${{ github.ref_name }}/Text2Voice-windows-latest.zip)
- [macOS 版本 (Text2Voice-macos-latest.zip)](https://github.com/xdlee110/text2voice/releases/download/${{ github.ref_name }}/Text2Voice-macos-latest.zip)
- [Linux 版本 (Text2Voice-ubuntu-latest.zip)](https://github.com/xdlee110/text2voice/releases/download/${{ github.ref_name }}/Text2Voice-ubuntu-latest.zip)
### 更新内容
- 请在此处添加版本更新说明
### 构建信息
- 构建时间: ${{ github.event.head_commit.timestamp }}
- 提交信息: ${{ github.event.head_commit.message }}
- 构建分支: ${{ github.ref_name }}
draft: false
prerelease: false
files: |
Text2Voice-*.zip
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 确保压缩步骤在 release 之前执行
- name: Zip Artifacts
shell: bash
run: |
echo "Creating zip archive..."
cd dist
if [ "${{ runner.os }}" == "Windows" ]; then
7z a ../Text2Voice-${{ matrix.os }}.zip ./*
else
zip -r ../Text2Voice-${{ matrix.os }}.zip ./*
fi
cd ..
ls -la Text2Voice-*.zip
echo "Zip files created:"
for f in Text2Voice-*.zip; do
echo "- $f ($(stat -f%z "$f" 2>/dev/null || stat -c%s "$f") bytes)"
done
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: Text2Voice-${{ matrix.os }}
path: Text2Voice-${{ matrix.os }}.zip
compression-level: 9
retention-days: 365