Skip to content

Commit e2b31bb

Browse files
authored
Merge pull request #13 from Zipstack/feat/release-workflow-with-version-bump
feat: release workflow with automated version bump and publish
2 parents 6e3816a + 616207b commit e2b31bb

File tree

1 file changed

+158
-11
lines changed

1 file changed

+158
-11
lines changed

.github/workflows/main.yml

Lines changed: 158 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,177 @@
1-
name: Publish Python Package
1+
name: Release Tag and Publish Package
22

33
on:
4-
release:
5-
types:
6-
- published
4+
# Manual trigger with version bump input
5+
workflow_dispatch:
6+
inputs:
7+
version_bump:
8+
description: "Version bump type"
9+
required: true
10+
default: "patch"
11+
type: choice
12+
options:
13+
- patch
14+
- minor
15+
- major
16+
pre_release:
17+
description: "Create as pre-release"
18+
required: false
19+
default: false
20+
type: boolean
21+
release_notes:
22+
description: "Release notes (optional)"
23+
required: false
24+
type: string
725

826
jobs:
9-
pypi-publish:
10-
name: upload release to PyPI
27+
release-and-publish:
1128
runs-on: ubuntu-latest
1229
permissions:
13-
contents: read
30+
contents: write
1431
id-token: write
1532
steps:
33+
- name: Generate GitHub App Token
34+
id: generate-token
35+
uses: actions/create-github-app-token@v2
36+
with:
37+
app-id: ${{ vars.PUSH_TO_MAIN_APP_ID }}
38+
private-key: ${{ secrets.PUSH_TO_MAIN_APP_PRIVATE_KEY }}
39+
owner: Zipstack
40+
repositories: |
41+
unstract-python-client
42+
1643
- uses: actions/checkout@v4
1744
with:
18-
ref: ${{ github.event.release.tag_name }}
45+
token: ${{ steps.generate-token.outputs.token }}
46+
fetch-depth: 0
1947

20-
- name: Install uv
21-
uses: astral-sh/setup-uv@v7
48+
# Configure git for commits
49+
- name: Configure Git
50+
run: |
51+
git config --global user.name "github-actions[bot]"
52+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
53+
54+
# Setup Python
55+
- uses: actions/setup-python@v5
2256
with:
2357
python-version: "3.12"
58+
59+
# Install uv
60+
- name: Install uv
61+
uses: astral-sh/setup-uv@v6
62+
with:
63+
version: "0.6.14"
2464
enable-cache: true
2565

66+
# Install dependencies
67+
- name: Install dependencies
68+
run: uv sync --dev
69+
70+
# Handle workflow_dispatch (manual trigger)
71+
- name: Bump version and create release
72+
if: github.event_name == 'workflow_dispatch'
73+
id: create_release
74+
run: |
75+
# Get current version from __init__.py using grep/sed (avoid importing)
76+
CURRENT_VERSION=$(grep -E "^__version__ = " src/unstract/api_deployments/__init__.py | sed -E 's/__version__ = "(.*)"/\1/')
77+
echo "Current version: $CURRENT_VERSION"
78+
79+
# Calculate new version based on input
80+
IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
81+
MAJOR=${VERSION_PARTS[0]}
82+
MINOR=${VERSION_PARTS[1]}
83+
PATCH=${VERSION_PARTS[2]}
84+
85+
case "${{ github.event.inputs.version_bump }}" in
86+
"major")
87+
MAJOR=$((MAJOR + 1))
88+
MINOR=0
89+
PATCH=0
90+
;;
91+
"minor")
92+
MINOR=$((MINOR + 1))
93+
PATCH=0
94+
;;
95+
"patch")
96+
PATCH=$((PATCH + 1))
97+
;;
98+
esac
99+
100+
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
101+
echo "New version: $NEW_VERSION"
102+
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
103+
104+
# Update version in __init__.py
105+
sed -i "s/__version__ = \"$CURRENT_VERSION\"/__version__ = \"$NEW_VERSION\"/" src/unstract/api_deployments/__init__.py
106+
107+
# Commit version changes
108+
git add src/unstract/api_deployments/__init__.py
109+
git commit -m "chore: bump version to $NEW_VERSION [skip ci]"
110+
git push origin main
111+
112+
# Create git tag
113+
git tag "v$NEW_VERSION"
114+
git push origin "v$NEW_VERSION"
115+
116+
# Create GitHub release
117+
RELEASE_NOTES="${{ github.event.inputs.release_notes }}"
118+
if [ -z "$RELEASE_NOTES" ]; then
119+
gh release create "v$NEW_VERSION" \
120+
--title "Release v$NEW_VERSION" \
121+
--generate-notes \
122+
${{ github.event.inputs.pre_release == 'true' && '--prerelease' || '' }}
123+
else
124+
gh release create "v$NEW_VERSION" \
125+
--title "Release v$NEW_VERSION" \
126+
--notes "$RELEASE_NOTES" \
127+
--generate-notes \
128+
${{ github.event.inputs.pre_release == 'true' && '--prerelease' || '' }}
129+
fi
130+
131+
echo "Created release v$NEW_VERSION"
132+
env:
133+
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
134+
135+
# Set version for subsequent steps
136+
- name: Set version output
137+
id: version
138+
run: |
139+
echo "version=${{ steps.create_release.outputs.version }}" >> $GITHUB_OUTPUT
140+
141+
# Verify the version was updated correctly
142+
- name: Verify version update
143+
run: |
144+
PACKAGE_VERSION=$(grep -E "^__version__ = " src/unstract/api_deployments/__init__.py | sed -E 's/__version__ = "(.*)"/\1/')
145+
echo "Package version: $PACKAGE_VERSION"
146+
echo "Target version: ${{ steps.version.outputs.version }}"
147+
if [ "$PACKAGE_VERSION" != "${{ steps.version.outputs.version }}" ]; then
148+
echo "Version mismatch! Exiting..."
149+
exit 1
150+
fi
151+
152+
# Create test environment
153+
- name: Create test env
154+
run: cp tests/sample.env tests/.env
155+
156+
# Run linting
157+
- name: Run linting
158+
run: uv run ruff check src/
159+
160+
# Run tests
161+
- name: Run tests
162+
run: uv run pytest tests/
163+
164+
# Build the package
26165
- name: Build package
27166
run: uv build
28167

29-
- name: Publish package distributions to PyPI
168+
# Publish to PyPI using Trusted Publishers
169+
- name: Publish to PyPI
30170
run: uv publish
171+
172+
# Output success message
173+
- name: Success message
174+
run: |
175+
echo "Successfully published version ${{ steps.version.outputs.version }} to PyPI using uv publish with Trusted Publishers"
176+
echo "Release: https://github.com/${{ github.repository }}/releases/tag/v${{ steps.version.outputs.version }}"
177+
echo "PyPI: https://pypi.org/project/unstract-client/${{ steps.version.outputs.version }}/"

0 commit comments

Comments
 (0)