Publish to PyPI #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: Publish to PyPI | |
| on: | |
| release: | |
| types: [published] # Trigger on manual release publications | |
| workflow_call: | |
| inputs: | |
| publish_test: | |
| description: "Publish to TestPyPI" | |
| required: false | |
| type: boolean | |
| default: false | |
| workflow_dispatch: | |
| inputs: | |
| publish_test: | |
| description: "Publish to TestPyPI" | |
| required: false | |
| type: boolean | |
| default: false | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| if: inputs.publish_test != true | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: "3.9" | |
| - name: Install Poetry | |
| uses: snok/install-poetry@v1 | |
| - name: Configure Poetry | |
| run: | | |
| poetry config pypi-token.pypi ${{ secrets.PYPI_TOKEN }} | |
| - name: Configure SSH | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/deploy_key | |
| chmod 600 ~/.ssh/deploy_key | |
| ssh-keyscan github.com >> ~/.ssh/known_hosts | |
| - name: Update versions | |
| run: | | |
| # Configure git to use SSH | |
| git config --local core.sshCommand "ssh -i ~/.ssh/deploy_key" | |
| git remote set-url origin git@github.com:${GITHUB_REPOSITORY}.git | |
| # Get version from git tag | |
| VERSION=$(git describe --tags --abbrev=0) | |
| VERSION=${VERSION#v} # Remove 'v' prefix if present | |
| # Update pyproject.toml | |
| poetry version $VERSION | |
| # Update __init__.py | |
| echo "__version__ = \"$VERSION\"" > pythonik/__init__.py | |
| # Fetch and checkout main branch | |
| git fetch origin main | |
| git checkout main | |
| # Commit and push the changes | |
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
| git config --local user.name "github-actions[bot]" | |
| git add pyproject.toml pythonik/__init__.py | |
| git commit -m "chore: update version to $VERSION [skip ci]" | |
| git push origin main | |
| - name: Build and publish | |
| run: | | |
| poetry build | |
| poetry publish | |
| publish-test: | |
| runs-on: ubuntu-latest | |
| if: inputs.publish_test == true && github.event_name == 'workflow_dispatch' # Only run for manual triggers with publish_test true | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: "3.9" | |
| - name: Install Poetry | |
| uses: snok/install-poetry@v1 | |
| - name: Configure Poetry | |
| run: | | |
| poetry config repositories.testpypi https://test.pypi.org/legacy/ | |
| poetry config pypi-token.testpypi ${{ secrets.TEST_PYPI_TOKEN }} | |
| - name: Configure SSH | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/deploy_key | |
| chmod 600 ~/.ssh/deploy_key | |
| ssh-keyscan github.com >> ~/.ssh/known_hosts | |
| - name: Update versions | |
| run: | | |
| # Configure git to use SSH | |
| git config --local core.sshCommand "ssh -i ~/.ssh/deploy_key" | |
| git remote set-url origin git@github.com:${GITHUB_REPOSITORY}.git | |
| # Get version from git tag | |
| VERSION=$(git describe --tags --abbrev=0) | |
| VERSION=${VERSION#v} | |
| poetry version $VERSION | |
| echo "__version__ = \"$VERSION\"" > pythonik/__init__.py | |
| - name: Build and publish to TestPyPI | |
| run: | | |
| poetry build | |
| poetry publish -r testpypi |