How to use the ANSIcolor plugin in Jenkins? - plugins

I have installed the ANSI-color plugin for Jenkins.
In the Jobs I have activated that plugin with the default profile 'xterm'.
I cannot figure out how to colorize the output of the Console Log when printing to the
log from the batch files (Windows-platform).
The documentation on
https://wiki.jenkins-ci.org/display/JENKINS/AnsiColor+Plugin
is not helpful. There are no examples how to actually print in color.
I tried several different echo/print commands, but I cannot get colors to work.
Any hint appreciated.

The ANSI-color plug in transforms your console output to HTML. If your output contains ansi escape sequences, then it converts these special char sequences into (colored) HTML. You can only configure the mapping via 'ANSI color map'.
Example:
\x1b[31m is transformes html color red
It looks that your program does't use Escape sequences. But if you wrote your own software or script, you can use these Escape sequences.
1st Example BASH:
echo -e "\033[31mRed\033[0m"
2nd Example BASH:
printf "\033[31mRed\033[0m"
If you need it, you have to add a newline sequence to printf:
printf "\033[31mRed\033[0m\n"
More to Escape sequences:
English: http://en.wikipedia.org/wiki/ANSI_escape_code
Deutsch: http://de.wikipedia.org/wiki/Escape-Sequenz

There's a lot more documentation available on the Jenkins-ANSIcolor plugin here:
https://github.com/dblock/jenkins-ansicolor-plugin
I wasn't seeing colors because I was using "high intensity" colors (in the 90's range) and these aren't supported. Gotta stick to colors in the 30's

Double check that the software really is outputting ANSI colours. For example, a bash script running this echo will produce colour with the AnsiColor plugin installed, but it'll produce messed-up escape sequences in the console output with no plugin.
In BASH:
echo -e '\033[35mPurple!\033[0m'
Check your project configuration, check the "Build Environment" and make sure "Color ANSI Console Output" is checked.
If you can't find the "Build Environment" section, go to Manage Plugins to double-check that the right plugin is really installed.(I recently installed the "Ansible" plugin instead of "AnsiColor"....)

Jenkins console output is place where you can spend decent amount of time trying to figure out what went wrong (or perhaps right?).
AnsiColor plugins gives you opportunity to color monochromatic Jenkins console output.
Set-by-step guide
Install AnsiColor plugin Under Build Environment section check Color
ANSI Console OutputJenkins -
Color ANSI Console Output -should look like
TesterFenster Inside Execute shell step add something like:
set +x
info() {
echo "\033[1;33m[Info]    \033[0m $1"
}
error() {
echo "\033[1;31m[Error]   \033[0m $1"
}
success() {
echo "\033[1;32m[Success] \033[0m $1"
}
info "This is information message"
error "Houston we have a problem"
success "Great!!!"
echo "Foreground colors"
echo "\033[31m Red \033[0m"
echo "\033[32m Green \033[0m"
echo "\033[33m Yellow \033[0m"
echo "\033[34m Blue \033[0m"
siz
echo "\033[35m Magneta \033[0m"
echo "\033[36m Cyan \033[0m"
echo "Background colors"
echo "\033[41m Red \033[0m"
echo "\033[42m Green \033[0m"
echo "\033[43m Yellow \033[0m"
echo "\033[44m Blue \033[0m"
echo "\033[45m Magneta \033[0m"
echo "\033[46m Cyan \033[0m"
echo "Different combinations"
echo "\033[1;31m Red \033[0m"
echo "\033[1;4;37;42m Green \033[0m"
echo "\033[1;43m Yellow \033[0m"
set -x
you should see like your output

This only works from the Jenkins console, and not from the actual scripts that out put to your console Output

The OP asked explicitly about using AnsiColor Jenkins Plugin on Windows, so here is my answer:
cmd.exe is not a terminal emulator
As #ocodo wrote, cmd.exe (which is triggered by Jenkins) is not a terminal emulator, so it won't support ANSI output from batch commands by default.
So this is just unsupported on Windows.
These tests were unsuccessful in Jenkins using Windows Batch build step:
echo \e[33m hello \e[0m
echo \033[33m hello \033[0m
echo \x1b[33m hello \x1b[0m
echo \27[33m hello \27[0m
echo \[33m hello \[0m
echo ←[33m hello ←[0m
Last line: To inject an ESC character in a Firefox testbox form, press alt+27, which is also the escape sequence in most editors. more info here: http://www.robvanderwoude.com/ansi.php#AnsiColor and https://groups.google.com/forum/#!topic/jenkinsci-users/0OcjlZQfqrk
A solution: Use Python print for text coloring
The build jobs which use Python for console output had no problem with AnsiColor, so if Python is available in your environment where the console output is generated by a script, you can inject ANSI codes from there. Here's how I worked around it to make it colorful.
Create a helper script in util/ansiGreen.py like so:
#!/usr/bin/env python
print("\033[32m");
Register as env var at the level of your global build configuration:
set ansiGreen=python %cdRootPath%util\ansiGreen.py
Add it to your scripts:
call make-some-tests.bat
if %errorlevel% gtr 0 (
%ansiRed%
echo [ERROR] You screwed up trunk!
%ansiOff%
)
As you can see, %ansiOff% is also required, to end with coloring (Its simply "\033[0m" printed through python). Else everything after %ansiRed% will be in red.
This script is run on a Jenkins slave. To set and get an environment directly within Jenkins batch build step is another challenge.
It's a bit ugly but after a lot of research it seems to be the best workaround to enable AnsiColor to be returned to a Windows Jenkins master from a Windows Jenkins slave.

Related

redirecting echo with undefined variable

I'm running a script in solaris 11 with different results depending of the shell used.
The script has an echo redirecting to a file given by an environment value:
echo "STARTING EXEC" >> $FILE
ps. EXEC is just the message the script show, it's not using exec command.
If I execute the script without the variable FILE defined (using /usr/bin/ksh):
./start.sh[10]: : cannot open
and the script continue the execution.
The flags for ksh are:
echo $-
imsuBGEl
But if I change to /usr/xpg4/bin/sh, the script show me the echo in stdout and there is no error shown.
The flags for xpg4 sh are:
echo $-
imsu
I tried to change the flags with set +- (I can't remove El flags, but BG are removed ok), but can't get the same behavior.
Is there anything I can do to get the same result using ksh without cannot open error?
/usr/bin/ksh --version
version sh (AT&T Research) 93u 2011-02-08
I'll want the script keep going, showing the message in stdout, instead of showing the error just like it does now.
Like shellter said in the comments, the good thing to do is to check if the FILE variable is defined before doing anything. This is a script migration from an HPUX to a SOLARIS environment, and client think they must have the same result as before (we unset FILE variable before execution to test it).
You are likely running Solaris 11, not Solaris 64.
Should you want to have your scripts to work under Solaris 11 without having to search everywhere the bogus redirections, you can simply replace all shebangs (first line) by #!/usr/xpg4/bin/sh.
The final solution we are going to take is to install the ksh88 package and use it like default shell (/usr/sunos/bin/ksh). This shell have the same behavior the client had before, and we can let the scripts with no modifications.
The ksh used in solaris 11 is the 93 (http://docs.oracle.com/cd/E23824_01/html/E24456/userenv-1.html#shell-1)
Thanks #jlliagre and #shellter for your help.

How do I tell eclipse that an external tool is done executing?

The Eclipse IDE has a feature where one could run external tools. I use it to run batch scripts. Oddly if a batch script runs a powershell command, the powershell command will never exit until I hit enter. This is especially odd since it exits just fine when running in cmd. How should I correct my script so that it runs as expected via the eclipse external tools?
Current script (foo.bat):
#echo off
echo "Hello 1"
REM Configure this to your installation of maven.
SET "CMD=C:\foo.ps1"
REM Reformat args to be Powershell friendly.
SET "ARGS=%*"
SET "ARGS=%ARGS: =' '%"
PowerShell.Exe -Command "%CMD%" '%ARGS%'
echo "Hello 2"
EXIT /B
In cmd, I see "Hello 1", the output of %CMD%, and "Hello 2". In Eclipse, I see "Hello 1", the output of %CMD%, and then it hangs in the progress tab forever until I click the Console window and press the enter key.
I tried passing the -NonInteractive flag to Powershell. I tried having my Powershell script echo a newline at the end. Not sure how to get this to "just work".
Found the answer. I needed to add a NUL redirect to the end of my Powershell command. So it looks like this:
#echo off
REM Configure this to your installation of maven.
SET "CMD=C:\foo.ps1"
REM Reformat args to be Powershell friendly.
SET "ARGS=%*"
SET "ARGS=%ARGS: =' '%"
PowerShell.Exe -Command "%CMD%" '%ARGS%' < NUL
Note that I also removed the dubugging code from the script found in my question. If you add that code back in, you'll see that it echos everything now.

including command line arguments in window title

My current fish_title function is as follows:
# Set the window title
function fish_title
if [ $_ != fish ]
echo (prompt_pwd)": $_"
else
echo (prompt_pwd)":"
end
end
It puts an abbreviated version of the current working directory and the currently running command in the window's title bar. However, what I need is to put both the base command and its arguments in the window title. For example, if I run
ssh home
currently I see "~: ssh" in the window title. I need "~: ssh home". This is because I use a password entry program that triggers off the window title and the password must match the ssh destination.
Is there a way to include the command line options in the window title from fish shell?
I requested this in fish back in 2012, but it's finally been added in development versions as $argv.
Approaching your desired goal from a different angle, you could do as I have done and explicitly include the remote hostname in your title. I already extract and use the hostname in my fish prompt via:
set -g __fish_prompt_hostname (hostname|cut -d . -f 1)
... so I re-use that __fish_prompt_hostname variable in my fish title if, and only if, I'm currently connected to a remote host via SSH. You could do something similar, perhaps modifying your relevant line above to be:
if [ $SSH_CONNECTION ]
echo (prompt_pwd)": $_ $__fish_prompt_hostname"
end
As an aside, I published the Tacklebox and Tackle projects in order to share these kinds of fish functions and tools, so feel free to check them out, see if anything there meets your needs, and/or contribute to the projects.

Invoking external commands with options from a makefile

I have a makefile for compiling Arduino programs.
I need to add some text at the beginning of some files based on some logic. I am using echo command for that.
ECHO = echo
and later in the file, I have lot of places like
$(OBJDIR)/%.cpp: %.pde
$(ECHO) '#if ARDUINO >= 100\n #include "Arduino.h"\n#else\n #include "WProgram.h"\n#endif' > $#
which works fine.
Recently, some users complained that echo command doesn't work properly in some linux distros and I had to add the '-e' option to the echo command.
So I changed the first line where I declare the command to
ECHO = echo -e
This is not working, because makefile considers -e as part of the text and not as part of the option.
Edit:
I am not getting any error, but the text -e is also appended to the file that I am creating.
Is there a way to declare the -e as an option and not as part of the text?
Most likely you're seeing behavior differences because echo is a shell built-in command in some versions of some shells. Then that's being compounded because make only sometimes uses the shell to invoke commands -- it will prefer to invoke commands directly if possible. So, sometimes, on some systems, you are not invoking the echo command that you think you are.
You would probably have better luck by setting
ECHO = /bin/echo -e
which will explicitly invoke the external echo command, even if the shell has a built-in version. That way you should get consistent results.
if get /bin/sh: 1: -e: not found error it's related to your shell, not makefile.else, please put your error. of course if you get error.

How to echo custom startup message in vim

Is there a way to echo a custom message in vim on startup? I tried using echo "Message" in my vimrc, but nothing seems to come up.
You probably want to set up an autocmd to ensure everything is finished loading before your message appears.
autocmd VimEnter * echo "Message"
Heres a command line version that might be helpful in tandem with some type of launcher:
vim -c 'e .|redraw|echom "Welcome!"'