Skip to content

Grouping of log lines emitted by chartpress when running inside a GitHub workflow #130

@consideRatio

Description

@consideRatio

Proposed change

GitHub Actions provide some features to help tools present nicer logs, one of those features are groups (of lines). This is described in the GitHub Workflow Commands reference.

This is how it can look when a single build step built loads of docker files with loads of logs in each. It was somewhat readable still thanks to the grouping. But, with chartpress currently, we don't have such grouping.

image

Implementation idea

We wrap work to be done for example, to build or push an image, inside a with _gwc_group(f"Building {image}"): scope using the example code below. Note that it checks for if GITHUB_ACTIONS is set or not, that is the indication that we are inside a GitHub actions runner.

import json
from contextlib import contextmanager


def _gwc(command_name, command_value="", **params):
    if not os.environ.get("GITHUB_ACTIONS"):
        return
    # Assume non-string values are meant to be dumped as JSON
    if not isinstance(command_value, str):
        command_value = json.dumps(command_value)
        print(f"dumped json: {command_value}")
    if params:
        comma_sep_params = ",".join([f"{k}={v}" for k, v in params.items()])
        print(f"::{command_name} {comma_sep_params}::{command_value}")
    else:
        print(f"::{command_name}::{command_value}")


@contextmanager
def _gwc_group(group_name):
    """
    Entering the context prints the group command, and exiting the context
    prints the endgroup command.<<
    """
    try:
        yield _gwc("group", group_name)
    finally:
        _gwc("endgroup", group_name)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions