Skip to content

Release automatically when the version changes #122

Release automatically when the version changes

Release automatically when the version changes #122

name: Test and Publish ipdata to PyPI
on:
push:
branches:
- master
tags:
- '*'
pull_request:
jobs:
test:
name: Test (Python ${{ matrix.python-version }})
# ubuntu-latest now resolves to Ubuntu 26.04, for which actions/setup-python has no Python 3.9 builds
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
steps:
- uses: actions/checkout@v7
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v7
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
- name: Test with pytest (offline tests only, no API key needed)
run: |
python -m pytest -m "not api"
release:
name: Tag and create the GitHub release
# Only pushes to the default branch can release; a version that is already tagged is left alone.
if: github.event_name == 'push' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
needs: test
runs-on: ubuntu-24.04
permissions:
contents: write
concurrency:
group: release
cancel-in-progress: false
outputs:
tag: ${{ steps.version.outputs.tag }}
created: ${{ steps.release.outputs.created }}
steps:
- uses: actions/checkout@v7
- name: Read the version from setup.cfg
id: version
run: |
tag="$(sed -nE 's/^version *= *([^[:space:]]+).*/\1/p' setup.cfg | head -1)"
if [ -z "$tag" ]; then
echo "::error file=setup.cfg::no version found in setup.cfg"
exit 1
fi
echo "version $tag"
echo "tag=$tag" >> "$GITHUB_OUTPUT"
- name: Create the tag and the release unless the tag exists
id: release
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ steps.version.outputs.tag }}
run: |
status=0
git ls-remote --exit-code --tags origin "refs/tags/$TAG" > /dev/null || status=$?
case "$status" in
0)
echo "tag $TAG already exists, nothing to release"
echo "created=false" >> "$GITHUB_OUTPUT"
;;
2)
gh release create "$TAG" --target "$GITHUB_SHA" --title "$TAG" --generate-notes
echo "created=true" >> "$GITHUB_OUTPUT"
;;
*)
echo "::error::git ls-remote failed with exit code $status"
exit "$status"
;;
esac
build-n-publish:
name: Build and publish to PyPI
needs: [test, release]
# Runs for every push as before; the release job is skipped for tag pushes, hence the explicit result checks.
if: >-
!cancelled() && github.event_name == 'push' && needs.test.result == 'success'
&& (needs.release.result == 'success' || needs.release.result == 'skipped')
# kept on the same image as the test job
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- name: Set up Python 3.13
uses: actions/setup-python@v7
with:
python-version: "3.13"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install python-dotenv
if [ -f requirements.txt ]; then python -m pip install -r requirements.txt; fi
- name: Test with pytest
run: |
python -m pytest
env:
IPDATA_API_KEY: ${{ secrets.IPDATA_API_KEY }}
- name: Install build
run: python -m pip install build
- name: Build a binary wheel and a source tarball
run: >-
python -m
build
.
- name: Publish to PyPI
# a tag created by the release job in this run, or a tag pushed by hand
if: needs.release.outputs.created == 'true' || startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_PASSWORD }}