From 4f67fae0ba679c371e6e54d970679cecb80760ff Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Tue, 3 Dec 2024 18:38:41 +0000 Subject: [PATCH] CI: Publish workflow (PyPI) (#83) * Use static version * Fix version in docs * Add check-release-tag jobs --- .github/workflows/publish.yml | 110 ++++++++++++++++++++++++++++++++++ aiida_test_cache/__init__.py | 2 - docs/source/conf.py | 5 +- pyproject.toml | 2 +- 4 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..f52d085 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,110 @@ +# WARNING: This file should not be renamed! +# It's name is tied to Trusted Publishing on PyPI. +# +# Deploy to PyPI when a release tag vX.Y.Z is created. +# Will only be published to PyPI if the git tag matches the released version. +# Additionally, creating a "test-release" label on a PR will trigger a publish to TestPyPI. +name: Publish + +on: + push: + tags: + - "v*" + pull_request: + types: [labeled] + +env: + FORCE_COLOR: 1 + +jobs: + + check-release-tag: + name: Check tag version + if: >- + github.repository == 'aiidateam/aiida-test-cache' && + github.event_name == 'push' && + github.ref_type == 'tag' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Check version tag + run: | + ver=$(python -c "from tomllib import load;p=load(open('pyproject.toml','rb'));print(p['project']['version'])") + errormsg="tag version '${{ github.ref_name }}' != 'v$ver' specified in pyproject.toml" + if [ "v${ver}" != "${{ github.ref_name }}" ]; then echo "$errormsg"; exit 1; fi + + dist: + name: Distribution build + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: hynek/build-and-inspect-python-package@v2 + + + test-publish: + needs: [dist] + name: Publish to TestPyPI + if: >- + github.repository == 'aiidateam/aiida-test-cache' && + github.event_name == 'pull_request' && + github.event.action == 'labeled' && + github.event.label.name == 'test-release' + environment: + name: testpypi + url: https://test.pypi.org/p/aiida-test-cache/ + permissions: + id-token: write + pull-requests: write + runs-on: ubuntu-latest + + steps: + - uses: actions/download-artifact@v4 + with: + name: Packages + path: dist + + - uses: pypa/gh-action-pypi-publish@release/v1 + with: + repository-url: https://test.pypi.org/legacy/ + + # Remove the PR label which triggered this release, + # but only if the release failed. Presumably, in this case we might want + # to fix whatever caused the failure and re-trigger the test release. + # If the release succeeded, re-triggering the release would fail anyway, + # unless the version would be bumped again. + - name: Remove test-release label + if: failure() + uses: actions-ecosystem/action-remove-labels@v1 + with: + labels: test-release + + + pypi-publish: + name: Publish release to PyPI + needs: [dist, check-release-tag] + if: >- + github.repository == 'aiidateam/aiida-test-cache' && + github.event_name == 'push' && + github.ref_type == 'tag' + runs-on: ubuntu-latest + environment: + name: pypi + url: https://pypi.org/p/aiida-test-cache/ + permissions: + id-token: write + + steps: + - uses: actions/download-artifact@v4 + with: + name: Packages + path: dist + + - uses: pypa/gh-action-pypi-publish@release/v1 diff --git a/aiida_test_cache/__init__.py b/aiida_test_cache/__init__.py index 62447cb..04788a9 100644 --- a/aiida_test_cache/__init__.py +++ b/aiida_test_cache/__init__.py @@ -1,3 +1 @@ """A pytest plugin for testing AiiDA plugins.""" - -__version__ = '0.1.0.dev1' diff --git a/docs/source/conf.py b/docs/source/conf.py index 667126f..9e43f2a 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -10,12 +10,11 @@ # serve to show the default. import contextlib +import importlib.metadata import os import sys import time -import aiida_test_cache - # -- AiiDA-related setup -------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, @@ -76,7 +75,7 @@ # built documents. # # The full version, including alpha/beta/rc tags. -release = aiida_test_cache.__version__ +release = importlib.metadata.version('aiida_test_cache') # The short X.Y version. version = '.'.join(release.split('.')[:2]) diff --git a/pyproject.toml b/pyproject.toml index 5bdfb4f..9bc6f43 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,8 +7,8 @@ name = "aiida_test_cache" [project] name = "aiida-test-cache" +version = "0.0.1a1" description = "A pytest plugin to simplify testing of AiiDA workflows" -dynamic = ["version"] authors = [ {name = "Dominik Gresch"}, {name = "Leopold Talirz"},