Getting Started With Jenkins(Part-1)

Updated: Sep 1, 2020











What Is The Concept of CI/CD ?


This is probably the most common term in the devops world, CI/CD stands for Continuous Integration and Continuous Delivery.


Let's discuss about how development used to happen before CI/CD came in to picture. Earlier a base working code was developed with some of the functionalities that were good to go with.


Different teams (back-end, front-end etc.) used to download the code from the repository and started working with their own portion.Once they all are done with their own parts , integration of code will happen.


The integration area is where the real problem lies because of the teams didn't communicate with each other so there was lack of transparency.

Only the development team had an idea of what logic is being used and what methods need to be exposed but no one tested these before.


So problems used to occur at the time of integration and it took longer to get the project completed.


The relief to the above situation was CI/CD.





Here developers commit their code in to the repository very often. Each Commit has some improvements or additional code. Each commit triggers an automated workflow on a CI server(Jenkins) that can notify developers of any failure or success of the integration. This way, the code is deployed in small increments, tested and ultimately put to production which was far more productive.


Continuous Delivery is a discipline where software is developed in such a manner that allows to deploy code in production anytime. Please note, this doesn't mean that code has to be deployed but it makes it feasible for the deployment to happen in production if required.


This gives us a possibility of continuously getting feedback regarding code deployment, code quality and a way to trigger deployment in production if everything is fine in dev environment.


Jenkins Installation


Jenkins is a free and open source automation server. It helps automate the parts of software development related to building, testing, deploying, facilitating continuous integration and continuous delivery.


To install jenkins we will use 2 CentOS 7 machines(64 bits)


192.168.1.11 node1 # Jenkins Master

192.168.1.12 node2 # Jenkins Slave

  • On Jenkins master (node1), login with root and follow the below steps

[root@node1 ~]# pwd
/root
[root@node1 ~]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
--2020-06-08 11:36:21--  https://pkg.jenkins.io/redhat-stable/jenkins.repo
Resolving pkg.jenkins.io (pkg.jenkins.io)... 151.101.154.133, 2a04:4e42:24::645
Connecting to pkg.jenkins.io (pkg.jenkins.io)|151.101.154.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 85
Saving to:/etc/yum.repos.d/jenkins.repo’

100%[===============================================================================================================>] 85          --.-K/s   in 0s

2020-06-08 11:36:21 (5.75 MB/s) -/etc/yum.repos.d/jenkins.repo’ saved [85/85]

[root@node1 ~]# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
[root@node1 ~]# yum install jenkins -y
  • Note that jenkins user will be created with a no login shell.

  • Execute below command to change the shell of jenkins user to /bin/bash so that jenkins is able to take the console.

[root@node1 ~]# chsh -s /bin/bash jenkins
Changing shell for jenkins.
chsh: Shell changed.

  • Give sudo access to jenkins user. For this append the below line to /etc/sudoers file.

 jenkins ALL=(ALL)       NOPASSWD: ALL
  • Reset password for jenkins user

[root@node1 ~]# passwd jenkins
Changing password for user jenkins.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
  • Install java

yum install java-1.8.0-openjdk.x86_64 -y 
  • Now login with jenkins user and start jenkins service.

[root@node1 ~]# su - jenkins
Last login: Mon Jun  8 11:49:43 IST 2020 on pts/1
-bash-4.2$ sudo systemctl start jenkins && sudo systemctl enable jenkins && sudo systemctl status jenkins
jenkins.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig jenkins on
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: active (running) since Mon 2020-06-08 11:53:07 IST; 178ms ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/jenkins.service
           └─25205 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jen...

Jun 08 11:53:06 node1.example.com systemd[1]: Starting LSB: Jenkins Automation Server...
Jun 08 11:53:06 node1.example.com runuser[25180]: pam_unix(runuser:session): session opened for user jenkins by (uid=0)
Jun 08 11:53:07 node1.example.com runuser[25180]: pam_unix(runuser:session): session closed for user jenkins
Jun 08 11:53:07 node1.example.com systemd[1]: Started LSB: Jenkins Automation Server.
Jun 08 11:53:07 node1.example.com jenkins[25175]: Starting Jenkins [  OK  ]

  • Access the jenkins console on http://node1:8080 and follow the instructions.

  • Use the default one time password to login.

  • Install Suggested Plugins


  • Now we will create our first admin user as shown below

Our First Jenkins Job


We can do a lot of things with jenkins but just to start the show we will create a freestyle project.After that install Apache on the server and run a small web page.

  • Click on create new jobs

  • Give a name to the job and select “FreeStyle project”

  • Now moving forward , give a description, select source code management as None

  • Under “Build Step” select “Execute shell”

Under “Execute Shell” , write this small bash script.

#!/usr/bin/bash
sudo echo "This is a webpage deployed by Jenkins" >> /tmp/index.html
sudo yum install httpd -y
sudo cp /tmp/index.html /var/www/html/
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl status httpd
curl localhost
  • Save and Apply.

  • After that click on build now on the left hand side of the page.The build will run and console output will look like below.

Lets put a full stop to this article here. In the next part, we will study some jenkins basic terminologies and we will add a jenkins slave machine to distribute the workload.






799 views2 comments

Recent Posts

See All
 

Subscribe Form

©2020 by Linux Advise