diff --git a/.github/workflows/publish-to-pypi.yml b/.github/workflows/publish-to-pypi.yml index cfbf991..e9ac061 100644 --- a/.github/workflows/publish-to-pypi.yml +++ b/.github/workflows/publish-to-pypi.yml @@ -10,9 +10,15 @@ on: required: true default: "" +permissions: + contents: read + jobs: publish: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 + permissions: + # Creating the GitHub Release for the version. + contents: write environment: production # Optional: use environment for additional security steps: @@ -33,6 +39,25 @@ jobs: fi echo "✅ Confirmation received for manual publish" + # The tag is the source of truth for the version; the files in the repo + # carry whatever was last committed. These edits live and die with the + # runner. + - name: Set the version from the tag + run: | + if [ "$GITHUB_REF_TYPE" != "tag" ]; then + echo "Run this workflow from a v* tag, not $GITHUB_REF." + exit 1 + fi + if ! [[ "$GITHUB_REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z.-]+)?$ ]]; then + echo "Tag $GITHUB_REF_NAME is not a version like v1.2.3." + exit 1 + fi + VERSION="${GITHUB_REF_NAME#v}" + sed -i "s/^version = \".*\"/version = \"$VERSION\"/" pyproject.toml + sed -i "s/^__version__ = \".*\"/__version__ = \"$VERSION\"/" tonic_fabricate/__init__.py + grep -n '^version = ' pyproject.toml + grep -n '^__version__ = ' tonic_fabricate/__init__.py + - name: Make publish script executable run: chmod +x publish.sh @@ -60,13 +85,15 @@ jobs: # Run the publish script in non-interactive mode ./publish.sh - - name: Create GitHub Release (if triggered by tag) - if: github.event_name == 'workflow_dispatch' && startsWith(github.ref, 'refs/tags/') - uses: softprops/action-gh-release@v1 - with: - files: | - dist/*.whl - dist/*.tar.gz - generate_release_notes: true + # Skip if the release exists: creating one in the UI also pushes the tag, + # and that is what triggers this workflow in the first place. + - name: Create the GitHub Release unless it exists env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ github.token }} + run: | + if gh release view "$GITHUB_REF_NAME" > /dev/null 2>&1; then + echo "Release $GITHUB_REF_NAME already exists. Skipping." + else + gh release create "$GITHUB_REF_NAME" dist/*.whl dist/*.tar.gz \ + --generate-notes --verify-tag + fi