Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit c3a21e5

Browse files
committed
ci: add GitHub workflow for PyPI publishing and releases
Add a new workflow that automatically publishes packages to PyPI and creates GitHub releases when commits to main branch contain version tags. The workflow includes version verification, package building, PyPI upload, and release creation with changelog.
1 parent 6f01334 commit c3a21e5

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

.github/workflows/release.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Publish to PyPI and GitHub Release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
publish:
13+
name: Build, publish and release
14+
if: contains(github.event.head_commit.message, 'new_version:')
15+
runs-on: ubuntu-24.04
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Set up Python
21+
uses: actions/setup-python@v5
22+
with:
23+
python-version: '3.11'
24+
25+
- name: Install build tools
26+
run: |
27+
python -m pip install --upgrade pip
28+
python -m pip install build twine
29+
30+
- name: Extract version and release notes from commit message
31+
id: extract
32+
run: |
33+
COMMIT_MSG="${{ github.event.head_commit.message }}"
34+
VERSION=$(printf "%s" "$COMMIT_MSG" | sed -n 's/.*new_version:[[:space:]]*\(v[0-9][0-9\.]*\).*/\1/p' | head -n1)
35+
if [ -z "$VERSION" ]; then
36+
echo "No version marker found in commit message." >&2
37+
exit 1
38+
fi
39+
NOTES=$(printf "%s" "$COMMIT_MSG" | sed -n '1!p')
40+
RELEASE_BODY="$NOTES\n\nFull Changelog: https://github.com/${{ github.repository }}/commits/$VERSION"
41+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
42+
printf "release_body<<EOF\n%s\nEOF\n" "$RELEASE_BODY" >> "$GITHUB_OUTPUT"
43+
44+
- name: Verify package version matches commit version
45+
run: |
46+
PKG_VERSION=$(python - <<'PY'
47+
import configparser
48+
c = configparser.ConfigParser()
49+
c.read('setup.cfg')
50+
print(c['metadata']['version'])
51+
PY
52+
)
53+
COMMIT_VERSION="${{ steps.extract.outputs.version }}"
54+
STRIPPED="${COMMIT_VERSION#v}"
55+
echo "Package version: $PKG_VERSION"
56+
echo "Commit version: $STRIPPED"
57+
if [ "$PKG_VERSION" != "$STRIPPED" ]; then
58+
echo "Version mismatch: setup.cfg=$PKG_VERSION commit=$STRIPPED" >&2
59+
exit 1
60+
fi
61+
62+
- name: Build package
63+
run: |
64+
python -m build
65+
66+
- name: Publish to PyPI
67+
env:
68+
TWINE_USERNAME: __token__
69+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
70+
run: |
71+
python -m twine upload --non-interactive --skip-existing dist/*
72+
73+
- name: Create and push tag
74+
run: |
75+
VERSION="${{ steps.extract.outputs.version }}"
76+
git config user.name "github-actions"
77+
git config user.email "github-actions@github.com"
78+
git tag -a "$VERSION" -m "Release $VERSION"
79+
git push origin "$VERSION"
80+
81+
- name: Create GitHub Release
82+
uses: actions/create-release@v1
83+
env:
84+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
85+
with:
86+
tag_name: ${{ steps.extract.outputs.version }}
87+
release_name: MainyDB ${{ steps.extract.outputs.version }}
88+
body: ${{ steps.extract.outputs.release_body }}
89+
draft: false
90+
prerelease: false

0 commit comments

Comments
 (0)