Publish to PyPI #28
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: | |
| workflow_dispatch: | |
| inputs: | |
| environment: | |
| description: 'Choose environment' | |
| required: true | |
| type: choice | |
| options: | |
| - test | |
| - production | |
| default: test | |
| permissions: | |
| contents: write | |
| jobs: | |
| build-and-publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.11" | |
| - name: Upgrade pip | |
| run: python -m pip install --upgrade pip | |
| - name: Install build tools | |
| run: python -m pip install --upgrade build twine | |
| - name: Get version | |
| id: get_version | |
| run: | | |
| VERSION=$(grep -oP 'version = "\K[^"]+' pyproject.toml) | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "VERSION is: $VERSION" | |
| - name: Extract release notes | |
| if: ${{ github.event.inputs.environment == 'production' }} | |
| run: | | |
| VERSION=$(grep -oP 'version = "\K[^"]+' pyproject.toml) | |
| echo "VERSION is: $VERSION" | |
| awk -v v="$VERSION" '$0 ~ "^### Version " v ":" {found=1; next} found && /^### Version / {exit} found {print}' CHANGELOG.md > release_notes.md | |
| echo "Release notes content:" | |
| cat release_notes.md | |
| - name: Modify version for test environment | |
| if: ${{ github.event.inputs.environment == 'test' }} | |
| run: | | |
| TIMESTAMP=$(date +%Y%m%d%H%M) | |
| VERSION=$(grep -oP 'version = "\K[^"]+' pyproject.toml) | |
| IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION" | |
| NEW_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1)).dev${TIMESTAMP}" | |
| sed -i "s/version = \".*\"/version = \"${NEW_VERSION}\"/" pyproject.toml | |
| - name: Build package | |
| run: python -m build | |
| - name: Publish to Test PyPI | |
| if: ${{ github.event.inputs.environment == 'test' }} | |
| env: | |
| TWINE_USERNAME: __token__ | |
| TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }} | |
| run: twine upload --repository testpypi dist/* | |
| - name: Publish to PyPI | |
| if: ${{ github.event.inputs.environment == 'production' }} | |
| env: | |
| TWINE_USERNAME: __token__ | |
| TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
| run: twine upload --skip-existing dist/* | |
| - name: Create Git Tag | |
| if: ${{ github.event.inputs.environment == 'production' }} | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git tag -a "v${{ steps.get_version.outputs.version }}" -m "Release v${{ steps.get_version.outputs.version }}" | |
| git push origin "v${{ steps.get_version.outputs.version }}" | |
| - name: Create GitHub Release | |
| if: ${{ github.event.inputs.environment == 'production' }} | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: v${{ steps.get_version.outputs.version }} | |
| name: Release v${{ steps.get_version.outputs.version }} | |
| draft: false | |
| prerelease: false | |
| body_path: release_notes.md |