Skip to content

Commit

Permalink
ci/cd: publishing nightly packages with devYYYYMMMDD
Browse files Browse the repository at this point in the history
  • Loading branch information
Borda committed Mar 31, 2024
1 parent b7bed7b commit 9fef6ed
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/release-nightly.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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/[email protected]
with:
user: __token__
password: ${{ secrets.pypi_password }}
3 changes: 1 addition & 2 deletions .github/workflows/release-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"):
Expand All @@ -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]
Expand Down Expand Up @@ -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 /
Expand Down

0 comments on commit 9fef6ed

Please sign in to comment.