Jenkins Introduction

jenkins-docker-logo

  • Jenkins is an open-source continuous integration and continuous delivery tool written in Java.
  • It's an automation server used to build and deliver software projects.
  • The major benefit of using Jenkins is that it has a lot of plugins available.
  • There is easier to use CI software available, but Jenkins is open source, free and is still very popular.

Continuous integration

Continuous integration, CI, is the practice in software engineering of merging all developer working copies to shared mainline several times a day.

If you're familiar with Git, you will see that continuous integration is just integrating your own code together with the rest of the projects.

This can be in the master branch, develop branch, or even in your own branch.

Continuous delivery

Continuous delivery, on the other side, abbreviated as CD, is a software engineering approach in which teams produce software in short cycles, ensuring that the software can be reliably released at any time.

Jenkins can really help you to release software in short cycles and then ensure it's reliability because we are going to test and build the software over and over again.

In practice, CI and CD means that we are going to verify and publish work by triggering automated builds and tests. And Jenkins is a tool that we can use for that.

We are going to do these automated builds and tests for every commit, or at least once a day.

All developers should push their changes to version control, which should then be built and tested, which can be done by Jenkins.

Jenkins doesn't merge code, nor it resolves code conflicts.

Benefits.

Jenkins provides a feedback loop back to the developer to fix the build errors.

It is a lot quicker to have a developer fix the error immediately rather than wait with it because it's then when the code is still fresh in memory.

Jenkins can publish every build of your software. Every time someone commits a change, Jenkins can build and publish this piece of software.

This build already has gone through automated testing, you can configure this in Jenkins. Then, Jenkins will test whether the code is good, whether the features the developer implemented actually work and then when published and deployed to development, a QA or staging server, you can advance the software development life cycle, the SDLC, much quicker.

The quicker you can go through an iteration of the SDLC the better.

Install CentOS for Jenkins

Ignore the centos installation if you are not working in Windows Operating System)
  • Install the VirtualBox
  • Download a centos iso
  • Add centos to VirtualBox by clicking on New icon
    vm-add-jenkins
  • Provide the details like name and the Operating system details; here we are choosing Linus and RedHat(64) as we do not have Centos; also select the location where you want this VMs to be present
    name-vm-operating-system
  • Press Next button and Select 2 GB of RAM
  • Press Next button and select Create Virtual hard Disk Now
  • Press Create button
  • Keep default for Hard disk file type and Storage on Physical hard disk
  • Drag the pointer to 20 Gb for size and Press Create
    new-vm-for-jenkins
  • Go to Machine>Settings>Network and select below settings
    vm-network-setting-jenkins
  • Click on the Start Icon
    start-vm-jenkins
  • Click the folder icon for select startup disk
    select-iso-file-centos-jenkins
  • Select the downloaded Centos and Click Start button
    centos-for-jenkins
  • Note: Some people who are using windows might face below error
    Call to WHvSetupPartition failed: ERROR_SUCCESS (Last=0xc000000d/87) (VERR_NEM_VM_CREATE_FAILED).​
  • Follow the below steps to solve it. (Please do RESTART TWICE)
    1. Go to Type here to search
    2. Type into the search textbox Turn Windows features on or off
    3. Make sure both Hyper-V and Windows Sandbox are unchecked/disabled
    4. If the system requires a restart, perform the restart.
    5. Restart a second time to ensure all Hyper-V features are disabled​
  • Select the Install CentOS and wait for few minutes
    install-centos-vm-jenkins
  • Select the language
  • Select Minimal under Software, because we just need Terminal.
  • Select the Network option and Turn on the network
    turn-on-network-connection
  • Press Begin Installation button and create user & password if required ( may take up to 45mins)
  • You will see the below page if the installation is successful. Close the window ( Donot reboot)
    reboot-vm-centos-jenkins-oracle
  • In virtual Box, Under setting remove the iso file otherwise it keeps installing every time
    remove-iso-vm-jenkins

  • Accept the License and Finish Installation.
    login-vm-jenkins

Connect VM with Putty

We will be using putty hereafter, so let get the Ip address from the VM. We are using putty rather than VM because managing things from Putty is actually pretty simple and it's even easier.

  • Get the IP address using ip a command
    ip-address-vm-centos-command-jenkins

  • Download Putty x84 or x64 according to your system. and enter the IP address that you got from the VM. And click on Open Button
    putty-jenkins-vm

  • Now use the user name and password same as VM
    putty-vm-connection-jenkins

Install Docker for Jenkins

If below steps become outdated then you refer to official docs : https://docs.docker.com/engine/install/centos/#install-using-the-repository

Installation Steps of Docker CE on CentOS 8/RHEL 8 Server

Step:1) Enable Docker CE Repository
As we said above, docker packages are not available any more on CentOS 8 or RHEL 8 package repositories, so run following dnf command to enable the Docker CE package repository.

[[email protected] ~]# dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Step:2) Install Docker CE using dnf command.

Once the docker ce repo has been configured successfully then run following command to verify which version of docker is available for installation

[[email protected] ~]# dnf list docker-ce
Available Packages
docker-ce.x86_64              3:19.03.5-3.el7                    docker-ce-stable

Now, use beneath dnf command to install the latest version of docker,

[[email protected] ~]# dnf install docker-ce --nobest -y

After the installation of docker, start and enable its service using the following systemctl commands

[[email protected] ~]# systemctl start docker
[[email protected] ~]# systemctl enable docker

Run the following command to verify installed docker version

[[email protected] ~]# docker --version
Docker version 18.06.3-ce, build d7080c1
[[email protected] ~]#

Step:3) Verify and test Docker CE Engine
To verify whether docker ce engine has been set up correctly or not, try to spin up a "hello-world" container using below docker command,

[[email protected] ~]# docker run hello-world

Output of above command
hello-world-docker-jenkins

Install docker compose:

We will need docker compose, we an install the docker compse using

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

doker-compse-installation

You have to provide executable access to docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose-commands

Install Jenkins on CentOS with Putty

Let's install the Jenkins image on docker, so let's pull the Jenkins image from docker repo. You can find the Jenkins image on https://hub.docker.com/r/jenkins/jenkins/

# jenkins image pull command
docker pull jenkins/jenkins

Once the Jenkins image is installed you can see where it is located using the following command.

[[email protected] ~]# docker info | grep -i root
# output
 Docker Root Dir: /var/lib/docker

Create Docker-compose file for Jenkins:

We have installed jenkins but we have not started the jenkins service. To start jenkins we need to configure the docker-compse file. docker compose file helps to store the data and to specify ports and network for the jenkins service.

When a docker image is destroyed all the data in also will be destoryed, but we need to store the data before destrying the docker image. So using docker-compose file we can specify where we want to save the data.

The explanation for Jenkins docker-compose file:

Let's create a folder to store all the Jenkins related stuff and open it.

  • mkdir to create a folder
  • cd to open the folder
  • pwd to see the present working directory
[[email protected] ~]# mkdir jenkins-data
[[email protected] ~]# ls
anaconda-ks.cfg  jenkins-data
[[email protected] ~]# cd jenkins-data/
[[email protected] jenkins-data]# pwd
/root/jenkins-data
[[email protected] jenkins-data]#

Now e need to store the data from the jenkins image, to store it we have to create another folder insie the jenkins-data . Also we will be using this folder inside our docker-compse file.

[[email protected] jenkins-data]# mkdir jenkins_home
[[email protected] jenkins-data]#

Create file docker-compose.yml inside jenkins-data folder and put the below content. Use spaces and not tabs

version: '3.7'
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    ports:
      - "8080:8080"
    volumes:
      - $PWD/jenkins_home:/var/jenkins_home
    networks:
      - net
networks:
  net:
  • version : compose file version; version is very important and it should be compatible with docker-compose version
  • services : what are the services this file will trigger
  • jenkins : triggers jenkins service
  • container_name : can give any name to container which holds the jenkins
  • image : docker images name for jenkins, you can get it if you type docker images command
  • ports : which port the jenkins should be accessed from
  • 8080:8080 : map the 8080 of the VM machine with the 8080 port of the docker jenkins images
  • volumes : where the files should be stored and from where
  • $pwd/jenkins_home:/var/jenkins_home : $pwd/images-data where the data should be stored in VM, and should be copied from /var/jenkins_home of docker jenkins images.
  • networks : which networks to use
  • - net : use network called net defined in the file
  • (outside) networks : list of networks we provide
  • (outside) net : the network details but we have left blank, so it will use the default.

if you list the directory; you should see below items

[[email protected] jenkins-data]# ls
docker-compose.yml  jenkins_home
Start the jenkins service using the docker-compose:

Once we create the docker-compose, we can create/start the Jenkins service.

Before we start we need to give permission to the user to write data to the jenkins_home folder

[[email protected] jenkins-data]# sudo chown 1000:1000 jenkins_home -R

Start the jenkins:

[[email protected] jenkins-data]# docker-compose up -d
Creating network "jenkins-data_net" with the default driver
Creating jenkins ... done

Check whether docker service up or not using docker ps command
jenkins-started-vm-centos

Now let's get the authentication key; we need authentication for the UI, to get authentication keys; let's open the logs of Jenkins container.

Syntax
#docker logs -f container_name
# f - full and continue to show logs
docker logs -f jenkins

authentication-key-jenkinsvm-centos-docker

To terminate the log continuation press CTRL+C

Docker Jenkins UI

Get your VM IP address by ip a and use the port as 8080 as defined in docker-compose.yml file.
unlock-jenkins-docker-vm

Use the copied key to unlock the Jenkins and Install Suggested Plugins
install-selected-plugins-docker
plugin-installation-jenkins-docker

Create admin
create-admin-jenkins-docker

Save and Continue then Finish.
jenkins-start-page-docker

Tada, You are at END and Installation is successful.

Jenkins Installation Errors

Docker Commands for Jenkins

  • Set Up the docker
    docker-compse up -d​
  • Stop the docker
    docker-compose stop​
  • Start the docker
    docker-compose start​
  • Remove jenkins but keep the data
    docker-compose down​
  • Setup the docker from existing files
    docker-compose up -d​
  • Terminate jenkins service
    docker kill jenkins​
  • List all docker services that are running
    docker ps​
  • List all the downloaded docker images
    docker images​
  • Remove the downloaded docker images

    #docker rmi ImageName
    docker rmi jenkins/jenkins
  • Delete all the files of Jekins
    rm -rf folderName​
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions