log icon indicating copy to clipboard operation
log copied to clipboard

colors doesn't work on github actions out of the box

Open os14 opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. we have a cli tool that uses charmbracelet/log for logging, locally it prints nice colors, but in github actions terminal it doesn't print colors.

Describe the solution you'd like perhaps the issue is on github side but i would like to get a workaround for this or ideally make it works out of the box.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context local: image

github actions: image

os14 avatar May 30 '24 15:05 os14

@os14 this is intentional, GitHub Actions and other CIs export CI=1 to indicate that the application is running in a CI environment. Some CIs don't support colors. Termenv, the underlying library currently used to render colors, detects the CI environment variable and disables colors accordingly. To enable colors make sure that you set CI='' in your workflow or application.

aymanbagabas avatar May 30 '24 15:05 aymanbagabas

@os14 this is intentional, GitHub Actions and other CIs export CI=1 to indicate that the application is running in a CI environment. Some CIs don't support colors. Termenv, the underlying library currently used to render colors, detects the CI environment variable and disables colors accordingly. To enable colors make sure that you set CI='' in your workflow or application.

I found that, in GitHub Actions at least, all it takes is CLICOLOR_FORCE=1.

I tested the logics here:

  • https://github.com/muesli/termenv/blob/master/termenv.go#L32
  • https://github.com/muesli/termenv/blob/master/termenv.go#L35-L37
  • https://github.com/muesli/termenv/blob/master/termenv.go#L111-L113

Using the following snippet:

// fatih/color
white := color.New(color.FgBlue)
boldWhite := white.Add(color.Bold)
boldWhite.Println("Colors using fatih/color")

// charmbracelet/log
logger := log.New(os.Stdout)
logger.Info("Colors using charmbracelet/log")

if isatty.IsTerminal(os.Stdout.Fd()) {
	fmt.Println("IsTerminal: true")
} else {
	fmt.Println("IsTerminal: false")
}
fmt.Printf("TERM: %s\n", os.Getenv("TERM"))
fmt.Printf("CI: %s\n", os.Getenv("CI"))
fmt.Printf("CLICOLOR_FORCE: %s\n", os.Getenv("CLICOLOR_FORCE"))

and adding the following to the Github Actions step, more info on that here:

shell: 'script -q -e -c "bash --noprofile --norc -eo pipefail {0}"'

Here are the results:

  • image
  • image
  • image
  • image
  • image
  • image
  • image

paleboot avatar Jun 21 '24 07:06 paleboot