diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f3ef81d..3b94f00 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,8 +1,13 @@ name: Release +# Run this workflow on main (workflow_dispatch) to release the version in +# pyproject.toml: it publishes to PyPI, then creates the v GitHub +# Release. Publishing a GitHub Release by hand still works too. Every step +# skips what is already published, so a re-run only fills in what is missing. on: release: types: [published] + workflow_dispatch: jobs: build: @@ -16,7 +21,11 @@ jobs: - uses: actions/setup-python@v6 with: python-version: "3.12" + - name: Release only from main + if: github.event_name == 'workflow_dispatch' + run: test "$GITHUB_REF" = refs/heads/main - name: Verify release tag + if: github.event_name == 'release' env: RELEASE_TAG: ${{ github.event.release.tag_name }} run: >- @@ -49,3 +58,28 @@ jobs: name: python-package-distributions path: dist/ - uses: pypa/gh-action-pypi-publish@release/v1 + with: + skip-existing: true + + # A Release created with GITHUB_TOKEN does not start this workflow again. + github-release: + needs: publish + if: github.event_name == 'workflow_dispatch' + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + - name: Create GitHub Release + env: + GH_TOKEN: ${{ github.token }} + run: | + VERSION=$(python3 -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])") + TAG="v$VERSION" + if gh release view "$TAG" >/dev/null 2>&1; then + echo "$TAG already has a GitHub Release" + else + gh release create "$TAG" --target "$GITHUB_SHA" --title "$TAG" --generate-notes + fi