Manage Your Routine Tasks With Jenkins

1
9225

This article shows how to set up Jenkins for email-notification, and build set-up on Linux- fedora core.

In a software project life cycle, we are continuously integrating software on the version control, and building it. This is a repetitive job, and it can certainly be automated. By automation, we can save time, and also avoid human errors. In order to automate such jobs, open community has gifted us with a tool called ‘Jenkins’.
So, Jenkins in more simple terms!
A similar to Cron Job, Jenkins can schedule the build. Not only scheduling, it can automate test and notify the build/test status to the user.  Don’t you think this as a remarkable feature? We have seen in the Software Industry that a few people are dedicated to build up such servers. They do write a complex Perl scripts to achieve such tasks. There comes a handy tool to achieve all this in a very short span of time. It is Jenkins.

The Installation process
I believe that you have a wget utility installed on your fedora. If it is not installed, then do the following on a Fedora core machine.

 /** Install wget utility - command **/
$sudo yum install wget

Here come the installation steps for Jenkins.

/** Install Jenkins on a fedora core machine **/
$sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
$sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
$sudo yum install jenkins

Wow!! Jenkins is installed now.

Start, Stop & Restart!
It is a daemon that runs as a service. Thus, you can start, stop, and restart the Jenkins. Here are the commands to start, stop, and restart the services.

$sudo service jenkins stop
$sudo service jenkins start

or

$sudo service jenkins restart

[Disclaimer]: I prefer to do stop and start rather than restart.

Access Jenkins

You can interact with Jenkins through a web page. Open your favorite browser, and paste the following address – http://localhost:8080. By default, Jenkins listens at the port -8080. Aah! You saw the web-page. I assume that you got the similar page. This tells you that the Jenkins service is running fine.

How to name the local host?

Let’s give a name to the machine. This can be done in a few steps. You just need to change the hostname.

$ sudo gvim /etc/sysconfig/network

Change or add the following – In my example, the name is diamondbuild.com

HOSTNAME=diamondbuild.com

Now the machine name is ‘diamondbuild’. To see the effect, just restart the network and httpd services. You will see that the hostname is changed. If the hostname is changed successfully, you will able to access the url with the server name rather than the local host only. Here, it should be the following url – http://diamondbuild:8080/

Basic Configuration of Jenkins

There are some settings for Jenkins to run comfortably on your machine. Let’s start, and take it with a pinch of a salt.

$ sudo geany /etc/sysconfig/jenkins

This is the main file for jenkins configuration. When the jenkins service is started, it looks for this file to set the environment. This is a place to change the basic configuration. Remember, whenever you change here, you need to restart the Jenkins service.
Generally, we would not like Jenkins to run under Jenkins user-id. Since we have already had a logged-in user-id; we would like to use this user-id rather than Jenkins. For example, my login-id is joshis1; I would like Jenkins service to run under joshis1 user id rather than Jenkins. Here is what I need to do so.

  • Unix user account that runs the Jenkins daemon
  • Be careful when you change this, as you need to update
  • Instead of JENKINS_USER=”jenkins” change it to the following
    JENKINS_USER=”joshis1”   # This is an example based on the user-id joshis1

Note: You need to give a read, write, and executable access to joshis1 for the following folder.
$JENKINS_HOME and /var/log/jenkins

Let’s see what is $(JENKINS_HOME) by searching for this string in the file. JENKINS_HOME=”/var/lib/jenkins”

/** These commands are to give a privilege to the user-id joshis1 **/

$sudo chown -R joshis1 /var/lib/jenkins
$sudo chown -R joshis1 /var/log/jenkins

Remember to restart the Jenkins service to see the effects.

Office-365 Email-Configuration on Jenkins
Java by default supports SSL only. It doesn’t support TLS. These are the protocols that secure the data between the web-browser and the web-server. Both of these are used extensively, and there is a subtle difference between them. But SSL and TLS cannot be interchanged. If SSL is supported, then you cannot use TLS. It holds true vice-versa also. I faced this issue while I was configuring the email-notification on office-365. Thus, you need to pass “TLS enable” parameter to Java. This needs to be done in Jenkins configuration.

# Options to pass to java when running Jenkins.
#JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true" -> comment this line, and paste the following line.
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dmail.smtp.starttls.enable=true"

Firstly, you can download the plugins for the extended email services. Open the browser with the following address – http://diamondbuild:8080/pluginManager/. Alternatively, you can go to manage jenkins – manage plugins. Over there, install the extended email notification plugin. Afterwards, restart the jenkins service. There is a check button over there to restart the jenkins after plugin download. Alternatively, you can manually stop and start the jenkins service. Now, In the URL http://diamondbuild:8080/configure do the following.

  • Register the email-address: System Admin e-mail address: shreyas.joshi@company.com .
  • Setup the Server configuration –  SMTP server: smtp.office365.com
  •  Setup the company email-domain
    Default user e-mail suffix: @company.com
  • Enable the SMTP Authentication –
    Check this one – Use SMTP Authentication
  • User Name – Shreyas_joshi@company.com
    password – xxxxxxxxxxxxxxxxxx

Verify the email-setup on office-365:

 

  • Test the configuration by sending test e-mail (check mark).
  • Test e-mail recipient – Shreyas_joshi@company.com

If you have setup everything correctly, then the email will be send successfully. This tells you that the setup is correct. However, if there is a problem then see the following troubleshooting here.

 Troubleshooting Email-notification settings- Office365:
I) Problem-Description:  Failed to send out e-mail -javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
  Check-list  
1. The TLS service is not set by the jenkins, re-check this flag JENKINS_JAVA_OPTIONS=”-Djava.awt.headless=true -Dmail.smtp.starttls.enable=true” in the file /etc/sysconfig/jenkins. If it is there, then restart the jenkins service. May be you forgot it.
2. In the url, http://diamondbuild:8080/configure. See that you have not enabled – Use SSL check. Also, please don’t give any SMTP port number. Although, you can feed it by seeing the settings in the web portal of microsoftonline – with your login. The first place in fact is to check the settings for your user-id in the portal microsoftonline.com with your credentials. This way, you will get to know whether it is TLS/SSL, and the port number of SMTP.
Problem-Description:  Failed to send out e-mail -com.sun.mail.smtp.SMTPSendFailedException: 550 5.7.1 Client does not have permissions to send as this sender at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057) at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1862)
Check-list  
Did you see that it says that client does not have permissions to send as this sender? You forgot to provide a user-id to the following field. Go the following URL – http://diamondbuild:8080/configure.  System Admin e-mail address: By default, this is some anonymous user-id. Please do change this to the office 365 email-id of yours.

[Note]:  These issues can bug you a lot while setting up the email notification. The problem is because the form doesn’t mandate the fields.  Also, the TLS setting part is a bit tricky.

 Configuration of Build System

  • Go to the URL http://diamondbuild:8080
  • Create a job – Give a name and description of the job. Let’s say I create a job name Diamond-Build/
  • Go the configure page of that job. http://diamondbuild:8080/job/Diamond-Build/configure
  • Go to the section – Build Triggers
  • Go to the section – Build
    Enable the   Execute Shell  check.

    #!/bin/bash

var=$(date '+%B %d')
name=$(echo $var| gawk '{print $1 $2}')
echo $name
mkdir  -p /export/home/joshis1/neutron/$name
pushd  /export/home/joshis1/neutron/$name
export WORKROOT=`pwd`
./neutron_make staging-install-now
./neutron_make all

Troubleshooting Build Configuration:
Problem-Description:

sudo: sorry, you must have a tty to run sudo
      Build step 'Execute shell' marked build as failure
Finished: FAILURE
Check-list
 #visudo
 joshis1 ALL=(ALL) ALL

Comment the line that says – requiretty.
This is the main cause of the issue “sudo: sorry, you must have a tty to run sudo”. When this flag is set, the sudo commands will only run when the user is logged into a real tty”. Here, jenkins is running as a daemon, and it is not associated with tty. Thus, you are getting this issue. In order to solve it, you need to disable requiretty. This might be a security threat, but you can safely live by setting global security of Jenkins. Give a privilege to the user you wish by selecting the matrix option. On the URL: http://diamondbuild:8080/configureSecurity/

Conclusion
Jenkins eases the process of integration. This article is just a tip of the ice berg. There are many salient features of Jenkins – like mobile app – control, automated tests, etc.  However, this is sufficient to automate nightly builds.

References:    http://jenkins-ci.org/

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here