PyPI Publishing Setup Guide¶
Complete guide to setting up automated PyPI publishing for the MSN Weather Wrapper package.
Overview¶
The project uses GitHub's trusted publishing (OIDC) for secure PyPI uploads with no stored secrets. The first release requires manual setup, but subsequent releases are fully automated.
Quick Setup (First Release)¶
Step 1: Create PyPI Account¶
- Go to PyPI.org
- Register a new account
- Verify your email address
Step 2: Upload First Release Manually¶
The first release must be uploaded manually to claim the package name on PyPI.
# Install build tools
pip install build twine
# Build package
python -m build
# Upload to PyPI (interactive)
python -m twine upload dist/*
You'll be prompted for your PyPI username and password.
Step 3: Configure Trusted Publishing (OIDC)¶
After the first manual upload:
- Open the project's Publishing settings (Manage → Settings → Publishing, https://pypi.org/manage/project/msn-weather-wrapper/settings/publishing/)
- Add a trusted publisher:
- Owner:
jim-wyatt - Repository name:
msn-weather-wrapper - Workflow name:
publish-release.yml - Environment name:
pypi(the environment used bypublish-release.yml) - Click Add
The publisher is active immediately. (Alternatively, skip the manual first upload: add a pending publisher with the same values under your account's Publishing settings, https://pypi.org/manage/account/publishing/, and the first automated release creates the project.)
Step 4: Create the pypi GitHub environment¶
The publish-pypi job in publish-release.yml runs in the pypi environment, which is required and must exist in the repository:
- Go to Settings → Environments → New environment
- Name:
pypi - Under Deployment branches and tags, select Selected branches and tags and allow branch
mainand tag patternv* - Optionally add required reviewers or a wait timer
No repository secret is needed: the job requests an OIDC token (id-token: write) at release time and pypa/gh-action-pypi-publish exchanges it for a short-lived PyPI token. There is no PYPI_TOKEN.
Step 5: Verify a published release¶
Each release's wheel and sdist carry a signed build-provenance attestation (actions/attest in publish-release.yml). Consumers can verify what they install:
pip download msn-weather-wrapper --no-deps
gh attestation verify msn_weather_wrapper-*.whl --repo jim-wyatt/msn-weather-wrapper
Automated Publishing Workflow¶
Once OIDC is configured, releases are fully automatic:
- Developer creates PR with changes
- PR merged to main
- ci.yml runs on
main; on a green, releasable run itsReleasejob tagsvX.Y.Z(the version is derived from the tag by hatch-vcs), which: - Determines version bump
- Creates and merges version bump PR
- Pushes version tag
- publish-release.yml triggers on tag:
- Sets the package version from the tag
- Builds Python package and attests its provenance
- Uses OIDC to authenticate to PyPI (in the
pypienvironment) - Publishes to PyPI
- Creates GitHub Release
- Package available on PyPI ✅
Version Management¶
Conventional Commits¶
Use conventional commit prefixes in PR titles for automatic version bumping:
| Prefix | Type | Bump | Example |
|---|---|---|---|
feat: |
New Feature | Minor (0.X.0) | feat: add weather alerts |
fix: |
Bug Fix | Patch (0.0.X) | fix: correct temp parsing |
feat!: |
Breaking Feature | Major (X.0.0) | feat!: redesign API |
breaking!: |
Breaking Change | Major (X.0.0) | breaking!: change response format |
docs: |
Documentation | Patch (0.0.X) | docs: update README |
chore: |
Maintenance | Patch (0.0.X) | chore: update deps |
Default: Patch version (0.0.X)
Testing Before Release¶
Test Locally¶
# Build the package
python -m build
# Verify package integrity
python -m twine check dist/*
# Optional: Test on TestPyPI first
python -m twine upload --repository testpypi dist/*
Verify Package Metadata¶
# Check what will be published
tar -tzf dist/*.tar.gz | head -20
# Verify wheel contents
unzip -l dist/*.whl | head -20
Troubleshooting¶
OIDC Configuration Issues¶
Problem: publish-pypi fails with invalid-publisher or 403 Forbidden
Solution: The OIDC token's claims must match the trusted publisher exactly. Check, in order:
- The workflow filename on PyPI is
publish-release.yml(not a display name) - The environment name on PyPI is
pypiand the job runs in that environment - Owner is
jim-wyattand repository ismsn-weather-wrapper(case-sensitive) - The
publish-pypijob still haspermissions: id-token: write - The
pypienvironment's deployment branch/tag rule allows the ref being published (mainand tagsv*); a tag that does not match the rule fails before the job starts - Check the
publish-release.ymllogs for the exact claim PyPI rejected
Package Upload Issues¶
Problem: Version already exists on PyPI
Solution:
- Versions are immutable on PyPI
- Create a new version bump (auto-version handles this)
- Cannot override or delete existing versions
Problem: Package name already taken
Solution:
- Update
project.nameinpyproject.toml - Ensure uniqueness on PyPI
- Re-test with TestPyPI first
Version Tag Issues¶
Problem: Tag not created automatically
Solution:
- Check the
Releasejob of theCI/CD Pipelinerun onmain - Verify version bump PR was merged
- Manually create tag if needed:
Manual Release Process¶
If automated release fails, you can manually publish:
# Build package
python -m build
# Get OIDC token (if using trusted publishing)
# This is handled automatically by the workflow
# For manual releases, use password/token auth
# Upload to PyPI
python -m twine upload dist/*
# Create GitHub release manually
gh release create v1.2.3 --title "Release v1.2.3" dist/*
Security Best Practices¶
✅ Do¶
- Use GitHub OIDC trusted publishing (no tokens stored)
- Keep
pyproject.tomlversion synchronized with git tags - Review
publish-release.ymllogs for each release - Test on TestPyPI before first production release
- Use conventional commits for clear version history
❌ Don't¶
- Never store PyPI tokens in GitHub secrets
- Never manually bump versions (let automation handle it)
- Never release without running full CI/CD pipeline
- Never use the same version twice
Useful Commands¶
# Check current package version
grep '^version' pyproject.toml
# Build package locally
python -m build
# Check package contents
tar -tzf dist/*.tar.gz
unzip -l dist/*.whl
# Verify metadata
python -m twine check dist/*
# Upload to TestPyPI
python -m twine upload --repository testpypi dist/*
# Upload to PyPI
python -m twine upload dist/*
# List releases
gh release list
# Check publish workflow runs
gh run list --workflow publish-release.yml
Additional Resources¶
- PyPI Documentation
- Trusted Publishing Guide
- Python Packaging Guide
- Hatch Documentation
- twine Documentation
Related Documentation¶
- CI/CD Workflows - Complete workflow documentation
- Versioning Guide - Version management process
- Development Guide - Local development setup