Skip to content

[UPDATE] version retrieval #9

[UPDATE] version retrieval

[UPDATE] version retrieval #9

name: build and publish
on:
push:
branches:
- '**'
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry requests packaging
- name: Fetch the package name
id: package_name
run: |
PACKAGE_NAME=$(poetry version --short)
echo "PACKAGE_NAME=$PACKAGE_NAME" >> $GITHUB_ENV
- name: Fetch the latest version from PyPI
id: pypi
run: |
echo "Fetching latest version from PyPI for $PACKAGE_NAME"
LATEST_VERSION=$(python -c "import requests, sys; from packaging import version; response = requests.get(f'https://pypi.org/pypi/{os.getenv('PACKAGE_NAME')}/json'); data = response.json(); print(data['info']['version']) if 'info' in data else sys.exit(1)")
echo "LATEST_VERSION=$LATEST_VERSION" >> $GITHUB_ENV
- name: Get the current version from pyproject.toml
id: current_version
run: |
CURRENT_VERSION=$(poetry version --short)
echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV
- name: Compare versions
id: compare_versions
run: |
python -c "import os; from packaging import version; current = version.parse(os.getenv('CURRENT_VERSION')); latest = version.parse(os.getenv('LATEST_VERSION')); exit(0) if current > latest else exit(1)"
- name: Build package
run: poetry build
- name: Publish package
if: github.ref == 'refs/heads/main' && steps.compare_versions.outcome == 'success'
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}