Repository navigation
ci: publish package to PyPI on release tags #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
01cb145
ci: publish package to PyPI on release tags
JAugusto42 0559e14
ci: gate PyPI publishing behind protected environment
JAugusto42 2cb2779
ci: restrict PyPI releases to repository maintainers
JAugusto42 c9294c4
ci: authorize both tag pusher and re-run initiator
JAugusto42 6f07835
ci: smoke test built wheel before publishing
JAugusto42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| name: Publish to PyPI | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - "v[0-9]+.[0-9]+.[0-9]+" | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: publish-pypi | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| authorize: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Ensure tag pusher and run initiator have admin or maintain role | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| ACTOR: ${{ github.actor }} | ||
| TRIGGERING_ACTOR: ${{ github.triggering_actor }} | ||
| run: | | ||
| # actor is who pushed the tag; triggering_actor is who started this run (differs on re-runs). | ||
| for USER in "$ACTOR" "$TRIGGERING_ACTOR"; do | ||
| ROLE="$(gh api "repos/${GITHUB_REPOSITORY}/collaborators/${USER}/permission" --jq '.role_name' || echo none)" | ||
| case "$ROLE" in | ||
| admin|maintain) | ||
| echo "$USER is allowed to release (role: $ROLE)." | ||
| ;; | ||
| *) | ||
| echo "$USER is not allowed to release (role: $ROLE)." | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| tests: | ||
| needs: authorize | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Set up Python 3.10 | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.10" | ||
| - name: Install package and test dependencies | ||
| run: pip install . pytest | ||
| - name: Run tests | ||
| run: pytest -q | ||
|
|
||
| version-check: | ||
| needs: authorize | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Ensure tag matches VERSION file | ||
| run: | | ||
| TAG="${GITHUB_REF_NAME#v}" | ||
| FILE_VERSION="$(tr -d '[:space:]' < src/conviso/VERSION)" | ||
| if [ "$TAG" != "$FILE_VERSION" ]; then | ||
| echo "Tag ($GITHUB_REF_NAME) does not match src/conviso/VERSION ($FILE_VERSION)." | ||
| exit 1 | ||
| fi | ||
| echo "Tag matches VERSION ($FILE_VERSION)." | ||
|
|
||
| build-and-publish-pypi: | ||
| needs: | ||
|
JAugusto42 marked this conversation as resolved.
Resolved
|
||
| - tests | ||
| - version-check | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Set up Python 3.10 | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.10" | ||
| - name: Install build tools | ||
| run: pip install build twine | ||
| - name: Build package | ||
| run: python -m build | ||
| - name: Check distribution | ||
| run: twine check dist/* | ||
| - name: Smoke test built wheel | ||
| run: | | ||
| python -m venv /tmp/wheel-venv | ||
| /tmp/wheel-venv/bin/pip install dist/*.whl | ||
| cd /tmp | ||
| /tmp/wheel-venv/bin/conviso --version | ||
| /tmp/wheel-venv/bin/python -c " | ||
| from pathlib import Path | ||
| import conviso | ||
| pkg = Path(next(iter(conviso.__path__))) | ||
| for name in ('VERSION', 'security_gate_rules_schema.json'): | ||
| assert (pkg / name).is_file(), f'{name} missing from wheel' | ||
| " | ||
| - name: Publish to PyPI | ||
| env: | ||
| TWINE_USERNAME: __token__ | ||
| TWINE_PASSWORD: ${{ secrets.SRE_PYPI_TOKEN }} | ||
| run: twine upload dist/* | ||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.