Skip to content

Release Tag and Publish Package #1

Release Tag and Publish Package

Release Tag and Publish Package #1

Workflow file for this run

name: Release Tag and Publish Package
on:
# Manual trigger with version bump input
workflow_dispatch:
inputs:
version_bump:
description: "Version bump type"
required: true
default: "patch"
type: choice
options:
- patch
- minor
- major
pre_release:
description: "Create as pre-release"
required: false
default: false
type: boolean
release_notes:
description: "Release notes (optional)"
required: false
type: string
jobs:
release-and-publish:
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- name: Generate GitHub App Token
id: generate-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ vars.PUSH_TO_MAIN_APP_ID }}
private-key: ${{ secrets.PUSH_TO_MAIN_APP_PRIVATE_KEY }}
owner: Unstract
repositories: |
apihub-python-client
- uses: actions/checkout@v4
with:
token: ${{ steps.generate-token.outputs.token }}
fetch-depth: 0
# Configure git for commits
- name: Configure Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# Setup Python
- uses: actions/setup-python@v5
with:
python-version: "3.12"
# Install uv
- name: Install uv
uses: astral-sh/setup-uv@v6
with:
version: "0.6.14"
enable-cache: true
# Install dependencies
- name: Install dependencies
run: uv sync --dev
# Handle workflow_dispatch (manual trigger)
- name: Bump version and create release
if: github.event_name == 'workflow_dispatch'
id: create_release
run: |
# Get current version from __init__.py
CURRENT_VERSION=$(python -c "import sys; sys.path.insert(0, 'src'); from apihub_client import __version__; print(__version__)")
echo "Current version: $CURRENT_VERSION"
# Calculate new version based on input
IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
MAJOR=${VERSION_PARTS[0]}
MINOR=${VERSION_PARTS[1]}
PATCH=${VERSION_PARTS[2]}
case "${{ github.event.inputs.version_bump }}" in
"major")
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
;;
"minor")
MINOR=$((MINOR + 1))
PATCH=0
;;
"patch")
PATCH=$((PATCH + 1))
;;
esac
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
echo "New version: $NEW_VERSION"
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
# Update version in __init__.py
sed -i "s/__version__ = \"$CURRENT_VERSION\"/__version__ = \"$NEW_VERSION\"/" src/apihub_client/__init__.py
# Commit version changes
git add src/apihub_client/__init__.py
git commit -m "chore: bump version to $NEW_VERSION [skip ci]"
git push origin main
# Create git tag
git tag "v$NEW_VERSION"
git push origin "v$NEW_VERSION"
# Create GitHub release
RELEASE_NOTES="${{ github.event.inputs.release_notes }}"
if [ -z "$RELEASE_NOTES" ]; then
gh release create "v$NEW_VERSION" \
--title "Release v$NEW_VERSION" \
--generate-notes \
${{ github.event.inputs.pre_release == 'true' && '--prerelease' || '' }}
else
gh release create "v$NEW_VERSION" \
--title "Release v$NEW_VERSION" \
--notes "$RELEASE_NOTES" \
--generate-notes \
${{ github.event.inputs.pre_release == 'true' && '--prerelease' || '' }}
fi
echo "Created release v$NEW_VERSION"
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
# Set version for subsequent steps
- name: Set version output
id: version
run: |
echo "version=${{ steps.create_release.outputs.version }}" >> $GITHUB_OUTPUT
# Verify the version was updated correctly
- name: Verify version update
run: |
PACKAGE_VERSION=$(python -c "import sys; sys.path.insert(0, 'src'); from apihub_client import __version__; print(__version__)")
echo "Package version: $PACKAGE_VERSION"
echo "Target version: ${{ steps.version.outputs.version }}"
if [ "$PACKAGE_VERSION" != "${{ steps.version.outputs.version }}" ]; then
echo "Version mismatch! Exiting..."
exit 1
fi
# Install tox for testing
- name: Install tox with UV
run: uv tool install tox
# Create test environment
- name: Create test env
shell: bash
run: |
if [ -f ".env.example" ]; then
cp .env.example .env
sed -i "s|API_KEY=your_api_key_here|API_KEY=test_api_key|" .env
sed -i "s|BASE_URL=https://api.example.com|BASE_URL=https://api.test.com|" .env
else
echo "API_KEY=test_api_key" > .env
echo "BASE_URL=https://api.test.com" >> .env
fi
# Run linting
- name: Run linting
run: tox -e lint
# Run tests
- name: Run tests
run: tox -e py312
# Build the package
- name: Build package
run: uv build
# Publish to PyPI using Trusted Publishers
- name: Publish to PyPI
run: uv publish
# Output success message
- name: Success message
run: |
echo "✅ Successfully published version ${{ steps.version.outputs.version }} to PyPI using uv publish with Trusted Publishers"
echo "🚀 Release: https://github.com/${{ github.repository }}/releases/tag/v${{ steps.version.outputs.version }}"
echo "📦 PyPI: https://pypi.org/project/apihub-python-client/${{ steps.version.outputs.version }}/"