ci: add auto-publish workflow with CalVer versioning (#2) #1
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: Publish to PyPI | |
| on: | |
| push: | |
| branches: [main] | |
| workflow_dispatch: | |
| concurrency: | |
| group: publish-${{ github.ref }} | |
| cancel-in-progress: false | |
| jobs: | |
| build: | |
| name: Build distribution | |
| runs-on: ubuntu-latest | |
| outputs: | |
| version: ${{ steps.version.outputs.version }} | |
| permissions: | |
| contents: read | |
| steps: | |
| - uses: actions/checkout@v6 | |
| with: | |
| fetch-depth: 0 | |
| - name: Set up Python | |
| uses: actions/setup-python@v6 | |
| with: | |
| python-version: '3.12' | |
| - name: Generate date-based version | |
| id: version | |
| env: | |
| PACKAGE_NAME: acedatacloud-scaffold | |
| run: | | |
| python - <<'PY' | |
| import json | |
| import os | |
| import urllib.request | |
| from datetime import datetime, timezone | |
| package_name = os.environ['PACKAGE_NAME'] | |
| date_prefix = datetime.now(timezone.utc).strftime('%Y.%-m.%-d') | |
| next_build = 0 | |
| try: | |
| with urllib.request.urlopen(f'https://pypi.org/pypi/{package_name}/json', timeout=10) as response: | |
| releases = json.load(response).get('releases', {}) | |
| builds = [] | |
| for release in releases: | |
| if not release.startswith(f'{date_prefix}.'): | |
| continue | |
| parts = release.split('.') | |
| if len(parts) != 4: | |
| continue | |
| try: | |
| builds.append(int(parts[3])) | |
| except ValueError: | |
| continue | |
| if builds: | |
| next_build = max(builds) + 1 | |
| except Exception: | |
| next_build = 0 | |
| version = f'{date_prefix}.{next_build}' | |
| with open(os.environ['GITHUB_OUTPUT'], 'a', encoding='utf-8') as fh: | |
| fh.write(f'version={version}\n') | |
| print(f'Generated version: {version}') | |
| PY | |
| - name: Update package version | |
| env: | |
| VERSION: ${{ steps.version.outputs.version }} | |
| run: | | |
| python - <<'PY' | |
| import os | |
| import pathlib | |
| import re | |
| version = os.environ['VERSION'] | |
| version_file = pathlib.Path('acedatacloud_scaffold/__version__.py') | |
| content = version_file.read_text(encoding='utf-8') | |
| updated = re.sub( | |
| r'^VERSION = \([^\n]+\)$', | |
| f'VERSION = ({", ".join(version.split("."))})', | |
| content, | |
| count=1, | |
| flags=re.MULTILINE, | |
| ) | |
| if updated == content: | |
| raise SystemExit('Failed to update VERSION tuple in acedatacloud_scaffold/__version__.py') | |
| version_file.write_text(updated, encoding='utf-8') | |
| print(version_file.read_text(encoding='utf-8')) | |
| PY | |
| - name: Install build dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install build "twine>=6.1.0,<7.0.0" | |
| - name: Build package | |
| run: python -m build | |
| - name: Check package metadata | |
| run: twine check dist/* | |
| - name: Upload distribution artifacts | |
| uses: actions/upload-artifact@v7 | |
| with: | |
| name: python-package-distributions | |
| path: dist/ | |
| publish-pypi: | |
| name: Publish to PyPI | |
| needs: build | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: pypi | |
| url: https://pypi.org/project/acedatacloud-scaffold/${{ needs.build.outputs.version }} | |
| permissions: | |
| contents: read | |
| steps: | |
| - name: Download distribution artifacts | |
| uses: actions/download-artifact@v8 | |
| with: | |
| name: python-package-distributions | |
| path: dist/ | |
| - name: Publish to PyPI | |
| env: | |
| TWINE_USERNAME: __token__ | |
| TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install "twine>=6.1.0,<7.0.0" | |
| twine upload dist/* | |
| create-release: | |
| name: Create GitHub Release | |
| if: github.event_name == 'push' | |
| needs: [build, publish-pypi] | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - uses: actions/checkout@v6 | |
| - name: Download distribution artifacts | |
| uses: actions/download-artifact@v8 | |
| with: | |
| name: python-package-distributions | |
| path: dist/ | |
| - name: Create Release | |
| env: | |
| GITHUB_TOKEN: ${{ github.token }} | |
| run: | | |
| gh release create "v${{ needs.build.outputs.version }}" dist/* \ | |
| --title "v${{ needs.build.outputs.version }}" \ | |
| --generate-notes \ | |
| --repo '${{ github.repository }}' || echo "Release already exists, skipping" |