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.
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 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:
Install the plugin
Once it is successfully installed. Visit jenkins manage jenkins section.
Visit the configure system
Scrolling down bellow on that page, you will find the section where you can add the variable name and its value.
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
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 syntax connecting with key-file: ssh <username>@<machine-ip> -i <key-file>
ssh email@example.com -i keyfile.pem
Writing bash script in JENKINS execute shell scripts sections:
#will pass this variables to the connected server when SSHing
ssh -tt -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ubuntu@some-ip "name=$var1" '
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.
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
sudo apt-get update
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
Created a git hub repository
Created a simple hello world script named it as hellow_world.py
Created my first job to start building my simple hellow_world script.
Jenkins jobs setup details here:
Added a git repository that I created
Added the build section with following bash script
Apply and save it
Start the build by selecting the project and clicking on build now.
Worked as expected ,happy me and twitted on twitter with big large output you see above.