From d43cceddc9250a19d737985367d0d1cd152527ce Mon Sep 17 00:00:00 2001 From: Jirka Date: Sun, 31 Mar 2024 15:36:29 +0200 Subject: [PATCH] ci/cd: publishing nightly packages with `devYYYYMMMDD` --- .github/workflows/release-nightly.yml | 47 +++++++++++++++++++++++++++ .github/workflows/release-pypi.yml | 3 +- setup.py | 35 ++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/release-nightly.yml diff --git a/.github/workflows/release-nightly.yml b/.github/workflows/release-nightly.yml new file mode 100644 index 0000000000..08a0136694 --- /dev/null +++ b/.github/workflows/release-nightly.yml @@ -0,0 +1,47 @@ +name: Nightly packages + +on: + pull_request: # this shall test only the part of workflow before publishing + branches: [main, "release/*"] + types: [opened, reopened, ready_for_review, synchronize] + paths: + - ".github/workflows/release-nightly.yml" + schedule: + - cron: "0 0 * * 0" # on Sundays + workflow_dispatch: {} + +defaults: + run: + shell: bash + +jobs: + releasing-nightly: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install dependencies + run: python -m pip install --user --upgrade setuptools wheel + - name: Build + env: + CONVERT_VERSION2NIGHTLY: "1" + run: python setup.py sdist bdist_wheel + + # We do this, since failures on test.pypi aren't that bad + - name: Publish to Test PyPI + if: startsWith(github.event.ref, 'refs/tags') || github.event_name == 'release' + uses: pypa/gh-action-pypi-publish@v1.8.14 + with: + user: __token__ + password: ${{ secrets.test_pypi_password }} + repository_url: https://test.pypi.org/legacy/ + + - name: Publish distribution 📦 to PyPI + if: startsWith(github.event.ref, 'refs/tags') || github.event_name == 'release' + uses: pypa/gh-action-pypi-publish@v1.8.14 + with: + user: __token__ + password: ${{ secrets.pypi_password }} diff --git a/.github/workflows/release-pypi.yml b/.github/workflows/release-pypi.yml index 078f9e6066..cfcf1409eb 100644 --- a/.github/workflows/release-pypi.yml +++ b/.github/workflows/release-pypi.yml @@ -10,9 +10,8 @@ on: # Trigger the workflow on push or pull request, but only for the main branc # based on https://github.com/pypa/gh-action-pypi-publish jobs: - build: + releasing-pypi: runs-on: ubuntu-22.04 - steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 diff --git a/setup.py b/setup.py index b0ee14e897..817caa05da 100755 --- a/setup.py +++ b/setup.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import glob import os +import re from importlib.util import module_from_spec, spec_from_file_location from pathlib import Path @@ -10,6 +11,8 @@ _PATH_ROOT = os.path.dirname(__file__) _PATH_REQUIRES = os.path.join(_PATH_ROOT, "requirements") +# check if os env. variable is set to convert version to nightly +_CONVERT_VERSION = int(os.environ.get("CONVERT_VERSION2NIGHTLY", 0)) def _load_py_module(fname, pkg="thunder"): @@ -19,6 +22,35 @@ def _load_py_module(fname, pkg="thunder"): return py +def convert_version2nightly(about_file: str = "thunder/__about__.py") -> None: + """Load the actual version and convert it to the nightly version.""" + from datetime import datetime + + # load the about file + with open(about_file) as fo: + lines = fo.readlines() + idx = None + # find the line with version + for i, ln in enumerate(lines): + if ln.startswith("__version__"): + idx = i + break + if idx is None: + raise ValueError("The version is not found in the `__about__.py` file.") + # parse the version from variable assignment + version = lines[idx].split("=")[1].strip().strip('"') + # parse X.Y.Z version and prune any suffix + vers = re.match(r"(\d+)\.(\d+)\.(\d+).*", version) + # create timestamp YYYYMMDD + timestamp = datetime.now().strftime("%Y%m%d") + version = f"{'.'.join(vers.groups())}.dev{timestamp}" + # print the new version + lines[idx] = f'__version__ = "{version}"\n' + # dump updated lines + with open(about_file, "w") as fo: + fo.writelines(lines) + + def _load_requirements(path_dir: str, file_name: str = "requirements.txt") -> list: reqs = parse_requirements(open(os.path.join(path_dir, file_name)).readlines()) return [r for r in list(map(str, reqs)) if "@" not in r] @@ -56,6 +88,9 @@ def _load_readme_description(path_dir: str, homepage: str, version: str) -> str: return text +if _CONVERT_VERSION: + convert_version2nightly() + about = _load_py_module("__about__.py") # https://packaging.python.org/discussions/install-requires-vs-requirements /