Contents

Install Jenkins on Centos7

Overview

/images/jenkins.png

Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.

Jenkins can be installed through native system packages, Docker, or even run standalone by any machine with a Java Runtime Environment (JRE) installed (Jenkins.io).

Prerequisites

  • Vagrant, see how to install Vagrant here
  • Add the following host entries on local device /etc/hosts :
    1
    2
    
    192.168.0.1 jenkins.local
    192.168.1.1 agent.local
    

Startup Vagrant

Create Vagrantfile and fill the following code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Vagrant.configure(2) do |config|
    config.vm.box = "bento/centos-7"
  
    config.vm.define "jenkins" do |jenkins|
      jenkins.vm.network "private_network", ip: "192.168.0.1", name: 'vboxnet0'
      jenkins.vm.hostname = "jenkins.local"
      jenkins.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", disabled: true
      jenkins.vm.network :forwarded_port, guest: 22, host: 2230, auto_correct: true
      jenkins.vm.network :forwarded_port, guest: 8080, host: 8080, auto_correct: true
      jenkins.ssh.port = 2230
      jenkins.vm.provider "jenkins" do |vb|
        vb.cpus = 1
        vb.memory = 1024
      end
    end

    config.vm.define "agent" do |agent|
      agent.vm.network "private_network", ip: "192.168.1.1", name: 'vboxnet1'
      agent.vm.hostname = "agent.local"
      agent.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", disabled: true
      agent.vm.network :forwarded_port, guest: 22, host: 2231, auto_correct: true
      agent.ssh.port = 2231
      agent.vm.provider "agent" do |vb|
        vb.cpus = 1
        vb.memory = 1024
      end
    end
  end

And start vagrant by typing vagrant up on your terminal.

Install Jenkins

SSH into jenkins machine, you can use vagrant ssh jenkins or ssh from host ssh -p 2230 [email protected] -i .vagrant/machines/jenkins/virtualbox/private_key

Login as root sudo su -

Install wget yum install -y wget

Create jenkins limit file /etc/security/limits.d/30-jenkins.conf

1
2
3
4
5
6
7
8
jenkins soft core unlimited
jenkins hard core unlimited
jenkins soft fsize unlimited
jenkins hard fsize unlimited
jenkins soft nofile 4096
jenkins hard nofile 8192
jenkins soft nproc 30654
jenkins hard nproc 30654

Setup firewall

1
2
3
4
5
6
7
systemctl start firewalld

firewall-cmd --permanent --add-port=22/tcp
firewall-cmd --permanent --add-port=8080/tcp

firewall-cmd --reload
firewall-cmd --list-all

We should get an outuput

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1
  sources: 
  services: dhcpv6-client ssh
  ports: 22/tcp 8080/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

Add the following entries to /etc/hosts

1
2
192.168.0.1 jenkins.local
192.168.1.1 agent.local

Add AdoptOpenJDK repository

1
2
3
4
5
6
7
8
cat <<'EOF' > /etc/yum.repos.d/adoptopenjdk.repo
[AdoptOpenJDK]
name=AdoptOpenJDK
baseurl=http://adoptopenjdk.jfrog.io/adoptopenjdk/rpm/centos/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public
EOF

Add repository to get latest Git

1
yum -y install https://packages.endpoint.com/rhel/7/os/x86_64/endpoint-repo-1.9-1.x86_64.rpm

Add jenkins repository

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

Create directory

1
2
mkdir -p /var/cache/jenkins/tmp
mkdir -p /var/cache/jenkins/heapdumps

Uninstall old Git by typing yum remove git* on your terminal

Install AdoptOpenJDK, Git, Jenkins and Fontconfig

1
yum -y install adoptopenjdk-11-hotspot git jenkins fontconfig

Edit the /etc/sysconfig/jenkins file

1
2
3
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Djava.io.tmpdir=/var/cache/jenkins/tmp -Dorg.apache.commons.jelly.tags.fmt.timeZone=Asia/Jakarta -Duser.timezone=Asia/Jakarta"

JENKINS_ARGS="--pluginroot=/var/cache/jenkins/plugins"

Change owner jenkins config file chown -R jenkins:jenkins /var/cache/jenkins

Start jenkins by typing systemctl start jenkins and get status systemctl -l status jenkins

We should get an output status.

1
2
3
4
5
6
7
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: active (running) since Sun 2021-08-01 01:06:57 UTC; 16s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3697 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/jenkins.service
           └─3718 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -Djava.io.tmpdir=/var/cache/jenkins/tmp -Dorg.apache.commons.jelly.tags.fmt.timeZone=Asia/Jakarta -Duser.timezone=Asia/Jakarta -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20 --pluginroot=/var/cache/jenkins/plugins

Setup Jenkins UI

Access http://jenkins.local:8080 on your host and should be like below.

/images/jenkins.local-ui-1.png

Type sudo cat /var/lib/jenkins/secrets/initialAdminPassword to get password and paste it on field then continue.

Install suggested plugin

/images/jenkins.local-ui-2.png

Create first admin user.

/images/jenkins.local-ui-3.png

Setup jenkins url and click start using jenkins.

/images/jenkins.local-ui-4.png

And restart jenkins then login with admin user http://jenkins.local:8080/restart

Agent Installation

SSH into jenkins machine, you can use vagrant ssh agent or ssh from host ssh -p 2231 [email protected] -i .vagrant/machines/agent/virtualbox/private_key

Login as root sudo su -

Setup firewall

1
2
3
4
5
6
systemctl start firewalld

firewall-cmd --permanent --add-port=22/tcp

firewall-cmd --reload
firewall-cmd --list-all

We should get an outuput

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1
  sources: 
  services: dhcpv6-client ssh
  ports: 22/tcp 8080/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

Add the following entries to /etc/hosts

1
2
192.168.0.1 jenkins.local
192.168.1.1 agent.local

Add AdoptOpenJDK repository

1
2
3
4
5
6
7
8
cat <<'EOF' > /etc/yum.repos.d/adoptopenjdk.repo
[AdoptOpenJDK]
name=AdoptOpenJDK
baseurl=http://adoptopenjdk.jfrog.io/adoptopenjdk/rpm/centos/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public
EOF

Add repository to get latest Git

1
yum -y install https://packages.endpoint.com/rhel/7/os/x86_64/endpoint-repo-1.9-1.x86_64.rpm

Uninstall old Git by typing yum remove git* on your terminal

Install AdoptOpenJDK, Git, Fontconfig and Wget

1
yum -y install adoptopenjdk-11-hotspot git fontconfig wget

Install docker and unzip from Install Docker Engine on CentOS

  • Remove old docker if any yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
  • yum -y install yum-utils
  • yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  • yum -y install docker-ce docker-ce-cli containerd.io unzip
  • systemctl enable docker
  • systemctl start docker
  • groupadd docker
  • systemctl -l status docker
  • exit
  • sudo usermod -aG docker $USER
  • exit

SSH again to vagrant agent

1
ssh -p 2231 [email protected] -i .vagrant/machines/agent/virtualbox/private_key

Try running container by typing docker run hello-world on your terminal.

Installing maven

  • sudo su -
  • mkdir -p /opt/tools/maven
  • cd /opt/tools/maven
  • wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
  • tar zxvf apache-maven-3.6.3-bin.tar.gz
  • rm -f apache-maven-3.6.3-bin.tar.gz
  • ln -s apache-maven-3.6.3 latest

Installing gradle

  • mkdir -p /opt/tools/gradle
  • cd /opt/tools/gradle
  • wget https://services.gradle.org/distributions/gradle-7.1.1-bin.zip
  • unzip gradle-7.1.1-bin.zip
  • rm -f gradle-7.1.1-bin.zip
  • ln -s gradle-7.1.1 latest

Export maven and gradle to profile

  • echo "PATH=/opt/tools/gradle/latest/bin:\$PATH" > /etc/profile.d/gradle.sh
  • echo "PATH=/opt/tools/maven/latest/bin:\$PATH" > /etc/profile.d/maven.sh
  • chown -R vagrant:vagrant /opt/tools
  • exit
  • exit

Verify that maven and gradle has already installed on your machine.

  • ssh -p 2231 [email protected] -i .vagrant/machines/agent/virtualbox/private_key
  • mvn --version
  • gradle --version

Connect Agent to Jenkins

Manage jenkins at system configuration.

/images/jenkins.local-ui-5.png

And fill field like below

/images/jenkins.local-ui-6.png

Add nodes and fill like below

/images/jenkins.local-ui-7.png

/images/jenkins.local-ui-8.png

Add credential

/images/jenkins.local-ui-9.png

Click advanced and set port to 2231 then click save.

/images/jenkins.local-ui-10.png

Create Test Job (Pipeline)

Create new pipeline then save

/images/jenkins.local-ui-11.png

Create build script to running jobs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
pipeline {
  agent {label "linux"}
  stages {
    stage("Hello") {
      steps {
        sh """
          mvn --version
          gradle --version
          docker info
        """
      }
    }
  }
}

You can see my example on Github

References

Jenkins.io - Jenkins User Documentation

CloudBeesTV - How To Install Jenkins on CentOS 7

RedHat - Continuous Delivery to JBoss EAP and OpenShift with the CloudBees Jenkins Platform

CloudBees - Prepare Jenkins for Support

Jenkins - Jenkins Redhat Packages

Docker - Install Docker Engine on CentOS

Docker - Post-installation steps for Linux

AdoptOpenJDK - Installation