|
| 1 | +name: Auto-increment version and release |
| 2 | + |
| 3 | +# 触发条件:推送到 main 分支(排除标签推送,避免循环触发) |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: [ "main" ] |
| 7 | + tags-ignore: [ 'v*' ] # 忽略标签推送,防止重复执行 |
| 8 | + |
| 9 | +jobs: |
| 10 | + get-next-version: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + outputs: |
| 13 | + next_version: ${{ steps.calc-version.outputs.next_version }} # 输出下一个版本号 |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 # 拉取所有历史,以便获取所有标签 |
| 18 | + |
| 19 | + - name: Calculate next version |
| 20 | + id: calc-version |
| 21 | + run: | |
| 22 | + # 获取所有符合 v*.*.* 格式的标签,按版本号排序 |
| 23 | + latest_tag=$(git tag --list "v*.*.*" --sort=-v:refname | head -n 1) |
| 24 | + |
| 25 | + # 如果没有历史标签,初始版本为 v1.0.0 |
| 26 | + if [ -z "$latest_tag" ]; then |
| 27 | + next_version="v0.0.1" |
| 28 | + else |
| 29 | + # 移除前缀 'v',拆分版本号为 major.minor.patch |
| 30 | + version=${latest_tag:1} # 如 v1.1.2 → 1.1.2 |
| 31 | + IFS='.' read -r major minor patch <<< "$version" |
| 32 | + |
| 33 | + # 递增 patch 版本(小版本+1) |
| 34 | + patch=$((patch + 1)) |
| 35 | + next_version="v$major.$minor.$patch" |
| 36 | + fi |
| 37 | + |
| 38 | + echo "next_version=$next_version" >> $GITHUB_OUTPUT |
| 39 | + echo "将创建新版本: $next_version" |
| 40 | + |
| 41 | + build-and-release: |
| 42 | + needs: get-next-version |
| 43 | + runs-on: ${{ matrix.os }} |
| 44 | + strategy: |
| 45 | + fail-fast: false |
| 46 | + matrix: |
| 47 | + os: [ubuntu-latest, windows-latest] |
| 48 | + python-version: ["3.11"] |
| 49 | + |
| 50 | + steps: |
| 51 | + - uses: actions/checkout@v4 |
| 52 | + |
| 53 | + - name: Set up Python |
| 54 | + uses: actions/setup-python@v5 |
| 55 | + with: |
| 56 | + python-version: ${{ matrix.python-version }} |
| 57 | + |
| 58 | + - name: Install dependencies |
| 59 | + run: | |
| 60 | + python -m pip install --upgrade pip |
| 61 | + pip install pyinstaller |
| 62 | + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi |
| 63 | + shell: ${{ matrix.os == 'windows-latest' ? 'pwsh' : 'bash' }} |
| 64 | + |
| 65 | + - name: Package with pyinstaller |
| 66 | + run: | |
| 67 | + # 按系统和版本号命名(如 myapp-v1.0.0-linux) |
| 68 | + os_name=${{ matrix.os == 'windows-latest' ? 'windows' : 'linux' }} |
| 69 | + pyinstaller --onefile --name "myapp-${{ needs.get-next-version.outputs.next_version }}-$os_name" main.py |
| 70 | + shell: ${{ matrix.os == 'windows-latest' ? 'pwsh' : 'bash' }} |
| 71 | + |
| 72 | + - name: Upload artifacts |
| 73 | + uses: actions/upload-artifact@v4 |
| 74 | + with: |
| 75 | + name: myapp-${{ needs.get-next-version.outputs.next_version }}-${{ matrix.os }} |
| 76 | + path: dist/ |
| 77 | + |
| 78 | + create-tag-and-release: |
| 79 | + needs: [get-next-version, build-and-release] |
| 80 | + runs-on: ubuntu-latest |
| 81 | + steps: |
| 82 | + - uses: actions/checkout@v4 |
| 83 | + with: |
| 84 | + fetch-depth: 0 |
| 85 | + |
| 86 | + - name: Download all artifacts |
| 87 | + uses: actions/download-artifact@v4 |
| 88 | + |
| 89 | + - name: Create Git tag |
| 90 | + run: | |
| 91 | + git config --global user.name "GitHub Actions" |
| 92 | + git config --global user.email "actions@github.com" |
| 93 | + git tag ${{ needs.get-next-version.outputs.next_version }} |
| 94 | + git push origin ${{ needs.get-next-version.outputs.next_version }} |
| 95 | + env: |
| 96 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 用于推送标签 |
| 97 | + |
| 98 | + - name: Create GitHub Release |
| 99 | + uses: softprops/action-gh-release@v2 |
| 100 | + with: |
| 101 | + tag_name: ${{ needs.get-next-version.outputs.next_version }} |
| 102 | + name: Release ${{ needs.get-next-version.outputs.next_version }} |
| 103 | + body: | |
| 104 | + 自动发布版本 ${{ needs.get-next-version.outputs.next_version }} |
| 105 | + 包含 Linux 和 Windows 可执行文件 |
| 106 | + files: | |
| 107 | + ExamCountdown-${{ needs.get-next-version.outputs.next_version }}-ubuntu-latest/* |
| 108 | + ExamCountdown-${{ needs.get-next-version.outputs.next_version }}-windows-latest/* |
| 109 | + env: |
| 110 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments