Merge branch 'main' of https://github.com/ahmed0x77/cpulimiter #17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto Version Bump and Publish | |
on: | |
push: | |
branches: [main] | |
paths: | |
- "cpulimiter/**" | |
- "setup.py" | |
- "pyproject.toml" | |
workflow_dispatch: # Allow manual trigger | |
jobs: | |
bump-version: | |
if: "!contains(github.event.head_commit.message, '[skip-version]')" | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
fetch-depth: 0 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: "3.x" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install build twine bump2version setuptools | |
- name: Configure Git | |
run: | | |
git config --local user.email "action@github.com" | |
git config --local user.name "GitHub Action" | |
- name: Determine version bump type | |
id: bump_type | |
run: | | |
commit_message="${{ github.event.head_commit.message }}" | |
if [[ "$commit_message" == *"[major]"* ]]; then | |
echo "bump_type=major" >> $GITHUB_OUTPUT | |
elif [[ "$commit_message" == *"[minor]"* ]]; then | |
echo "bump_type=minor" >> $GITHUB_OUTPUT | |
else | |
echo "bump_type=patch" >> $GITHUB_OUTPUT | |
fi | |
- name: Bump version | |
run: | | |
# Get current version from setup.py | |
current_version=$(python setup.py --version) | |
echo "Current version: $current_version" | |
# Determine new version based on bump type | |
python -c " | |
import re | |
bump_type = '${{ steps.bump_type.outputs.bump_type }}' | |
with open('setup.py', 'r') as f: | |
content = f.read() | |
version_match = re.search(r'version=\"([^\"]+)\"', content) | |
if version_match: | |
old_version = version_match.group(1) | |
version_parts = [int(x) for x in old_version.split('.')] | |
if bump_type == 'major': | |
version_parts[0] += 1 | |
version_parts[1] = 0 | |
version_parts[2] = 0 | |
elif bump_type == 'minor': | |
version_parts[1] += 1 | |
version_parts[2] = 0 | |
else: # patch | |
version_parts[2] += 1 | |
new_version = '.'.join(map(str, version_parts)) | |
new_content = content.replace(f'version=\"{old_version}\"', f'version=\"{new_version}\"') | |
with open('setup.py', 'w') as f: | |
f.write(new_content) | |
print(f'Updated version from {old_version} to {new_version} ({bump_type} bump)') | |
" | |
# Update pyproject.toml as well | |
python -c " | |
import re | |
bump_type = '${{ steps.bump_type.outputs.bump_type }}' | |
with open('pyproject.toml', 'r') as f: | |
content = f.read() | |
version_match = re.search(r'version = \"([^\"]+)\"', content) | |
if version_match: | |
old_version = version_match.group(1) | |
version_parts = [int(x) for x in old_version.split('.')] | |
if bump_type == 'major': | |
version_parts[0] += 1 | |
version_parts[1] = 0 | |
version_parts[2] = 0 | |
elif bump_type == 'minor': | |
version_parts[1] += 1 | |
version_parts[2] = 0 | |
else: # patch | |
version_parts[2] += 1 | |
new_version = '.'.join(map(str, version_parts)) | |
new_content = content.replace(f'version = \"{old_version}\"', f'version = \"{new_version}\"') | |
with open('pyproject.toml', 'w') as f: | |
f.write(new_content) | |
print(f'Updated pyproject.toml version from {old_version} to {new_version} ({bump_type} bump)') | |
" | |
- name: Get new version | |
id: version | |
run: | | |
new_version=$(python setup.py --version) | |
echo "new_version=$new_version" >> $GITHUB_OUTPUT | |
echo "New version: $new_version" | |
- name: Check if version exists on PyPI | |
id: check_version | |
run: | | |
new_version=$(python setup.py --version) | |
echo "Checking if version $new_version exists on PyPI..." | |
# Check if version exists on PyPI | |
response=$(curl -s -o /dev/null -w "%{http_code}" "https://pypi.org/pypi/cpulimiter/$new_version/json") | |
if [ "$response" = "200" ]; then | |
echo "Version $new_version already exists on PyPI. Skipping upload." | |
echo "version_exists=true" >> $GITHUB_OUTPUT | |
else | |
echo "Version $new_version does not exist on PyPI. Proceeding with upload." | |
echo "version_exists=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Build package | |
if: steps.check_version.outputs.version_exists == 'false' | |
run: python -m build | |
- name: Publish to PyPI | |
if: steps.check_version.outputs.version_exists == 'false' | |
env: | |
TWINE_USERNAME: __token__ | |
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
run: twine upload dist/* | |
- name: Commit version bump | |
if: steps.check_version.outputs.version_exists == 'false' | |
run: | | |
git add setup.py pyproject.toml | |
git commit -m "🚀 Bump version to ${{ steps.version.outputs.new_version }} [${{ steps.bump_type.outputs.bump_type }}] [skip-version]" | |
git tag "v${{ steps.version.outputs.new_version }}" | |
git push origin main | |
git push origin "v${{ steps.version.outputs.new_version }}" |