How to install and Configure Jenkins in Ubuntu 22.04

In this guide we are going to learn how to install and set up Jenkins in Ubuntu 22.04 server.

Jenkins is an open source automation server which enables developers around the world to reliably build, test, and deploy their software. It is a popular open source automation tool to perform continuous integration and build automation. Jenkins allows to execute a predefined list of steps, e.g. to compile golang source code to build build binary file. The trigger for this execution can be time or event based.

Possible steps executed by Jenkins are for example:

  • Cloning the code from source control system.
  • execute a command to install dependencies required for the code to run
  • Run tests for the software
  • build the software to get an executable
  • Publish test results
  • publish the resulting binary code

Jenkins monitors the execution of the steps and allows to stop the process, if one of the steps fails. Jenkins can also send out notifications in case of a build success or failure.

Jenkins can be extended by additional plug-ins. For example, you can install plug-ins to support building and testing Android applications.

Requirements

  • A Linux system with Ubuntu 22.04 server (At least 2GB of Ram)
  • A user account with sudo or root privileges
  • Access to a terminal window/command line
  • Recent Java version installed

Table of content

  1. Ensuring that the system is up to date
  2. Install Java
  3. Install Jenkins
  4. Starting and enabling jenkins
  5. Accessing Jenkins
  6. Optional: Accessing jenkins using a domain

1. Ensuring that the system is up to date

Before proceeding, ensure that your system has updated packages. Use this command to achieve this:

sudo apt update
sudo apt upgrade -y

2. Install Java

Jenkins requires Java 11 to run. We will have to install and ensure that java is working as expected. Type this command to check the java version

java -version

If that command results in an error, you need to install java runtime env.

sudo apt install -y default-jre

Once installed, confirm

 $ java -version
openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

3. Install Jenkins

This is the Debian package repository of Jenkins to automate installation and upgrade. To use this repository, first add the key to your system:

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \
    /usr/share/keyrings/jenkins-keyring.asc > /dev/null

Then add a Jenkins apt repository entry:

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
    https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
    /etc/apt/sources.list.d/jenkins.list > /dev/null

Update your local package index, then finally install Jenkins:

sudo apt update
sudo apt install fontconfig openjdk-11-jre
sudo apt-get install jenkins

4. Starting and enabling jenkins

Ensure that jenkins is tunning as extected by checking the systemd service status

sudo systemctl status jenkins

You should see something like this:

$ sudo systemctl status jenkins
● jenkins.service - Jenkins Continuous Integration Server
     Loaded: loaded (/lib/systemd/system/jenkins.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-06-16 13:47:24 UTC; 36s ago
   Main PID: 15980 (java)
      Tasks: 47 (limit: 4623)
     Memory: 1.2G
        CPU: 53.704s
     CGroup: /system.slice/jenkins.service
             └─15980 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080

Jun 16 13:47:03 ip-10-2-40-138 jenkins[15980]: This may also be found at: /var/lib/jenkins/secrets/initialAdminPassword
Jun 16 13:47:03 ip-10-2-40-138 jenkins[15980]: *************************************************************
Jun 16 13:47:03 ip-10-2-40-138 jenkins[15980]: *************************************************************
Jun 16 13:47:03 ip-10-2-40-138 jenkins[15980]: *************************************************************
Jun 16 13:47:24 ip-10-2-40-138 jenkins[15980]: 2022-06-16 13:47:24.297+0000 [id=30]        INFO        jenkins.InitReactorRunner$1#onAttained: Completed initialization
Jun 16 13:47:24 ip-10-2-40-138 jenkins[15980]: 2022-06-16 13:47:24.331+0000 [id=22]        INFO        hudson.lifecycle.Lifecycle#onReady: Jenkins is fully up and running
Jun 16 13:47:24 ip-10-2-40-138 systemd[1]: Started Jenkins Continuous Integration Server.
Jun 16 13:47:24 ip-10-2-40-138 jenkins[15980]: 2022-06-16 13:47:24.844+0000 [id=45]        INFO        h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenIns>
Jun 16 13:47:24 ip-10-2-40-138 jenkins[15980]: 2022-06-16 13:47:24.845+0000 [id=45]        INFO        hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1
Jun 16 13:47:24 ip-10-2-40-138 jenkins[15980]: 2022-06-16 13:47:24.851+0000 [id=45]        INFO        hudson.model.AsyncPeriodicWork#lambda$doRun$1: Finished Download metadata. 22,265 ms

Enable jenkins on boot

sudo systemctl enable jenkins

5. Accessing Jenkins

Access the jenkins server using the public ip on port 8080. If you are accessing the jenkins server locally, use http://localhost:8080

Get the jenkins public ip

curl -4 icanhazip.com

You can then navigate to http://public_ip:8080

6. Optional: Accessing jenkins using a domain name

If you want to access jenkins using a domain name, you can use something like nginx to proxy traffic. Nginx allows you to proxy the traffic and terminate ssl if need be.

Install nginx

sudo apt install -y nginx

Ensure that nginx is running

sudo systemctl status nginx

Create an nginx server for jenkins in /etc/nginx/conf.d with the content to serve:

File /etc/nginx/conf.d/jenkins.conf

server {
    listen 80;
    listen [::]:80;
    server_tokens off;
    server_name your.domain.com;

    ## Deny illegal Host headers
    if ($host !~* ^(your.domain.com)$ ) {
        return 444;
    }

    location / {
          proxy_pass http://127.0.0.1:8080;
          proxy_set_header   Host $host;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Host $server_name;
          proxy_set_header   X-Real-IP $remote_addr;
          proxy_set_header   X-Scheme $scheme;
          proxy_connect_timeout 60s;
          proxy_send_timeout 60s;
          proxy_read_timeout 60s;
    }
}

Restart nginx:

sudo systemctl restart nginx

Map the A record of the domain your.domain.com to the IP address of your server and you should be able to access it using http://your.domain.com

7. Configuring jenkins

The first time you access the jenkins server using the domain, you will be greeted with a getting started screen asking you to Unlock jenkins with an admin password from this path /var/lib/jenkins/secrets/initialAdminPassword in the server.

Get the pasword by typing this

cat /var/lib/jenkins/secrets/initialAdminPassword

Citizix - Jenkins install unlock

Citizix – Jenkins install unlock

Upon entering the password, you will be taken to plugin install page. You have an option of installing common plugins or choosing what plugins to install.

I always prefer to custom pick plugins I want installed so I have control on what plugins are installed and what not.

Citizix - Cuztomize jenkins

Citizix – Cuztomize jenkins

Once the plugin installation is done, you will be redirected to a page to create Jenkins First User. I always create an Admin user then choose a strong password.

Citizix - Jenkins Create First Admin User

Citizix – Jenkins Create First Admin User

After that Ensure the correct URL is set in the last page of the instance configuration page. After this you will be redirected to the jenkins dashboard page.

From here you can Create jenkins jobs and use jenkins and use it to build then.

Last updated on Oct 14, 2024 11:46 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy