Monday, May 27, 2019

Jenkins - Integration with PagerDuty

PagerDuty is an incident management platform that provides reliable incident notifications via email, push, SMS, and phone, as well as automatic escalations, on-call scheduling, and other functionality to help teams detect and fix infrastructure problems quickly. In this article we will see how we can configure pagerduty with jenkins and create a incident when a jenkins build fails.

For the demo purpose, i have created a 14 day trial account in pagerduty. Login to the pagerduty using the newly created account, go to configuration -> services -> Add a service.
Give a name and in the integration setting , choose jenkins as integration type

We can keep the rest of the setting as they are. Once the service is created, we will be given with an integration key as below,
Save the integration key some where as this is very important.

Now go to Manage Jenkins - Manage plugins , Install pagerduty plugin. Now create  sample free style job with a error in the execute shell as below,

This will make sure the job gets failed. In the post build actions, choose the Pager duty and fill the necessary details as below,
The service integration key is the same key that we obtained while configuring the jenkins service in pagerduty. Paste the key. You can keep the rest as they are. You can select the checkbox based on your requirement. I have chosen to open incident when job is success, failures, aborted etc. Now once we run the job and if that failes, we will be created with an incident in the pagerduty as below. Refresh your pagerduty page to see the incident.

Read More

Jenkins - Integration with Junit

Unit testing is defined as a type of software testing where individuals units/components of the software ( the code that developers write ) are tested. This testing is done during development of an application. The objective of unit testing is to isolate a section of code and verify its correctness. In a normal programming language , a unit can be taught as an individual function or procedure. The unit testing code will be generally written by developer.

Junit is an open Source Unit Testing framework for Java. It is useful for java Developers to write and run repeatable tests for the code they have written. Junit is an instance of xUnit architecture. Developers basically write and execute unit tests first before writing any code. Once the code is done we should execute all tests and they should pass. Every time a new code is added , we need to re execute all test cases and make sure nothing is broken

In this article, we will see how we can run the Junit test cases as a part of the CI/CD pipeline using jenkins

1. Install the Xunit plugin from Manage Jenkins -> Manage plugins. The xunit plugins once installed supports multiple languages like java ,python by including plugins for that language like Junit for java etc.

2. Provide the maven goals and options as “clean test verify” for the maven project which contains the Junit test cases

3. Choose the Post step action and provide the necessary details like below,

The pattern is the most important one. We need to provide the location where our junit test cases results will be saved. These details will be saved with the name starting with TEST. provide the location as above.

That's it ,once we run our maven project containing junit test cases, we we see the results displayed as,

Read More

Tuesday, May 21, 2019

Jenkins - Integration with JIRA

Jira can be used as a Issue tracking, project management etc for your project and brigins agility with the scrum board and process. The most common use case is to create or update a Jira Ticket when there is an update on the Jenkins Job such as failure.

In this article we will see how we can install Jira and integrate with Jenkins. For the article, we will install jira with an evaluation version and then import a sample project data. Once we have a project, we will then integrate with Jenkins by creating a issue when there is Job failure.

1. Download the Jira software from here.
2. We will be getting a bin extension file. Change the permission of the file using the chmod command and run the bin file as below,
[root@ip-172-31-51-99 tmp]# ./atlassian-jira-software-7.12.3-x64.bin
Unpacking JRE ...
Starting Installer ...
May 21, 2019 6:51:22 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
May 21, 2019 6:51:22 AM java.util.prefs.FileSystemPreferences$2 run
INFO: Created system preferences directory in java.home.
Fontconfig warning: ignoring UTF-8: not a valid region tag

This will install JIRA Software 7.12.3 on your computer.
OK [o, Enter], Cancel [c]
Choose the appropriate installation or upgrade option.
Please choose one of the following:
Express Install (use default settings) [1], Custom Install (recommended for advanced users) [2, Enter], Upgrade an existing JIRA installation [3]

Where should JIRA Software be installed?

Default location for JIRA Software data

Configure which ports JIRA Software will use.
JIRA requires two TCP ports that are not being used by any other
applications on this machine. The HTTP port is where you will access JIRA
through your browser. The Control port is used to startup and shutdown JIRA.
Use default ports (HTTP: 8080, Control: 8005) - Recommended [1, Enter], Set custom value for HTTP and Control ports [2]
HTTP Port Number
Control Port Number
JIRA can be run in the background.
You may choose to run JIRA as a service, which means it will start
automatically whenever the computer restarts.
Install JIRA as Service?
Yes [y, Enter], No [n]
Details on where JIRA Software will be installed and the settings that will be used.
Installation Directory: /opt/atlassian/jira
Home Directory: /var/atlassian/application-data/jira
HTTP Port: 9090
RMI Port: 9005
Install as service: Yes
Install [i, Enter], Exit [e]

Extracting files ...

Please wait a few moments while JIRA Software is configured.
Installation of JIRA Software 7.12.3 is complete
Start JIRA Software 7.12.3 now?
Yes [y, Enter], No [n]

Please wait a few moments while JIRA Software starts up.
Launching JIRA Software ...
Installation of JIRA Software 7.12.3 is complete
Your installation of JIRA Software 7.12.3 is now ready and can be accessed
via your browser.
JIRA Software 7.12.3 can be accessed at http://localhost:9090
Finishing installation ...

I Have followed the custom install so that i can provide jira with different port to run. Now once jira is installed open the jira using <ip>:9090 ( in my case i have given 9090).

Once the page is opened, it will take you to the jira setup. If we choose the first one, jira will connect to the jira server with your jira account and generate the license. The database setup etc everything will be taken care by jira itself.

Choose second one, in  next screen for our demo we will be using the “internal Database” When u click on that it will configure the Database by itself. Click next which will ask for the license key

I have obtained the license key generated by logging into the my atlassian account. Next screen it will ask to configure administrator. Go with the other screen as  it asks for avatar changes etc. I have chosen to use the sample data for my integration , choose Scrum software development and click next.
In the next page it would ask for the name and key for the project you are creating with the above sample data.

Thats it Jira is configured and integrated.

Now on the Jenkins side, Install the Jira plugin from the Manage plugins.
Next Configure System -> Configure Jira as below,
Url needs the Url of your Jira Server where it is running. Create a Credential to connect to the Jira ( the same we used to login to jira )

Now on the JOb -> Choose the Post build action as below,

The important thing is the Project key. Every project in jira will have a key and we can get the key details from Jira projects page. COnfigure the Jira project key which makes jira to create and update issues in that specific project. Run the job and once the job is failed we can see the output as ,
Build Failure

We can see that a JIRA issue SAM-24 is created by Jenkins itself. If we go to jira and see SAM-24, we can find additional details as below,

Read More

Tuesday, May 7, 2019

Jenkins - Integration with Slack

Notifications are very important in a Continuous Integration System. The most basic notification type is the email. A Build job sends a notification as an email when it fails or completes successfully. Now a days people requires these notifications not just by emails but by sending sms or messages to their chat channels etc.

Slack is essentially a chat room for the whole company designed to replace email as your primary method of communication and sharing. Slack has a concept of Workspaces which allow one to organize communication by channels for group discussions, private messaging to share information and more 

One of the best feature of slack is it integrates with different tools by which it allows to send notifications to the channels. In this article we will see how we can send a build notification to the slack channel from the Jenkins Server

Create a free account with the slack. This lets you create a workspace for your collaboration. Once you are logged in to the workspace, choose the Apps below which allows you to install apps.

Once you click on the apps, it will allows you to browse for the apps. Search for jenkins and choose install. We have to create a channel so that the notifications come over there. I created a channel by the name “jenkins-channel”

Once you choose install, it will take you to a page which gives Base URL and Integration key details like below,
Base URL: https:///services/hooks/jenkins-ci/
Integration Token: ******************

On the Jenkins Side
Go to Manage Jenkins -> Manage Plugins, Install the Slack Notification plugin from the Available tab. Make sure your version is 2.2. Install the plugin

Go to Manage Jenkins -> Configure System -> Find “Global Slack Notifier Settings” and fill the values.Fill the app URL as provided by the Slack.
Create a Secret Text token credentials with a name and in the secret text , enter the Integration token provided by slack and Save it

Choose the credential created above in the “integration token credential ID” field. Test the connection and check if that is success.

On the Jenkins Job Side :
Choose the “slack Notifications” on the Post Build actions and choose the checkbox that you would like to see messages from. I have chosen all and run the job.

Once the job is ran successfully in this case, we can see a notification message coming from slack to the workspace in slack. Go to the Slack workspace and in the jenkins-channel , we can see notifications like below,

Read More