3

I want to display a table in Gitlab CI log using python script. I check the script locally and it displays the table correctly, but when I run the script in gitlab CI, it breaks the line, although if you try to echo any long line, it will output it correctly.

What needs to be done in gitlab ci so that it does not break the line of the python script output table?

I make a table using the tabulate library, if it's important.

enter image description here

Python script:

#!/usr/bin/env python3

import ruamel.yaml
from tabulate import tabulate
from rich.console import Console

def main():
    with open('cluster.yml', 'r') as cluster_yml:
        cluster_yml_data = yaml.load(cluster_yml)
    path_to_env_file = cluster_yml_data['include'][0]
    with open(path_to_env_file, 'r') as env_yaml:
        env_yaml_data = yaml.load(env_yaml)
    for var, value in env_yaml_data['variables'].items():
        env_yaml_data['variables'][var] = str(env_yaml_data['variables'][var]).replace(' ', '\n')
    console.print(tabulate(env_yaml_data['variables'].items(), tablefmt='fancy_grid', headers=['Variable', 'Value']))

if __name__ == '__main__':
    yaml = ruamel.yaml.YAML()
    yaml.indent(mapping=2, sequence=4, offset=2)
    yaml.preserve_quotes = True
    console = Console(force_terminal=True)
    main()

cluster.yml file:

include:
  - 'cluster-init_variables.yml'

cluster-init_variables.yml file:

variables:
  FOLDER_PREFIX: "bcs"
  FOLDER_NAME: "sandbox"
  FOLDER: "$FOLDER_PREFIX-$FOLDER_NAME"
  INFRA_REPO: "git@github.com:aaaaaaa/bbbb-bbbbbbb/cccccc/dddddd/$FOLDER-repo.git"
  SUBNET_ZONE_A: "1.1.1.1/24"
  SUBNET_ZONE_B: "2.2.2.2/24"
  SUBNET_ZONE_C: "3.3.3.3/24"
  GROUP_ID: "12"
  CLUSTER_NAME: "cluster"
  NG_WORKER_MEMORY: 24
  NG_WORKER_CORES: 8
  NG_SCALE_MIN: 1
  NG_SCALE_MAX: 3
  NG_SCALE_INITIAL: 1
  NG_WORKER_DISK_SIZE: 64
RMNTRVN
  • 63
  • 1
  • 5
  • Please include the relevant code you have. Your code (or the code in the library you are using) appears to not be properly considering the terminal width. – sytech May 31 '22 at 18:33
  • Attached the script and yml files. – RMNTRVN May 31 '22 at 18:49
  • 1
    My guess is that GitLab is adding a newline into lines with more than X (75..80) characters. Truncate your data, or redirect output to a file and save it as a job artifact. https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html – Richard May 31 '22 at 21:53
  • The file in the artifact also has a broken table structure. – RMNTRVN Jun 01 '22 at 06:53

1 Answers1

2

You could pass width argument to Console call and set the width explicitly.

width (int, optional) – The width of the terminal. Leave as default to auto-detect width.
NobbyNobbs
  • 1,341
  • 1
  • 11
  • 17