|
| 1 | +name: Auto Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - '**.py' |
| 9 | + - 'pyproject.toml' |
| 10 | + |
| 11 | +jobs: |
| 12 | + release: |
| 13 | + name: Bump version and release |
| 14 | + runs-on: ubuntu-latest |
| 15 | + # Only run on merged PRs (not direct pushes or version bump commits) |
| 16 | + if: | |
| 17 | + github.event.head_commit.message != '' && |
| 18 | + !startsWith(github.event.head_commit.message, 'chore: bump version') |
| 19 | + permissions: |
| 20 | + contents: write |
| 21 | + |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 27 | + |
| 28 | + - name: Set up Python |
| 29 | + uses: actions/setup-python@v5 |
| 30 | + with: |
| 31 | + python-version: "3.12" |
| 32 | + |
| 33 | + - name: Install toml |
| 34 | + run: pip install toml |
| 35 | + |
| 36 | + - name: Determine version bump type |
| 37 | + id: bump-type |
| 38 | + run: | |
| 39 | + COMMIT_MSG="${{ github.event.head_commit.message }}" |
| 40 | + # Extract the type from conventional commit (feat, fix, etc.) |
| 41 | + if [[ "$COMMIT_MSG" =~ ^feat ]]; then |
| 42 | + echo "type=minor" >> $GITHUB_OUTPUT |
| 43 | + elif [[ "$COMMIT_MSG" =~ ^(fix|perf) ]]; then |
| 44 | + echo "type=patch" >> $GITHUB_OUTPUT |
| 45 | + elif [[ "$COMMIT_MSG" =~ ^(docs|style|refactor|test|chore|ci|build) ]]; then |
| 46 | + echo "type=patch" >> $GITHUB_OUTPUT |
| 47 | + else |
| 48 | + # Default to patch for non-conventional commits |
| 49 | + echo "type=patch" >> $GITHUB_OUTPUT |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Bump version |
| 53 | + id: bump |
| 54 | + run: | |
| 55 | + python << 'EOF' |
| 56 | + import toml |
| 57 | + import os |
| 58 | +
|
| 59 | + # Read current version |
| 60 | + with open('pyproject.toml', 'r') as f: |
| 61 | + data = toml.load(f) |
| 62 | +
|
| 63 | + current = data['project']['version'] |
| 64 | + major, minor, patch = map(int, current.split('.')) |
| 65 | +
|
| 66 | + bump_type = os.environ.get('BUMP_TYPE', 'patch') |
| 67 | +
|
| 68 | + if bump_type == 'major': |
| 69 | + major += 1 |
| 70 | + minor = 0 |
| 71 | + patch = 0 |
| 72 | + elif bump_type == 'minor': |
| 73 | + minor += 1 |
| 74 | + patch = 0 |
| 75 | + else: # patch |
| 76 | + patch += 1 |
| 77 | +
|
| 78 | + new_version = f"{major}.{minor}.{patch}" |
| 79 | + data['project']['version'] = new_version |
| 80 | +
|
| 81 | + with open('pyproject.toml', 'w') as f: |
| 82 | + toml.dump(data, f) |
| 83 | +
|
| 84 | + print(f"Bumped {current} -> {new_version}") |
| 85 | +
|
| 86 | + # Set output |
| 87 | + with open(os.environ['GITHUB_OUTPUT'], 'a') as f: |
| 88 | + f.write(f"version={new_version}\n") |
| 89 | + f.write(f"tag=v{new_version}\n") |
| 90 | + EOF |
| 91 | + env: |
| 92 | + BUMP_TYPE: ${{ steps.bump-type.outputs.type }} |
| 93 | + |
| 94 | + - name: Commit and tag |
| 95 | + run: | |
| 96 | + git config user.name "github-actions[bot]" |
| 97 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 98 | + git add pyproject.toml |
| 99 | + git commit -m "chore: bump version to ${{ steps.bump.outputs.version }}" |
| 100 | + git tag ${{ steps.bump.outputs.tag }} |
| 101 | + git push origin main --tags |
0 commit comments