Complete Guide to Learning Jenkins on RHEL

Install Jenkins on RHEL 


Prior to installation of Jenkins on a RHEL Linux machine, we should take care of all the additional software packages that come with the base operating system. If the additional software like development tools and its libraries are not installed we end up with glibc, libc, libjvm and other library issues. If the additional softwares is not installed we need to add the CD into the disk and install them. After installing the software we need to add the repository files into /etc/yum.repos.d file by adding the appropriate .repo files pointing to the file server where packages are present.


Install Jenkins on RHEL


Download Jenkins Repo : Add the repository file for jenkins using wget to make sure the machine is connected to the internet to download the jenkins.repo file.

wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import http://pkg.jenkins.io/redhat-stable/jenkins.io.key

Install Jenkins : Install OpenJDK and Jenkins software packages and configure the firewall to enable the tcp and http port numbers and refresh the firewall

yes | yum install java-1.8.0-openjdk
yes | yum install jenkins 
firewall-cmd --zone=public --add-port=8080/tcp --permanent 
firewall-cmd --zone=public --add-service=http –permanent 
firewall-cmd --reload

Start Jenkins : Start the Jenkins server and enable the services of jenkins, we can check the status of jenkins using service or ps -ef command as shown below

systemctl start jenkins
systemctl enable jenkins
service jenkins status
ps -ef | grep jenkins
jenkins     3178       1  1 03:41 ?        00:03:10 /etc/alternatives/java -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20

Manually start Jenkins : If possible we can also start the jenkins server manually using below command or else we can start the jenkins server in the background.


# java -jar /usr/lib/jenkins/jenkins.war --httpPort=8080
# bg
# disown -h %ABOVE_JOBID

(or)

# nohup java -jar jenkins.war > $LOGFILE 2>&1

Authenticating and Launching Dashboard : Once done login to the server using http://localhost:8080 or http://ip_address:8080  or http://domain_name:8080 and follow the procedure of configuring the jenkins server its simple as copying a key from the secret file

/var/lib/jenkins/secrets/initialAdminPassword

and entering into the GUI screen. After the login jenkins server will ask you for updating the required packages click yes and update and it will login to the Jenkins dashboard.






Create New Jenkins Python Project Example that takes Parameter

 
For creating a new project in Jenkins go to Jenkins dashboard and select New and then provide the project name and select free style project save. After creating the new project, build the project once so that the Jenkins project workspace gets created in “/var/lib/Jenkins/workspace” ; this is the default Jenkins project workspace location.


jenkins create project


Jenkins Project Workspace Directory

The project directory gets created in “/var/lib/Jenkins/workspace” path. Once we create a new project and build it will create the directory with the same name as project name given in Jenkins Dashboard Console. We have two options: either upload the python program files using Jenkins GUI or we can copy them using SCP. 


jenkins project configure



Copy the below python project code where it takes arguments as hostname, username and user password, logs in to the remote machine using ssh paramiko and then lists the file present in home directory.



[root@jenkins_server listfilesystem]# cat list_files.py 
#!/usr/bin/python3 

import sys
import paramiko
import time

def list_files(host, user, passwd):
    connection_handle = paramiko.client.SSHClient()
    connection_handle.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        connection_handle.connect(host, username=user, password=passwd)
    except Exception as e:
        print("SSH Authentication Failed : ",e)
        return -1
    stdin, stdout, stderr = connection_handle.exec_command("ls -l", get_pty=True)
    while not stdout.channel.exit_status_ready():
        time.sleep(10)
    for each_line in iter(stdout.readline, ""):
        print(each_line, end="")
    exit_status = stdout.channel.recv_exit_status()
    connection_handle.close()
    return exit_status
    
exit(list_files(host=sys.argv[1], user=sys.argv[2], passwd=sys.argv[3]))
[root@jenkins_server listfilesystem]#


Testing the python code in the workspace directory using the command line interface. As our python code uses the paramiko python library if the library is not present install it using “pip install paramiko” and if pip is not installed by default use command “python3 -m pip install -U pip” to install pip.


[root@jenkins_server listfilesystem]# /usr/bin/python3 list_files.py testhost testuser *********
total 0
-rw-r--r--    1 testuser staff             0 Apr 09 12:43 file1
-rw-r--r--    1 testuser staff             0 Apr 09 12:43 file2
-rw-r--r--    1 testuser staff             0 Apr 09 12:43 file3
[root@jenkins_server listfilesystem]# 
[root@jenkins_server listfilesystem]# pwd
/var/lib/jenkins/workspace/listfilesystem
[root@jenkins_server listfilesystem]#


Jenkins configuration for Python Project

Now in order to configure the above created project select configure as shown below and add got to general click add parameter. Our project is to take the hostname, username and password as input and list the files present in the file system of the remote machine. Add string parameter for both hostname and username and user password as password type parameter as in below screenshot.

 

set parameters



configure build as shell


Build Python Project in Jenkins

After we set the parameters, we should configure the build details, select build option, then add build step and Execute Shell. Add the command “/usr/bin/python3 -u list_files.py ${hostname} ${username} ${userpass}” in the provided command execution field. Click Save.


run jenkins job by selecting build with parameters


Once done go to Jenkins Dashboard select project build with parameters. Set the hostname, username and user password and then run the project. Select the run from the left bottom and go to the console output you should see the result.




Post a Comment

0 Comments