Jenkins – Add Color to Console Output

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.

1. Install AnsiColor plugin
On Jenkins:
Manage Jenkins > Manage Plugins > Available
> search and install ‘Ansi Color’

2. Configure your build/job
Under Build Environment section check Color ANSI Console Output and select xterm

TesterFenster-Jenkins-Build-Environment-Color-ANSI
3. 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"
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

4. On console output

Screenshot_2017-04-13_11-13-22

Done!!!

How to stop Jenkins CI build by extract a value from a JSON endpoint in bash using Curl

Curl the URL to get the JSON response and pipe it to the python tool. The python code will extract the value mapped to the given key (number in this case).

machine='dbelab04'
machineport=8080
jobname='mkat_fpga_tests_4k'
buildnumber=$(curl --silent -X POST http://$machine:$machineport/job/$jobname/lastBuild/api/json?tree=number | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["'number'"]';)
curl -X POST http://$machine:$machineport/job/$jobname/$number/stop

Note: the above command actually works

How to make Git commit to trigger a job/build in Jenkins CI

In this blog post I will show you how to configure a post commit hook on Git that notifies a Jenkins CI server about repository changes. This means that whenever a new commit is pushed to the Git repository, the cURL command line tool will be launched to initiate a Http request telling Jenkins to trigger a job.

1. Execute Jenkins build with Curl

In this setup, there is no authentication at Jenkins, considering that I will be responsible for the repository and jenkins server.

To test, I had to execute jenkins with curl


(Test)mmphego@dbelab04:~/src/mkat_fpga_tests/.git/hooks (GIT_DIR!)
|_ [2017-04-05 11:58:47] $ >>> curl -D - -X POST http://dbelab04:8080/job/mkat_fpga_tests_4k/build

HTTP/1.1 201 Created
Date: Wed, 05 Apr 2017 09:59:23 GMT
X-Content-Type-Options: nosniff
Location: http://dbelab04:8080/queue/item/387/
Content-Length: 0
Server: Jetty(9.2.z-SNAPSHOT)

2. Configure a hook of repository

Create a file in .git/hooks called post-commit and do not forget to make it executable.

$ nano $(pwd)/.git/hooks/post-commit

#!/bin/bash
# Jenkins-Git build trigger
# Mpho Mphego

machine=$(uname -n)
echo 'Notifying Jenkins Server to execute a Build on' $machine
curl -X POST http://$machine:8080/job/mkat_fpga_tests_4k/build

Screenshot_2017-04-05_12-57-44

Assumptions:
Git version > 2.6
Jenkins version > 2.0
Git Plugin (Jenkins)
Gitlab hook (Jenkins)

3. Configure Jenkins 

Configure your Jenkins job to be able to “Trigger builds remotely”

Screenshot_2017-04-05_13-18-19.png

Enable SCM polling for each project you want to notify:

  1. Go to the Dashboard.
  2. Click on your project.
  3. Click Configure.
  4. Under Build Triggers check the box for Poll SCM.
  5. Repeat for any other projects.

The notification you send tells Jenkins to poll the repository, so projects will only respond if SCM polling is enabled.

 

Now Jenkins will runs automatically when I/we commit and push using Git via CLI.

Done.

Python’s Virtualenv working with Jenkins

If you use virtualenv to isolate your python project’s environment, and want your code tested automatically — read on, else ignore.

virtualenv isolates your project’s python environment

virtualenv makes sure you lock down your project’s main directory and all subdirectories of it. This ‘lockdown’ means that you never touches your global python binary, or any globally installed libraries (like “sudo pip install ipython” ).

Once locked down, you install all packages again, even those you have globally installed. This enables you to have one version of flask globally installed, but another version in your project. All dependencies can be listed in a separate file and validate a precise environment for you to work with. Tightly controlled dependencies is key to a deployment without surprises.

Jenkins checks the health of your project for each change

Jenkins is a CI server which means it does a lot of repeating stuff so you can focus on doing more important stuff. More specifically, it listens for changes to your project’s version control system (like git).

When changes are detected, the project is built and the test suite is executed. If any step fails, the CI server tells you that it did.

Setup Jenkins, and make it use virtualenv

Jenkins needs some massaging before it handles the hijacked environment of virtualenv. This is how I did it for my local git repository:

  • Download and install Jenkins
  • Start it, it should be up on http://localhost:8080
  • Install the Git Plugin
  • Setup a new project with these properties:
    • Source Code Management: add the URI to your local repository,
    • /Users/you/Sites/asdf in my case. Make sure the jenkins user can read this directory, otherwise the Jenkins GUI will tell you something random about invalid git repo, without a hint about a permissions error.
    • Build Triggers: Poll SCM (with an interval like 0 * * * *). This is needed because
      you’re too lazy to build manually; and
      you can not trigger builds with a git post-commit hook otherwise
    • Build > Execute shell. I’ve used two steps, one for setting up the environment and one for the actual tests:

# Setup a proper path, I call my virtualenv dir "venv" and
# I've got the virtualenv command installed in /usr/local/bin
PATH=$WORKSPACE/venv/bin:/usr/local/bin:$PATH
if [ ! -d "venv" ]; then
        virtualenv venv
fi
. venv/bin/activate
pip install -r requirements.txt --download-cache=/tmp/$JOB_NAME

and


. venv/bin/activate
which python

# reply should be
/home/user/.virtualenv/bin/python