如何从 GitLab CI 中的 bash 脚本获取彩色消息

如何从 GitLab CI 中的 bash 脚本获取彩色消息
stages:
  - build

build:
  stage: Build
  image: alpine:latest
  variables:
    PY_COLORS: '1'
    LOGURU_COLORIZE: "true"
    ANSIBLE_FORCE_COLORS: '1'
  before_script:
    - yum update
  script:
    - bash ./scripts/foobar.sh
...
  • 我有一个执行自定义 bash 脚本的示例 gitlab ci。我的目标是漂亮地格式化回显消息/日志,以便它们具有粗体、斜体和颜色。为此,我尝试使用输出用于文本格式化目的的实用程序,但是文本渲染时没有颜色或格式!

这是我的示例 bash 脚本片段:

#!/bin/bash -x

# force tput to use the ansi terminal capabilities
export TERM=ansi

# Margin settings
top=18
bottom=18
left=1.6
right=1.6
pagesize=A4
fontsize=30px

# Text color variables
R=$(tput setaf 1)  # Red
G=$(tput setaf 2)  # Green
C=$(tput setaf 6)  # Cyan
Y=$(tput setaf 3)  # Yellow
P=$(tput setaf 5)  # Purple
RESET=$(tput sgr0) # Reset your text

# convenience functions
function bold {
    tput bold
    echo -n "$@"
}

function ul {
    tput smul
    echo -n "$@"
    tput rmul
}

server_name=$(hostname)

function one() {
    echo ""
    echo "Kernel version on $(bold)${Y}${server_name} ${RESET}is: "
    echo ""
    uname -r
    echo ""
}

function two() {
    echo ""
    echo "CPU load on ${G}${server_name}${RESET} is: "
    echo ""
    uptime
    echo ""
}

main (){
    one
    two
}

main

问题/挑战

  • 当我在本地(macos)执行 bash 脚本时,我得到所需的结果,如下所示:

预期产出

  • 当bash脚本在gitlab ci上执行时,它不显示格式。查看 gitlab runner 的示例输出:
tput: terminal attributes: No such device or address
tput: terminal attributes: No such device or address
....
Kernel version on  is: 
4.14.296-222.539.amzn2.x86_64

CPU load on  is: 


进一步阅读:

  • 阅读其他线程,例如我尝试添加以下标志PY_COLORS: '1'ANSIBLE_FORCE_COLORS: '1'但仍然没有颜色效果。

相关内容