Showing posts with label Jenkins. Show all posts
Showing posts with label Jenkins. Show all posts

Thursday 30 November 2017

Execute Shell in Jenkins - Update Permissions Example

Hello guys. How's it going? In today's post, let us look into an example of how to execute a shell command in Jenkins.

For illustration, let us consider a Java based project which is under IBM Clearcase source control and built using Maven on a Linux machine. Now we want to execute a bash command to update the permissions of all pom.xml files (i.e Parent pom's and Child pom's).

Check a few sample commands for reference :

find $WORKSPACE/$CLEARCASE_VIEWNAME/PROJECT_SRC_CVOB/SRC -type f -name pom.xml -exec chmod 755 {} \;

find $WORKSPACE/$CLEARCASE_VIEWNAME/PROJECT_SRC_CVOB/SRC -type f -name pom.xml -exec chmod 444 {} \;

find $WORKSPACE/$CLEARCASE_VIEWNAME/PROJECT_SRC_CVOB/SRC -type f -name pom.xml -exec chmod 777 {} \;

Hope it helps. Ciao!

Monday 23 October 2017

How to chain jobs in Jenkins using Parameterized trigger plugin

In Jenkins, sometimes we may need to trigger multiple jobs in a particular sequence, each performing some unique task. For instance let us consider a scenario, where we have one parent job to perform a maven build of a Java based project. We also have 2 separate child jobs for packaging - let's say one creates a Windows installer and the 2nd one which creates a Linux installer. Now, we want to automatically trigger the packaging jobs immediately after the build is finished. The Parameterized Trigger plugin helps us exactly to achieve the same. Using this, we can chain multiple jobs and pass parameters across them.

Let us consider the above scenario in some more detail.

Firstly, make sure the Parameterized Trigger plugin is installed in Jenkins.


Now, as said above I have one parent job which essentially performs a maven build of a Java based project. Once the build is finished, I want to trigger 2 child jobs - one which creates a Windows package and the other which creates a Linux package. In our case, we have the version 2.31 installed. Latest versions are available periodically.

Now, lets configure the chaining of packaging child job within the parent job i.e Maven build job. While doing so, I want to a bunch of pre-defined parameters across the build. For eg: I want to pass the build version and the location of Clearcase build view. The parameters could be either a constant variable or an environment variable.

Make sure to tick the option "This build is parameterized" and add a few parameters as desired.

Now add a new build step "Trigger/call builds on other projects" from the list.


Now all you have to do is mention the child build that you want to fire and the list of specific pre-defined parameters that you want to pass to it. Check the screenshot below for illustration.


Here we are passing 2 parameters - version and path_loc. Kindly note the syntax. Note that these 2 parameters need to be defined in both the parent and child jobs. In case you do not want to pass all the current build parameters instead of specific ones, simple choose the "Current build parameters" from the "Add Parameter dropdown list."


Alternatively, you could also pass the parameters from a specific properties file as well by specifying its location.

Ciao guys. Drop in your comments for any suggestions, questions or queries please. Grazie!

Tuesday 10 October 2017

How to automate Jenkins config backup

In today's post on Jenkins, let us learn how to take regular backups of our Jenkins environment configuration. There are a bunch of plugins out there which can help take thin and/or thick backups. But in our post we learn to take backups using a custom batch script.

We write a simple batch file which recursively takes backup of all Jenkins jobs and saves them in the destination directory. Later we set up a Windows scheduler job to execute this batch file daily.

rem IroncladZone
echo OFF

set today=%DATE:~7,2%%DATE:~4,2%%DATE:~10,4%
set url=http://17.16.18.14:8080/jenkins/
set dest_dir=C:\Jenkins_Jobs_backup\

cd /d "C:\Program Files (x86)\Jenkins\jobs"

FOR /D %%i IN (*) DO (

echo "Backing up %%i.xml"

"C:\Program Files\Java\jdk1.8.0_45\bin\java" -jar jenkins-cli.jar -s %url% get-job "%%i" > "%dest_dir%/%%i_%today%.xml"

echo "Successfully backup %%i.xml"

)

Note for taking backups, we use Jenkins' command line utility i.e jenkins-cli.jar. With this we save the Jenkins job configuration .xml file at the destination folder. Note that we append this .xml file name with today's date time-stamp so that we could easily identify it later.

Now all we have to do is set a Windows task scheduler job which runs this batch file to take backups at say 12:00 noon daily.


Hope it helps folks. Ciao!

Tuesday 3 October 2017

How to deploy specific file to Artifactory using Maven from Jenkins

This post will be helpful in a scenario where you are required to deploy i.e upload just a specific individual file (eg: xyz.zip or abc.jar) to Artifactory using Maven from Jenkins.

For illustration, configure a deploy-file job in Jenkins and add the following Maven goal.

clean deploy:deploy-file
-DgroupId=com.company.abc.message -DartifactId=message -Dversion=${version}
-DgeneratePom=false
-Dpackaging=jar
-DrepositoryId=artifactory
-Durl=http://17.16.18.12:8080/artifactory/simple/libs-release-local/
-Dusername=admin -Dpassword=admin-password
-Dfile=C:\Users\ironcladzone\Desktop\messageTest\message-${version}.jar

This will basically upload the message jar with a dynamically passed version parameter. Assuming the ${version} parameter value is defined 5.3.2 in Jenkins, the above command will essentially deploy message-5.3.2.jar to the libs-release-local repository.

Ciao!

How to manually install a plugin in Jenkins

Sometimes in corporate environments, accessing certain things from the internet is blocked by the firewall due to restricted access. In case if you ever come across such a scenario, wherein you are not able to install a plugin in Jenkins due to firewall and/or proxy issue, you always have a workaround to manually install the plugin.

Firstly go to the specific plugin page and download it. For instance, consider the Email-ext plugin page for Jenkins and download it. It will have .hpi extension.


Now go the plugins configuration in Jenkins i.e Manage Jenkins => Manage Plugins => Advanced.
Here you'll see the option to upload the plugin .hpi file

Hope it helps. Cheers!

Customized Build email notifications in Jenkins

When using Jenkins for automated builds, as most of you guys may be aware, we can setup SMTP settings for email notifications of build related events. We will cover the topic of SMTP setup in another separate post. In this post, let us just stick to the email customization part. Note that the SMTP settings must be configured in Jenkins for this plugin to work smoothly. For customized email notifications, we will use the Email-ext plugin to replace the default Jenkins'email settings.

With this plugin, we can setup email settings for various trigger conditions like a successful build, aborted build or a build failure etc.


In today's illustration, we'll cover the notification for Failure - Any trigger. In other words, Jenkins will fire a notification the moment there is any kind of build failure. We will use the HTML formatting in the notification. Use the following variables in the job configuration as shown in below screenshot.


In the Content field, try the following piece of code as below :


As you see, we include information like the Jenkins job URL, the cause of the failure which will be a short description of the error, the changes that went into the build and a link to the console URL.

Note the line which includes a regex - we basically do a simple pattern matching i.e we look for the words "BUILD FAILURE" and then display only that part from the build error. In better words, we truncate the verbose build log and show only the relevant error information in the mail notification.

${BUILD_LOG_REGEX, regex="^.*?BUILD FAILURE.*?$", linesAfter=10, matchedLineHtmlStyle="width:100%", showTruncatedLines="false", defaultValue="For detailed information, please check job console link below to find cause of failure"}  

Apart from this, the plugin also allows us to optionally attach the build log. However note that the build logs usually tend to be of big sizes (depending on the size of the project) and could fill your mailboxes fast.


Now with the email notification configured, let us look into how the notifications will actually look like. So here's the output - check the screenshot below :



Signing out for now. Hope it helps. Ciao guys!

Saturday 30 April 2016

Start Stop Jenkins from Terminal on Mac OS X

Ola guys. I hope you came across my previous posts on setting up Ant and Maven with Jenkins. In today's post I'll show you how to start and stop Jenkins from Terminal on Mac OS X machine.

I assume you know how to shutdown Jenkins from console OR restart it from the URL field.

Alright so here we go.

For staring Jenkins from command line, type this in Terminal :

sudo launchctl load /Library/LaunchDaemons/org.jenkins-ci.plist

Give it a few seconds for Jenkins to startup and you can then see the console in the browser. If you still stay executing the startup command again you'll get the following message :

/Library/LaunchDaemons/org.jenkins-ci.plist: service already loaded

In the meantime you can double check if Jenkins has started by checking the process status in Terminal :

ps -ef | grep jenkins

For stopping Jenkins from the command line, type this in Terminal :

sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist

A rougher way to stop Jenkins is by killing the process ID of Jenkins that you found above with the ps -ef command.


How to setup Jenkins with custom Maven Home

Guys I earlier posted a quick short article on how to setup Jenkins with custom Ant Home. In today's post you'll learn how to similarly setup Jenkins with your custom Maven Home.

As usual go to Manage Jenkins -> Configure System

Scroll down to the Maven installations and untick Install Automatically. Now simply enter details about your custom MAVEN_HOME. Refer the screenshot below for reference please.


Cheers guys! Hope you are having a good time reading my blog. Make sure to dig in to discover more interesting posts.

Friday 29 April 2016

How to setup Jenkins with custom Ant Home

Ola guys! From today onwards am also including fresh new posts on Jenkins. Jenkins as you know is a popular continuous integration tool where you can setup jobs and define steps to create custom builds. The main advantage of any CI tool is the quick fast feedback. It thereby reduces the overall delivery time and are to able to spot for any errors really fast. I'll post another article on the overall generic advantages of Jenkins later.

In today's post I will start with a very simple basic configuration. I assume you have downloaded Jenkins and set it up on your machine.

Now before we start using Jenkins to perform builds, we need to configure it right. Let's do that.

Go to Manage Jenkins -> Configure System and scroll down to the Ant installations.

If you have already downloaded Ant and set up your custom ANT_HOME and want to use that, then do the following :

Untick Install Automatically and enter the details for ANT_HOME and give it a nice name to identify it. Check the following screenshot for reference.


Now just click Apply. Save. Cheers!
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker