Build and publish python package #13
Workflow file for this run
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: Build and publish python package | |
| on: | |
| push: | |
| tags: | |
| - "v*" | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: 'Version to publish (e.g., 0.1.0)' | |
| required: false | |
| type: string | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write # Required for creating GitHub releases | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Fetch all history for tags | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.10" | |
| - name: Extract version from tag or input | |
| id: get_version | |
| run: | | |
| if [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ -n "${{ github.event.inputs.version }}" ]; then | |
| echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT | |
| elif [ -n "${{ github.ref_name }}" ]; then | |
| # Extract version from tag (e.g., v0.1.0 -> 0.1.0) | |
| VERSION=$(echo "${{ github.ref_name }}" | sed 's/^v//') | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| else | |
| # Fallback: extract from pyproject.toml | |
| VERSION=$(grep -E '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/') | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| fi | |
| echo "Publishing version: $(cat $GITHUB_OUTPUT | grep version | cut -d'=' -f2)" | |
| - name: Verify version matches pyproject.toml | |
| run: | | |
| PUBLISH_VERSION="${{ steps.get_version.outputs.version }}" | |
| FILE_VERSION=$(grep -E '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/') | |
| if [ "$PUBLISH_VERSION" != "$FILE_VERSION" ]; then | |
| echo "Error: Version mismatch!" | |
| echo "Tag/Input version: $PUBLISH_VERSION" | |
| echo "pyproject.toml version: $FILE_VERSION" | |
| exit 1 | |
| fi | |
| echo "✓ Version verified: $PUBLISH_VERSION" | |
| - name: Install build tools | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install build twine auditwheel | |
| - name: Build package | |
| run: python -m build | |
| # PyPI rejects bare 'linux_x86_64' wheels; require PEP 600 manylinux tags. | |
| - name: Repair wheel with auditwheel (manylinux) | |
| run: | | |
| sudo apt-get update -qq | |
| sudo apt-get install -qq patchelf | |
| mkdir -p repaired | |
| shopt -s nullglob | |
| for whl in dist/*linux*.whl; do | |
| echo "Repairing: $whl" | |
| auditwheel repair "$whl" -w repaired | |
| rm -f "$whl" | |
| done | |
| for w in repaired/*.whl; do | |
| mv "$w" dist/ | |
| done | |
| shopt -u nullglob | |
| rmdir repaired 2>/dev/null || true | |
| ls -la dist/ | |
| - name: Check package contents | |
| run: | | |
| echo "Built files:" | |
| ls -lh dist/ | |
| echo "" | |
| echo "Package info:" | |
| python -m twine check dist/* | |
| - name: Publish to PyPI | |
| env: | |
| TWINE_USERNAME: __token__ | |
| TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} | |
| run: twine upload --verbose dist/* | |
| - name: Create GitHub Release | |
| if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: ${{ github.ref_name }} | |
| name: Release ${{ steps.get_version.outputs.version }} | |
| body: | | |
| ## Release ${{ steps.get_version.outputs.version }} | |
| Published to PyPI: https://pypi.org/project/spherical-deepkriging/${{ steps.get_version.outputs.version }}/ | |
| ### Installation | |
| ```bash | |
| pip install spherical-deepkriging==${{ steps.get_version.outputs.version }} | |
| ``` | |
| draft: false | |
| prerelease: false | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |