Skip to content

Commit

Permalink
[ci] Better log grouping.
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel-Naumann committed Aug 21, 2023
1 parent bbff50a commit b5825bb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 52 deletions.
63 changes: 41 additions & 22 deletions .github/workflows/root-ci-config/build_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ def parse_args():
return args


@github_log_group("To reproduce")
def print_trace():
build_utils.log.print()

Expand Down Expand Up @@ -250,7 +249,9 @@ def show_node_state() -> None:
if result != 0:
build_utils.print_warning("Failed to extract node state")

# Just return the exit code in case of test failures instead of `die()`-ing; report test@github_log_group("Run tests")
# Just return the exit code in case of test failures instead of `die()`-ing; report test
# failures in main().
@github_log_group("Run tests")
def run_ctest(extra_ctest_flags: str) -> int:
ctest_result = subprocess_with_log(f"""
cd '{WORKDIR}/build'
Expand Down Expand Up @@ -278,34 +279,36 @@ def archive_and_upload(archive_name, prefix):
)


@github_log_group("Build")
def build(options, buildtype):
generator_flags = "-- '-verbosity:minimal'" if WINDOWS else ""
@github_log_group("Configure")
def cmake_configure(options, buildtype):
result = subprocess_with_log(f"""
cmake -S '{WORKDIR}/src' -B '{WORKDIR}/build' {options} -DCMAKE_BUILD_TYPE={buildtype}
""")

if not os.path.isdir(f'{WORKDIR}/build'):
result = subprocess_with_log(f"mkdir {WORKDIR}/build")
if result != 0:
die(result, "Failed cmake generation step")

if result != 0:
die(result, "Failed to create build directory")

if not os.path.exists(f'{WORKDIR}/build/CMakeCache.txt'):
result = subprocess_with_log(f"""
cmake -S '{WORKDIR}/src' -B '{WORKDIR}/build' {options} -DCMAKE_BUILD_TYPE={buildtype}
""")
@github_log_group("Dump existing configuration")
def cmake_dump_config():
# Print CMake cached config
result = subprocess_with_log(f"""
cmake -S '{WORKDIR}/src' -B '{WORKDIR}/build' -N -L
""")

if result != 0:
die(result, "Failed cmake generation step")
else:
# Print CMake cached config
result = subprocess_with_log(f"""
cmake -S '{WORKDIR}/src' -B '{WORKDIR}/build' -N -L
""")
if result != 0:
die(result, "Failed cmake cache print step")

if result != 0:
die(result, "Failed cmake cache print step")

@github_log_group("Dump requested build configuration")
def dump_requested_config(options):
print(f"\nBUILD OPTIONS: {options}")


@github_log_group("Build")
def cmake_build(buildtype):
generator_flags = "-- '-verbosity:minimal'" if WINDOWS else ""

result = subprocess_with_log(f"""
cmake --build '{WORKDIR}/build' --config '{buildtype}' --parallel '{os.cpu_count()}' {generator_flags}
""")
Expand All @@ -314,6 +317,22 @@ def build(options, buildtype):
die(result, "Failed to build")


def build(options, buildtype):
if not os.path.isdir(f'{WORKDIR}/build'):
result = subprocess_with_log(f"mkdir {WORKDIR}/build")

if result != 0:
die(result, "Failed to create build directory")

if not os.path.exists(f'{WORKDIR}/build/CMakeCache.txt'):
cmake_configure(options, buildtype)
else:
cmake_dump_config()

dump_requested_config(options)

cmake_build(buildtype)

@github_log_group("Rebase")
def rebase(base_ref, head_ref) -> None:
head_ref_src, _, head_ref_dst = head_ref.partition(":")
Expand Down
57 changes: 27 additions & 30 deletions .github/workflows/root-ci-config/build_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@
from openstack.connection import Connection
from requests import get


def github_log_group(title: str):
""" decorator that places function's stdout/stderr output in a
dropdown group when running on github workflows """
def group(func: Callable):
@wraps(func)
def wrapper(*args, **kwargs):
print("::group::" + title)

try:
result = func(*args, **kwargs)
except Exception as exc:
print("::endgroup::")
raise exc

print("::endgroup::")

return result

return wrapper if os.getenv("GITHUB_ACTIONS") else func

return group


class Tracer:
"""
Trace command invocations and print them to reproduce builds.
Expand All @@ -31,13 +55,9 @@ def __init__(self, image: str, docker_opts: str):
def add(self, command: str) -> None:
self.trace += '\n(\n' + textwrap.dedent(command.strip()) + '\n)'

@github_log_group("To replicate this build locally")
def print(self) -> None:
if self.trace != "":
print("""\
######################################
# To replicate build locally #
######################################
""")
if self.image:
print(f"""\
Grab the image:
Expand All @@ -49,28 +69,6 @@ def print(self) -> None:

log = Tracer("", "")

def github_log_group(title: str):
""" decorator that places function's stdout/stderr output in a
dropdown group when running on github workflows """
def group(func: Callable):
@wraps(func)
def wrapper(*args, **kwargs):
print("::group::" + title)

try:
result = func(*args, **kwargs)
except Exception as exc:
print("::endgroup::")
raise exc

print("::endgroup::")

return result

return wrapper if os.getenv("GITHUB_ACTIONS") else func

return group


def print_fancy(*values, sgr=1, **kwargs) -> None:
"""prints message using select graphic rendition, defaults to bold text
Expand Down Expand Up @@ -112,12 +110,11 @@ def subprocess_with_log(command: str) -> int:
return result.returncode



def die(code: int = 1, msg: str = "") -> None:
print_error(f"({code}) {msg}")

log.print()

print_error(f"({code}) {msg}")

sys.exit(code)


Expand Down

0 comments on commit b5825bb

Please sign in to comment.