|
| 1 | +name: Version Bump |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths-ignore: |
| 8 | + - 'README.md' |
| 9 | + - 'CHANGELOG.md' |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +jobs: |
| 13 | + version-bump: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + permissions: |
| 16 | + contents: write |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Get current version |
| 26 | + id: get_version |
| 27 | + shell: bash |
| 28 | + run: | |
| 29 | + # 从代码中获取当前版本号 |
| 30 | + current_version=$(grep -oP '__version__ = "[^"]+' github_cve_monitor.py | cut -d'"' -f2) |
| 31 | + echo "Current version: $current_version" |
| 32 | + echo "current_version=$current_version" >> $GITHUB_OUTPUT |
| 33 | +
|
| 34 | + - name: Bump version |
| 35 | + id: bump_version |
| 36 | + run: | |
| 37 | + # 解析当前版本号(格式:V1.1.0) |
| 38 | + current_version=${{ steps.get_version.outputs.current_version }} |
| 39 | + |
| 40 | + # 提取版本号的数字部分和后缀 |
| 41 | + if [[ $current_version =~ ^V([0-9]+)\.([0-9]+)\.([0-9]+)([a-z]?)$ ]]; then |
| 42 | + major=${BASH_REMATCH[1]} |
| 43 | + minor=${BASH_REMATCH[2]} |
| 44 | + patch=${BASH_REMATCH[3]} |
| 45 | + suffix=${BASH_REMATCH[4]} |
| 46 | + |
| 47 | + # 自动递增patch版本 |
| 48 | + new_patch=$((patch + 1)) |
| 49 | + new_version="V$major.$minor.$new_patch$suffix" |
| 50 | + else |
| 51 | + # 如果版本号格式不符合预期,使用默认值 |
| 52 | + new_version="V1.1.1" |
| 53 | + fi |
| 54 | + |
| 55 | + echo "New version: $new_version" |
| 56 | + echo "new_version=$new_version" >> $GITHUB_OUTPUT |
| 57 | +
|
| 58 | + - name: Update version in files |
| 59 | + shell: bash |
| 60 | + run: | |
| 61 | + new_version=${{ steps.bump_version.outputs.new_version }} |
| 62 | + current_date=$(date +'%Y-%m-%d') |
| 63 | + |
| 64 | + # 更新主程序中的版本号 |
| 65 | + sed -i "s/__version__ = .*/__version__ = \"$new_version\"/" github_cve_monitor.py |
| 66 | + |
| 67 | + # 更新README.md中的版本信息 |
| 68 | + # 假设 README 中有 "当前版本:**V1.1.0**" 和 "版本更新时间:2026-01-05" |
| 69 | + sed -i "s/当前版本:\*\*.*\*\*/当前版本:\*\*$new_version\*\*/" README.md || true |
| 70 | + sed -i "s/版本更新时间:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/版本更新时间:$current_date/" README.md || true |
| 71 | +
|
| 72 | + - name: Check if tag exists |
| 73 | + id: check_tag |
| 74 | + shell: bash |
| 75 | + run: | |
| 76 | + if git rev-parse --verify "${{ steps.bump_version.outputs.new_version }}" 2>/dev/null; then |
| 77 | + echo "tag_exists=true" >> $GITHUB_OUTPUT |
| 78 | + echo "Tag ${{ steps.bump_version.outputs.new_version }} already exists" |
| 79 | + else |
| 80 | + echo "tag_exists=false" >> $GITHUB_OUTPUT |
| 81 | + echo "Tag ${{ steps.bump_version.outputs.new_version }} does not exist" |
| 82 | + fi |
| 83 | +
|
| 84 | + - name: Commit and push changes without tagging |
| 85 | + if: steps.check_tag.outputs.tag_exists == 'true' |
| 86 | + uses: stefanzweifel/git-auto-commit-action@v5 |
| 87 | + with: |
| 88 | + commit_message: "Bump version to ${{ steps.bump_version.outputs.new_version }}" |
| 89 | + push_options: --force-with-lease |
| 90 | + |
| 91 | + - name: Commit and push changes with tagging |
| 92 | + if: steps.check_tag.outputs.tag_exists == 'false' |
| 93 | + uses: stefanzweifel/git-auto-commit-action@v5 |
| 94 | + with: |
| 95 | + commit_message: "Bump version to ${{ steps.bump_version.outputs.new_version }}" |
| 96 | + tagging_message: "${{ steps.bump_version.outputs.new_version }}" |
| 97 | + push_options: --force-with-lease |
0 commit comments