Merge pull request #81 from DataFog/chore/pypi #17
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
name: PyPI Release | |
on: | |
# Manual trigger with version input | |
workflow_dispatch: | |
inputs: | |
version: | |
description: "Version to release (e.g., 1.2.3)" | |
required: true | |
confirm_tests: | |
description: "Confirm all tests have passed" | |
type: boolean | |
required: true | |
is_prerelease: | |
description: "Is this a pre-release?" | |
type: boolean | |
default: false | |
required: false | |
# Auto-trigger for beta releases when merged to dev | |
push: | |
branches: | |
- dev | |
jobs: | |
# Job for manual releases (stable or pre-release) | |
manual_release: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'workflow_dispatch' && github.event.inputs.confirm_tests == 'true' | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.10" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install build twine | |
- name: Build package | |
run: python -m build | |
- name: Create GitHub Release | |
env: | |
GITHUB_TOKEN: ${{ secrets.pypi }} | |
run: | | |
git config user.name github-actions | |
git config user.email [email protected] | |
git tag v${{ github.event.inputs.version }} | |
git push origin v${{ github.event.inputs.version }} | |
if [ "${{ github.event.inputs.is_prerelease }}" == "true" ]; then | |
gh release create v${{ github.event.inputs.version }} --prerelease --generate-notes | |
else | |
gh release create v${{ github.event.inputs.version }} --generate-notes | |
fi | |
- name: Publish to PyPI | |
env: | |
TWINE_USERNAME: __token__ | |
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
run: twine upload dist/* | |
# Job for automatic beta releases on merge to dev | |
auto_beta_release: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'push' && github.ref == 'refs/heads/dev' | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.10" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install build twine setuptools-scm | |
- name: Generate beta version | |
id: beta_version | |
run: | | |
# Read current version from setup.py | |
CURRENT_VERSION=$(grep -o '__version__ = "[^"]*"' setup.py | sed 's/__version__ = "\(.*\)"/\1/') | |
echo "Current version in files: $CURRENT_VERSION" | |
# Split version into components | |
IFS='.' read -r MAJOR MINOR PATCH_FULL <<< "$CURRENT_VERSION" || true | |
# Validate we got valid version components | |
if [[ -z "$MAJOR" || -z "$MINOR" || -z "$PATCH_FULL" ]]; then | |
echo "Error: Could not parse version components from $CURRENT_VERSION" | |
echo "Using default version 0.0.1b1" | |
MAJOR=0 | |
MINOR=0 | |
PATCH_FULL=1 | |
fi | |
# Handle beta suffix if it exists | |
if [[ $PATCH_FULL == *b* ]]; then | |
# Extract the numeric part before 'b' | |
PATCH_NUM=${PATCH_FULL%%b*} | |
# Extract the beta number and increment it | |
BETA_NUM=${PATCH_FULL#*b} | |
# Ensure beta number is a valid integer | |
if ! [[ $BETA_NUM =~ ^[0-9]+$ ]]; then | |
echo "Warning: Invalid beta number format. Resetting to beta1." | |
BETA_NUM=1 | |
else | |
BETA_NUM=$((BETA_NUM + 1)) | |
fi | |
else | |
# If not already a beta, use the patch number and start with beta1 | |
PATCH_NUM=$PATCH_FULL | |
BETA_NUM=1 | |
fi | |
# Generate new beta version | |
BETA_VERSION="$MAJOR.$MINOR.${PATCH_NUM}b$BETA_NUM" | |
echo "Generated beta version: $BETA_VERSION" | |
echo "version=$BETA_VERSION" >> $GITHUB_OUTPUT | |
# Update version in setup.py | |
sed -i "s/__version__ = \"[^\"]*\"/__version__ = \"$BETA_VERSION\"/g" setup.py | |
# Update version in __about__.py if it exists | |
if [ -f "datafog/__about__.py" ]; then | |
sed -i "s/__version__ = \"[^\"]*\"/__version__ = \"$BETA_VERSION\"/g" datafog/__about__.py | |
fi | |
- name: Build package | |
run: python -m build | |
- name: Create GitHub Pre-Release | |
env: | |
GITHUB_TOKEN: ${{ secrets.pypi }} | |
BETA_VERSION: ${{ steps.beta_version.outputs.version }} | |
run: | | |
git config user.name github-actions | |
git config user.email [email protected] | |
# Commit the version changes | |
git add setup.py datafog/__about__.py | |
git commit -m "Bump version to $BETA_VERSION [skip ci]" | |
# Create and push tag | |
git tag v$BETA_VERSION | |
# Push both the commit and the tag | |
git push origin HEAD:dev | |
git push origin v$BETA_VERSION | |
# Create GitHub release | |
gh release create v$BETA_VERSION --prerelease --title "Beta Release v$BETA_VERSION" --notes "Automated beta release from dev branch" | |
- name: Publish to PyPI as Beta | |
env: | |
TWINE_USERNAME: __token__ | |
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
BETA_VERSION: ${{ steps.beta_version.outputs.version }} | |
run: | | |
# Ensure package is marked as beta in PyPI | |
twine upload --skip-existing dist/* |