Skip to content

Color output from CI jobs #1090

@ericfrederich

Description

@ericfrederich

GitLab supports color escape sequences when viewing a job's output. Perhaps the same is true of GitHub, Jenkins, or other CI tools.

I'm guessing that the server running these commands is piping stdout and stderr to other processes or files which later get shipped over the wire. Because of this it is likely failing some isatty() call in _compat.py.

While it isn't a tty, it will eventually be displayed by something capable of handling colors. My current workaround is to detect if it's being ran as a GitLab CI job by the presence of an environment variable and explicitly setting ctx.color = True

I think this is something that could/should be handled internally by click so that I don't have to add this workaround to every utility I write which may end up being used within some CI. Does anyone else agree?

workaround

#!/usr/bin/env python

import os

import click


@click.command()
@click.option('--name', '-n', default='Anonymous', envvar='LOGNAME')
@click.pass_context
def cli_main(ctx, name):
    if 'GITLAB_USER_LOGIN' in os.environ:
        ctx.color = True
    click.secho(f'Hello {name}', fg='magenta', bold=True)


if __name__ == '__main__':
    import sys
    cli_main(sys.argv[1:])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions