Skip to content

Commit 8ed1548

Browse files
committed
chore(ci): enhance version bump workflow for improved flexibility
This commit refactors the version bumping logic in the GitHub Actions workflow to allow for both custom version inputs and standard version type bumps. It ensures that the version is always updated in the `pyproject.toml`, even if the new version is the same as the current one. Additionally, it introduces checks for changes before committing and allows for force pushing tags to handle existing tags on the remote. These improvements enhance the reliability and usability of the release process.
1 parent 2428c83 commit 8ed1548

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

.github/workflows/version-bump.yml

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,43 +44,28 @@ jobs:
4444
4545
- name: Bump version
4646
id: bump
47+
env:
48+
VERSION_TYPE: ${{ github.event.inputs.version_type }}
49+
CUSTOM_VERSION: ${{ github.event.inputs.custom_version }}
4750
run: |
48-
if [ -n "${{ github.event.inputs.custom_version }}" ]; then
49-
# Use custom version
50-
NEW_VERSION="${{ github.event.inputs.custom_version }}"
51-
echo "Using custom version: $NEW_VERSION"
52-
else
53-
# Use version type bump
54-
case "${{ github.event.inputs.version_type }}" in
55-
patch)
56-
bump2version patch --dry-run --list | grep new_version | cut -d'=' -f2
57-
;;
58-
minor)
59-
bump2version minor --dry-run --list | grep new_version | cut -d'=' -f2
60-
;;
61-
major)
62-
bump2version major --dry-run --list | grep new_version | cut -d'=' -f2
63-
;;
64-
esac
65-
fi
66-
6751
# Extract current version from pyproject.toml
6852
CURRENT_VERSION=$(grep '^version =' pyproject.toml | sed 's/version = "\(.*\)"/\1/')
6953
70-
if [ -n "${{ github.event.inputs.custom_version }}" ]; then
71-
NEW_VERSION="${{ github.event.inputs.custom_version }}"
54+
# Check if custom version is provided
55+
if [ -n "$CUSTOM_VERSION" ]; then
56+
NEW_VERSION="$CUSTOM_VERSION"
57+
echo "Using custom version: $NEW_VERSION"
7258
else
73-
# Calculate new version based on type
74-
IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
75-
case "${{ github.event.inputs.version_type }}" in
59+
# Use version type bump
60+
case "$VERSION_TYPE" in
7661
patch)
77-
NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$((VERSION_PARTS[2] + 1))"
62+
NEW_VERSION=$(bump2version patch --dry-run --list | grep new_version | cut -d'=' -f2)
7863
;;
7964
minor)
80-
NEW_VERSION="${VERSION_PARTS[0]}.$((VERSION_PARTS[1] + 1)).0"
65+
NEW_VERSION=$(bump2version minor --dry-run --list | grep new_version | cut -d'=' -f2)
8166
;;
8267
major)
83-
NEW_VERSION="$((VERSION_PARTS[0] + 1)).0.0"
68+
NEW_VERSION=$(bump2version major --dry-run --list | grep new_version | cut -d'=' -f2)
8469
;;
8570
esac
8671
fi
@@ -89,19 +74,38 @@ jobs:
8974
echo "New version: $NEW_VERSION"
9075
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
9176
92-
# Update pyproject.toml
93-
sed -i "s/version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" pyproject.toml
77+
# Always update version - force the change even if it's the same
78+
if [ -n "$CUSTOM_VERSION" ]; then
79+
# For custom version, force update to user-specified version
80+
echo "Forcing version update to: $NEW_VERSION"
81+
sed -i "s/version = \".*\"/version = \"$NEW_VERSION\"/" pyproject.toml
82+
else
83+
# Use bump2version for standard version bumps
84+
bump2version $VERSION_TYPE --allow-dirty
85+
fi
86+
87+
# Always commit the changes, even if version didn't change
88+
# This ensures we have a commit record of the version bump
9489
9590
- name: Commit version bump
9691
run: |
9792
git add pyproject.toml
98-
git commit -m "Bump version to ${{ steps.bump.outputs.new_version }}"
93+
# Check if there are changes to commit
94+
if git diff --staged --quiet; then
95+
echo "No changes detected, creating empty commit for version bump"
96+
git commit --allow-empty -m "Bump version to ${{ steps.bump.outputs.new_version }}"
97+
else
98+
echo "Changes detected, committing version bump"
99+
git commit -m "Bump version to ${{ steps.bump.outputs.new_version }}"
100+
fi
99101
100102
- name: Create tag
101103
run: |
102-
git tag "v${{ steps.bump.outputs.new_version }}"
104+
# Force create tag, overwriting if it already exists
105+
git tag -f "v${{ steps.bump.outputs.new_version }}"
103106
104107
- name: Push changes and tag
105108
run: |
106109
git push origin main
107-
git push origin "v${{ steps.bump.outputs.new_version }}"
110+
# Force push tag, overwriting if it already exists on remote
111+
git push -f origin "v${{ steps.bump.outputs.new_version }}"

0 commit comments

Comments
 (0)