|
| 1 | +name: Publish Package |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: ['v*'] # Triggers on any tag push |
| 6 | + release: |
| 7 | + types: [published] # Triggers when a GitHub release is published |
| 8 | + workflow_dispatch: # Manual trigger |
| 9 | + |
| 10 | +jobs: |
| 11 | + publish: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + id-token: write # Required for trusted publishing |
| 15 | + contents: read |
| 16 | + |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: Set up Python |
| 23 | + uses: actions/setup-python@v5 |
| 24 | + with: |
| 25 | + python-version: '3.12' |
| 26 | + |
| 27 | + - name: Install build tools |
| 28 | + run: pip install --upgrade build setuptools wheel setuptools-scm twine |
| 29 | + |
| 30 | + - name: Extract version |
| 31 | + id: get_version |
| 32 | + run: | |
| 33 | + # Get clean version from the tag or release |
| 34 | + if [[ "${{ github.event_name }}" == "release" ]]; then |
| 35 | + # For releases, get the version from the release tag |
| 36 | + TAG_NAME="${{ github.event.release.tag_name }}" |
| 37 | + else |
| 38 | + # For tags, get version from the tag |
| 39 | + TAG_NAME="${{ github.ref_name }}" |
| 40 | + fi |
| 41 | +
|
| 42 | + # Remove 'v' prefix |
| 43 | + VERSION=$(echo $TAG_NAME | sed 's/^v//') |
| 44 | +
|
| 45 | + # Check if this is a stable version (no rc, alpha, beta, dev, etc.) |
| 46 | + if [[ $TAG_NAME =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 47 | + echo "IS_STABLE=true" >> $GITHUB_ENV |
| 48 | + else |
| 49 | + echo "IS_STABLE=false" >> $GITHUB_ENV |
| 50 | + fi |
| 51 | +
|
| 52 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 53 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 54 | +
|
| 55 | + - name: Build package |
| 56 | + run: | |
| 57 | + # Force clean version |
| 58 | + export SETUPTOOLS_SCM_PRETEND_VERSION=$VERSION |
| 59 | + python -m build |
| 60 | +
|
| 61 | + - name: Check dist |
| 62 | + run: | |
| 63 | + ls -alh |
| 64 | + twine check dist/* |
| 65 | +
|
| 66 | + # Always publish to TestPyPI for all tags and releases |
| 67 | + # TODO: Enable it later. |
| 68 | +# - name: Publish to TestPyPI |
| 69 | +# uses: pypa/gh-action-pypi-publish@release/v1 |
| 70 | +# with: |
| 71 | +# repository-url: https://test.pypi.org/legacy/ |
| 72 | +# password: ${{ secrets.TEST_PYPI_API_TOKEN }} |
| 73 | +# skip-existing: true |
| 74 | +# verbose: true |
| 75 | + |
| 76 | + # Only publish to PyPI for stable GitHub releases (no RC/alpha/beta) |
| 77 | + - name: Publish to PyPI |
| 78 | + # TODO: Enable '&& env.IS_STABLE == 'true' only publish to PyPI for stable GitHub releases (no RC/alpha/beta) |
| 79 | + if: github.event_name == 'release' #&& env.IS_STABLE == 'true' |
| 80 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 81 | + with: |
| 82 | + user: __token__ |
| 83 | + password: ${{ secrets.PYPI_API_TOKEN }} |
| 84 | + verbose: true |
0 commit comments