Skip to content

Commit

Permalink
Add python CI to support automatic release
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-the-hero committed Dec 9, 2024
1 parent b168ace commit 85a4282
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
113 changes: 113 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
name: Python CI/CD with Automatic and Manual Versioning

on:
push:
branches:
- main
workflow_dispatch:
inputs:
version:
description: 'Version to release (e.g., 2024.2.11)'
required: false
default: ''

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout source code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
pip install -r requirements.txt
- name: Run tests
run: |
python -m pip install pytest
pytest
deploy:
runs-on: ubuntu-latest

needs: build

steps:
- name: Checkout source code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Determine Version to Release
id: determine_version
run: |
if [ -z "${{ github.event.inputs.version }}" ]; then
# No version provided, use version.txt
VERSION=$(cat version.txt)
else
# Use the manually provided version
VERSION="${{ github.event.inputs.version }}"
fi
echo "version=$VERSION" >> $GITHUB_ENV
- name: Build distribution
run: |
python setup.py sdist bdist_wheel
- name: Deploy to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}

- name: Tag Release in GitHub
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION=${{ env.version }}
git tag -a "$VERSION" -m "Release $VERSION"
git push origin "$VERSION"
increment-version:
runs-on: ubuntu-latest

needs: deploy

steps:
- name: Checkout source code
uses: actions/checkout@v3

- name: Increment Version
id: increment_version
run: |
VERSION=${{ env.version }}
YEAR=$(date +%Y)
RELEASE=$(echo $VERSION | cut -d '.' -f 2)
# Increment the release version after deployment
NEW_VERSION="$YEAR.$((RELEASE + 1)).1"
echo $NEW_VERSION > version.txt
echo "new_version=$NEW_VERSION" >> $GITHUB_ENV
sed -i "s/version=.*,/version='$NEW_VERSION',/" setup.py
- name: Commit and Push Incremented Version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
git add version.txt setup.py
git commit -m "Increment version to ${{ env.new_version }}"
git push origin main
34 changes: 34 additions & 0 deletions .github/workflows/python-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Test Pull Request

on:
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout source code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
python -m pip install pytest
pytest
- name: Report Test Results
if: failure()
run: |
echo "::error::Some tests failed. Check the logs for details."
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
with open("README.md", "r") as f:
long_description = f.read()

with open("version.txt", "r") as f:
version = f.read()

setup(
name="watergate_local_api", # Replace with your own username
version="2024.2.1",
version=version,
author="Watergate",
author_email="[email protected]",
description="Python package to interact with the Watergate Local API.",
Expand Down
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024.3.1

0 comments on commit 85a4282

Please sign in to comment.