add: github workflow #1
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: Publish to PyPI and Create Release | |
on: | |
push: | |
tags: | |
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 | |
jobs: | |
build-publish-release: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Check if version exists on PyPI | |
id: check-pypi | |
run: | | |
TAG_VERSION="${GITHUB_REF##*/}" | |
VERSION="${TAG_VERSION#v}" | |
PKG="${GITHUB_REPOSITORY##*/}" | |
EXISTS=$(curl -s https://pypi.org/pypi/${PKG}/json | jq -e ".releases[\"${VERSION}\"] | length > 0" 2>/dev/null && echo "true" || echo "false") | |
EXISTS=$(echo $EXISTS | tr -d '\r\n') | |
echo "skip_publish<<EOF" >> $GITHUB_OUTPUT | |
echo "$EXISTS" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT | |
shell: bash | |
- name: Version already exists on PyPI, skipping publish | |
if: steps.check-pypi.outputs.skip_publish == 'true' | |
run: | | |
echo "Version already exists on PyPI. Skipping build and publish steps." | |
exit 0 | |
- name: Install uv | |
uses: astral-sh/setup-uv@v5 | |
with: | |
enable-cache: true | |
- name: Install the project | |
run: uv sync --all-extras --dev | |
- name: Build package | |
run: uv build | |
- name: Publish to PyPI | |
run: uv publish --token ${{ secrets.PYPI_API_TOKEN }} | |
- name: Generate changelog | |
id: changelog | |
run: | | |
PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "") | |
if [ -z "$PREV_TAG" ]; then | |
CHANGELOG=$(git log --pretty=format:"- %s" ${{ github.sha }}) | |
else | |
CHANGELOG=$(git log --pretty=format:"- %s" ${PREV_TAG}..HEAD) | |
fi | |
echo "CHANGELOG<<EOF" >> $GITHUB_OUTPUT | |
echo "$CHANGELOG" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
- name: Create GitHub Release | |
uses: softprops/action-gh-release@v2 | |
with: | |
tag_name: v${{ steps.check-pypi.outputs.VERSION }} | |
body: | | |
## 🔄Changes | |
${{ steps.changelog.outputs.CHANGELOG }} | |
## 📦Update | |
```bash | |
uv sync --force-reinstall | |
``` |