Skip to content

Merge pull request #422 from OpenPecha/refactor-toolkit #152

Merge pull request #422 from OpenPecha/refactor-toolkit

Merge pull request #422 from OpenPecha/refactor-toolkit #152

Workflow file for this run

name: Publish
on:
push:
branches:
- main
jobs:
check_commit:
runs-on: ubuntu-latest
outputs:
should_release: ${{ steps.check_message.outputs.should_release }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Check commit message
id: check_message
run: |
# Get the latest commit message
COMMIT_MSG=$(git log -1 --pretty=%B)
# Define the pattern for semantic version commits
# This checks for conventional commits format: fix:, feat:, BREAKING CHANGE:, etc.
if echo "$COMMIT_MSG" | grep -E "^(fix|feat|chore|docs|style|refactor|perf|test|build|ci|revert)(\([a-z]+\))?!?: .+" || echo "$COMMIT_MSG" | grep -E "BREAKING CHANGE:"; then
echo "Commit message matches release pattern"
echo "should_release=true" >> $GITHUB_OUTPUT
else
echo "Commit message does not match release pattern"
echo "should_release=false" >> $GITHUB_OUTPUT
fi
build:
runs-on: ubuntu-latest
needs: check_commit
if: needs.check_commit.outputs.should_release == 'true'
strategy:
max-parallel: 4
matrix:
python-version: ["3.10"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e ".[github]"
pip install ".[dev]"
- name: Run Test
env:
GITHUB_TOKEN: ${{ secrets.ADMIN_GITHUB_TOKEN }}
PYTHONPATH: ${GITHUB_WORKSPACE}
run: |
pytest
publish:
needs: [check_commit, build]
if: needs.check_commit.outputs.should_release == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.ADMIN_GITHUB_TOKEN }}
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Verify PyPI Token Presence
run: |
if [ -z "${{ secrets.PYPI_TOKEN }}" ]; then
echo "WARNING: PYPI_TOKEN is not set!"
exit 1
else
echo "PYPI_TOKEN is set. Will be used for publishing."
fi
# Check if a new version should be released using GitHub Action
- name: Python Semantic Release
id: release
uses: python-semantic-release/python-semantic-release@v9.8.9
with:
github_token: ${{ secrets.ADMIN_GITHUB_TOKEN }}
directory: "."
root_options: "--noop"
# Get version information
- name: Get version info
id: version_check
run: |
# Get current version from PyPI
PYPI_VERSION=$(pip index versions openpecha 2>/dev/null | grep -oP '(?<=\().*(?=\))' | cut -d ',' -f1 || echo "0.0.0")
echo "Current PyPI version: $PYPI_VERSION"
# Get current version from pyproject.toml
CURRENT_VERSION=$(grep 'version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/')
echo "Current project version: $CURRENT_VERSION"
# Check if semantic-release would make a new release
if [ "${{ steps.release.outputs.released }}" == "true" ]; then
NEW_VERSION="${{ steps.release.outputs.version }}"
echo "New version will be: $NEW_VERSION"
echo "should_upload=true" >> $GITHUB_OUTPUT
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
else
echo "No version change needed. Current: $CURRENT_VERSION"
echo "should_upload=false" >> $GITHUB_OUTPUT
echo "new_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
fi
# Actually run semantic-release to update version (without noop)
- name: Run semantic-release version
if: steps.version_check.outputs.should_upload == 'true'
uses: python-semantic-release/python-semantic-release@v9.8.9
with:
github_token: ${{ secrets.ADMIN_GITHUB_TOKEN }}
directory: "."
- name: Build and Publish Package
if: steps.version_check.outputs.should_upload == 'true'
run: |
# Install build tools
pip install build twine
# Build the package using a temp directory approach
TEMP_DIR=$(mktemp -d)
cp -r . $TEMP_DIR/
cd $TEMP_DIR
# Build the package in the temp directory
python -m build
echo "Built package files:"
ls -la dist/
# Upload to PyPI
echo "Uploading to PyPI..."
python -m twine upload dist/* --verbose --non-interactive -u __token__ -p ${{ secrets.PYPI_TOKEN }}
- name: Skip upload due to unchanged version
if: steps.version_check.outputs.should_upload == 'false'
run: |
echo "Skipping PyPI upload because the version was not updated."
echo "Check your semantic-release configuration and make sure version files are being updated properly."