Skip to content

jenkins

Stopping Jenkins build on test failure or reading the logs message

You might have come to this situation. A jenkins job runs though there are test case failing and the build appears to be successful. The problem occurs when you have another job on downstream. The downstream will be running even though there was a problem in the upstream job. So to tackle this problem. Initially I tried to write bash script but that was no so effective so I started searching for the plugin. I found this Jenkins plugin which works like a charm.

  • Jenkins plugin Name: TextFinder plugin
  • Jenkins plugin URL: http://wiki.jenkins-ci.org/display/JENKINS/Text-finder+Plugin

How Does the TextFinder Jenkins plugin works

The plugin has the feature to search the build logs and we can pass what to look for and based on the result we can decide what to do with the jobs. To make it success or make it unstable. Which is very clear from the image below.

jenkins Text finder plugin for making jobs unstable for error
Reading the jenkins logs to detect failure or success

 

 

 

setting up global variables in jenkins

Jenkins has so many cool plugins to ease our life. Today we will be looking into one of my personal favorite plugin, which allow us to set the global environment variable that can be used to entire jenkin jobs. The name of the plugin is Global Variable String Parameter.

How to use the global variable string parameter plugin in jenkin:

  1. Install the plugin
  2. Once it is successfully installed. Visit jenkins manage jenkins section.
  3. Visit the configure system
  4. Scrolling down bellow on that page, you will find the section where you can add the variable name and its value.
    global variable setup in jenkins
  5. Now save the changes and you can use the variable defined similar to how you use other global variables in jenkins. For the example above in image you can use it as $dbname

 

 

 

How to connect to remote server from jenkins, using ssh

You might have written bash scripts or the groovy scripts or any kind of scripts you are familiar with. In my case I regularly go with bash scripts. Writing bash scripts in Jenkins to accomplish the tasks on the same machine is just as how you write bash script in your system and test it. But today we will learn to connect to another server (remove server), make it communicate or do some action on the server it gets connected to and learn how we can pass value while connecting to it.

Some basic of SSH is prerequisite:

To communicate among servers we use ssh command in linux machine. If you are in windows machine, you might be familiar with putty. If you are not familiar with ssh and its syntax, get familiar with it first. A simple syntax and example is given bellow. The ip address provided is just the sample. You need to replace with real ip address of the server.

ssh syntax connecting with key-file:  ssh <username>@<machine-ip>

ssh ubuntu@192.168.0.3

ssh syntax connecting with key-file: ssh <username>@<machine-ip>  -i  <key-file>

ssh ubuntu@192.168.0.3 -i keyfile.pem

Writing bash script in JENKINS execute shell scripts sections:

#!/bin/bash

#will pass this variables to the connected server when SSHing
var1="hellow world"

ssh -tt -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ubuntu@some-ip "name=$var1" '
echo $name
ls -al
'

Note: The codes to execute inside the remote machine must be kept inside ‘ ‘ single quotes or you can use double quotes. But remember not to repeat that inside. Suppose if we are using the single quotes than we can use double quotes where we need to define the string variables.

-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

This will eliminate what appears when we ssh for the first time. To choose ‘yes’ or ‘no’

Also put the jenkins servers public id_rsa.pub key that can be found in ~/.ssh/ to  the authorized_keys in the server which it will be accessing so that it won’t ask for the password.

For example if jenkins is running in server1 and is trying to access server2 then server2 need to add the key of server1 to it’s authorized_keys section.

Watch video instead about jenkins and ssh:

my first hello world python script build running on jenkins

It took 5 minutes at max to setup a jenkins on my local machine. Here is the installation guide I followed to set it up on ubuntu 16.04 machine

  1. wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -

    Added the repository key which should return ok

  2. echo deb https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list

    Adding the package repository address to servers sources.list

  3. sudo apt-get update
  4. sudo apt-get install jenkins

    So the jenkin is ready and up and running at this url and port 8080

0.0.0.0:8080 Typing this on my browser url will start a jenkin setup page asking for the initial password. This page appears when you are opening the jenkins for the first time. The locations where you can find the password is also given in the page . Use the sudo cat command followed by the path given which will return you a password which you need to add it there than a user registration process starts .

Note: Please fill the form properly and create a user account don’t skip it. Last time i skipped it to use the default user name and password but that did’t work and I re-installed the jenkins. That’s it we are ready to start with jenkins

This is what I did after jenkins installation

  1. Created a git hub repository
  2. Created a simple hello world script named it as hellow_world.py
  3. Created my first job to start building my simple hellow_world script.

Jenkins jobs setup details here:

  1. Added a git repository that I created
  2. Added the build section with following bash script
    
    #!/bin/sh
    python hellow_world.py
  3. Apply and save it
  4. Start the build by selecting the project and clicking on build now.
  5. Output

successfully_running hellow world python in jenkins

 

Worked as expected ,happy me and twitted on twitter with big large output you see above.