Create Release and Publish #9
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: Create Release and Publish | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| id-token: write | |
| jobs: | |
| release: | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/master' | |
| environment: pypi | |
| outputs: | |
| version: ${{ steps.changelog.outputs.version }} | |
| released: ${{ steps.check_tag.outputs.exists == 'false' }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v5 | |
| - name: Extract version and notes from CHANGELOG | |
| id: changelog | |
| run: | | |
| # Extract version from first ## [x.x.x] header | |
| VERSION=$(grep -m1 -oP '## \[\K[0-9]+\.[0-9]+\.[0-9]+' CHANGELOG.md) | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| # Extract release notes (content between first and second ## headers) | |
| NOTES=$(awk '/^## \['"$VERSION"'\]/{flag=1; next} /^## \[/{flag=0} flag' CHANGELOG.md) | |
| # Handle multiline output | |
| { | |
| echo "notes<<EOF" | |
| echo "$NOTES" | |
| echo "EOF" | |
| } >> $GITHUB_OUTPUT | |
| - name: Check if tag already exists | |
| id: check_tag | |
| run: | | |
| if git rev-parse "v${{ steps.changelog.outputs.version }}" >/dev/null 2>&1; then | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Create Release | |
| if: steps.check_tag.outputs.exists == 'false' | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| tag_name: ${{ steps.changelog.outputs.version }} | |
| name: v${{ steps.changelog.outputs.version }} | |
| body: ${{ steps.changelog.outputs.notes }} | |
| draft: false | |
| prerelease: false | |
| - name: Skip release (tag exists) | |
| if: steps.check_tag.outputs.exists == 'true' | |
| run: | | |
| echo "⚠️ Tag v${{ steps.changelog.outputs.version }} already exists. Skipping release creation." | |
| exit 1 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.x" | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install build | |
| - name: Build package | |
| run: python -m build | |
| - name: Publish to PyPI | |
| uses: pypa/gh-action-pypi-publish@release/v1 |