|
| 1 | +# Quick Start Guide: Publishing to PyPI |
| 2 | + |
| 3 | +This guide will help you quickly set up and publish your packages to PyPI. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- GitHub repository with the workflows |
| 8 | +- PyPI account (https://pypi.org/account/register/) |
| 9 | +- TestPyPI account (https://test.pypi.org/account/register/) - optional but recommended |
| 10 | + |
| 11 | +## Step 1: Get PyPI Tokens |
| 12 | + |
| 13 | +### For PyPI |
| 14 | + |
| 15 | +1. Visit https://pypi.org/manage/account/token/ |
| 16 | +2. Click "Add API token" |
| 17 | +3. Fill in: |
| 18 | + - Token name: `GitHub Actions - NetDriver` |
| 19 | + - Scope: `Entire account` (or select specific project after first upload) |
| 20 | +4. Click "Add token" |
| 21 | +5. **Copy the token** (starts with `pypi-...`) - you won't see it again! |
| 22 | + |
| 23 | +### For TestPyPI (Recommended for testing) |
| 24 | + |
| 25 | +1. Visit https://test.pypi.org/manage/account/token/ |
| 26 | +2. Follow same steps as above |
| 27 | +3. Copy the token |
| 28 | + |
| 29 | +## Step 2: Add Secrets to GitHub |
| 30 | + |
| 31 | +1. Go to your GitHub repository |
| 32 | +2. Navigate to: **Settings** → **Secrets and variables** → **Actions** |
| 33 | +3. Click "New repository secret" |
| 34 | +4. Add two secrets: |
| 35 | + |
| 36 | + **Secret 1:** |
| 37 | + - Name: `PYPI_API_TOKEN` |
| 38 | + - Secret: Paste your PyPI token |
| 39 | + |
| 40 | + **Secret 2:** |
| 41 | + - Name: `TEST_PYPI_API_TOKEN` |
| 42 | + - Secret: Paste your TestPyPI token |
| 43 | + |
| 44 | +## Step 3: Choose Your Workflow |
| 45 | + |
| 46 | +### Option A: Standard Workflow (Recommended for first-time) |
| 47 | + |
| 48 | +Use `publish-pypi.yml` - installs everything on-the-fly |
| 49 | + |
| 50 | +**No additional setup needed!** |
| 51 | + |
| 52 | +### Option B: Docker-based Workflow (Faster) |
| 53 | + |
| 54 | +Use `publish-pypi-docker.yml` - uses pre-built image |
| 55 | + |
| 56 | +**Additional setup:** |
| 57 | + |
| 58 | +1. Build the CI image first: |
| 59 | + ```bash |
| 60 | + # Go to Actions → "Build CI Image" → Run workflow |
| 61 | + # Or build locally and push |
| 62 | + docker build -t ghcr.io/opensecflow/netdriver/ci:latest -f .github/Dockerfile.ci . |
| 63 | + docker push ghcr.io/opensecflow/netdriver/ci:latest |
| 64 | + ``` |
| 65 | + |
| 66 | +2. Update your workflow to use the image (already configured in `publish-pypi-docker.yml`) |
| 67 | + |
| 68 | +## Step 4: Test Publishing (Recommended) |
| 69 | + |
| 70 | +Before publishing to production PyPI, test with TestPyPI: |
| 71 | + |
| 72 | +### Manual Test |
| 73 | + |
| 74 | +1. Go to **Actions** → **"Publish to PyPI"** |
| 75 | +2. Click **"Run workflow"** |
| 76 | +3. Select: |
| 77 | + - Branch: `master` |
| 78 | + - Environment: **`testpypi`** |
| 79 | + - Projects: **`all`** |
| 80 | +4. Click **"Run workflow"** |
| 81 | + |
| 82 | +### Verify on TestPyPI |
| 83 | + |
| 84 | +1. Check your packages: |
| 85 | + - https://test.pypi.org/project/netdriver-agent/ |
| 86 | + - https://test.pypi.org/project/netdriver-simunet/ |
| 87 | + |
| 88 | +2. Test installation: |
| 89 | + ```bash |
| 90 | + pip install --index-url https://test.pypi.org/simple/ netdriver-agent |
| 91 | + ``` |
| 92 | + |
| 93 | +## Step 5: Publish to Production |
| 94 | + |
| 95 | +### Option 1: Manual Publishing |
| 96 | + |
| 97 | +1. Go to **Actions** → **"Publish to PyPI"** |
| 98 | +2. Click **"Run workflow"** |
| 99 | +3. Select: |
| 100 | + - Branch: `master` |
| 101 | + - Environment: **`pypi`** (NOT testpypi!) |
| 102 | + - Projects: **`all`** |
| 103 | +4. Click **"Run workflow"** |
| 104 | + |
| 105 | +### Option 2: Automatic Publishing (via Git Tags) |
| 106 | + |
| 107 | +```bash |
| 108 | +# 1. Update version numbers |
| 109 | +poetry version -P projects/agent 0.3.1 |
| 110 | +poetry version -P projects/simunet 0.3.1 |
| 111 | + |
| 112 | +# 2. Commit changes |
| 113 | +git add projects/*/pyproject.toml |
| 114 | +git commit -m "chore: bump version to 0.3.1" |
| 115 | +git push |
| 116 | + |
| 117 | +# 3. Create and push tag |
| 118 | +git tag v0.3.1 |
| 119 | +git push origin v0.3.1 |
| 120 | +``` |
| 121 | + |
| 122 | +The `release.yml` workflow will automatically: |
| 123 | +- ✅ Create a GitHub Release |
| 124 | +- ✅ Build both packages |
| 125 | +- ✅ Publish to PyPI |
| 126 | +- ✅ Attach wheel files to the release |
| 127 | + |
| 128 | +## Step 6: Verify Publication |
| 129 | + |
| 130 | +1. Check on PyPI: |
| 131 | + - https://pypi.org/project/netdriver-agent/ |
| 132 | + - https://pypi.org/project/netdriver-simunet/ |
| 133 | + |
| 134 | +2. Test installation: |
| 135 | + ```bash |
| 136 | + pip install netdriver-agent |
| 137 | + pip install netdriver-simunet |
| 138 | + ``` |
| 139 | + |
| 140 | +## Common Issues |
| 141 | + |
| 142 | +### "HTTP Error 403: Authentication failed" |
| 143 | + |
| 144 | +**Solution:** Check that GitHub secrets are correctly configured |
| 145 | + |
| 146 | +```bash |
| 147 | +# Verify secrets exist in: Settings → Secrets and variables → Actions |
| 148 | +# Should see: |
| 149 | +# - PYPI_API_TOKEN |
| 150 | +# - TEST_PYPI_API_TOKEN |
| 151 | +``` |
| 152 | + |
| 153 | +### "HTTP Error 400: Bad Request - duplicate keys" |
| 154 | + |
| 155 | +**Solution:** Version already exists on PyPI. Bump the version: |
| 156 | + |
| 157 | +```bash |
| 158 | +poetry version -P projects/agent patch |
| 159 | +poetry version -P projects/simunet patch |
| 160 | +# Then rebuild and publish |
| 161 | +``` |
| 162 | + |
| 163 | +### Workflow fails with "Poetry not found" |
| 164 | + |
| 165 | +**Solution:** |
| 166 | +- If using standard workflow: Check Poetry installation step |
| 167 | +- If using Docker workflow: Build the CI image first |
| 168 | + |
| 169 | +### Package shows as "0 B" or malformed |
| 170 | + |
| 171 | +**Solution:** Check build output - Polylith path warnings are normal, verify wheel contents: |
| 172 | + |
| 173 | +```bash |
| 174 | +unzip -l projects/agent/dist/netdriver_agent-*.whl |
| 175 | +``` |
| 176 | + |
| 177 | +## Best Practices |
| 178 | + |
| 179 | +### Version Management |
| 180 | + |
| 181 | +✅ **DO:** |
| 182 | +- Keep version numbers in sync across `projects/agent/pyproject.toml` and `projects/simunet/pyproject.toml` |
| 183 | +- Use semantic versioning: `MAJOR.MINOR.PATCH` |
| 184 | +- Test on TestPyPI before production |
| 185 | + |
| 186 | +❌ **DON'T:** |
| 187 | +- Publish the same version twice |
| 188 | +- Skip testing on TestPyPI |
| 189 | +- Use local version identifiers for production (e.g., `0.3.0+local`) |
| 190 | + |
| 191 | +### Release Process |
| 192 | + |
| 193 | +1. Develop features on branches |
| 194 | +2. Test locally: `poetry build -P projects/agent` |
| 195 | +3. Create PR and verify build test passes |
| 196 | +4. Merge to master |
| 197 | +5. Test on TestPyPI |
| 198 | +6. Tag and release to production PyPI |
| 199 | + |
| 200 | +### Security |
| 201 | + |
| 202 | +- 🔒 Never commit tokens to repository |
| 203 | +- 🔒 Use scoped tokens when possible |
| 204 | +- 🔒 Rotate tokens regularly |
| 205 | +- 🔒 Use GitHub environments for additional approval gates |
| 206 | + |
| 207 | +## Next Steps |
| 208 | + |
| 209 | +- Set up automated testing before publishing |
| 210 | +- Configure GitHub environments for approval workflows |
| 211 | +- Set up branch protection rules |
| 212 | +- Consider using PyPI Trusted Publishers (no tokens needed!) |
| 213 | + |
| 214 | +## Need Help? |
| 215 | + |
| 216 | +- 📖 Full documentation: `.github/workflows/README.md` |
| 217 | +- 🐛 Report issues: https://github.com/OpenSecFlow/netdriver/issues |
| 218 | +- 📝 PyPI Help: https://pypi.org/help/ |
| 219 | +- 🎯 GitHub Actions: https://docs.github.com/en/actions |
0 commit comments