Skip to content

Merge pull request #10 from APIForge-Organisation/dev #12

Merge pull request #10 from APIForge-Organisation/dev

Merge pull request #10 from APIForge-Organisation/dev #12

Workflow file for this run

name: Release
on:
push:
branches: [main]
jobs:
test:
name: Test before release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- name: Install dependencies
run: pip install -e ".[dev]"
- name: Run tests
run: python -m pytest tests/ -v
publish:
name: Publish to PyPI
runs-on: ubuntu-latest
needs: test
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- name: Install build tools
run: pip install build twine
- name: Check if version is already published
id: version_check
run: |
PKG_VERSION=$(python -c "import tomllib; f=open('pyproject.toml','rb'); d=tomllib.load(f); print(d['project']['version'])")
PYPI_VERSION=$(pip index versions apiforgepy 2>/dev/null | grep -oP '(?<=apiforgepy \()[\d.]+(?=\))' | head -1 || echo "none")
echo "pkg_version=$PKG_VERSION" >> $GITHUB_OUTPUT
echo "pypi_version=$PYPI_VERSION" >> $GITHUB_OUTPUT
if [ "$PKG_VERSION" != "$PYPI_VERSION" ]; then
echo "should_publish=true" >> $GITHUB_OUTPUT
echo "New version detected: $PKG_VERSION (PyPI has $PYPI_VERSION)"
else
echo "should_publish=false" >> $GITHUB_OUTPUT
echo "Version $PKG_VERSION is already on PyPI — skipping publish."
fi
- name: Build package
if: steps.version_check.outputs.should_publish == 'true'
run: python -m build
- name: Publish to PyPI
if: steps.version_check.outputs.should_publish == 'true'
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: twine upload dist/*
- name: Create GitHub Release
if: steps.version_check.outputs.should_publish == 'true'
uses: actions/github-script@v7
with:
script: |
const version = '${{ steps.version_check.outputs.pkg_version }}';
await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: `v${version}`,
name: `v${version}`,
body: `## apiforgepy v${version}\n\nSee [CHANGELOG.md](https://github.com/APIForge-Organisation/sdk-python/blob/main/CHANGELOG.md) for details.\n\n\`\`\`bash\npip install apiforgepy==${version}\n\`\`\``,
draft: false,
prerelease: version.startsWith('0.'),
});