Jenkins Tutorial pt.1 - Installing Jenkins on Ubuntu 18.04 and Setup

Prerequisites:

Install Java 8.

Note the version number! If you directly install the default jdk, it won’t be 8!

1
2
3
sudo apt update
sudo apt install openjdk-8-jdk
java -version

Install Docker

1
2
3
4
5
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable
sudo apt update
sudo apt install docker-ce

Check if it’s running by:

1
sudo systemctl status docker

Installing Jenkins

Now we can finally install Jenkins.

1
2
3
4
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins

Next steps we will start Jenkins and set it up.

Make sure jenkins can execute docker without sudo

Then we have to configure docker so that it is managed by non-root user, and we don’t have to type sudo everytime.
To do this, we create a new group and add the jenkins user.

1
sudo usermod -aG docker jenkins

Start Jenkins

1
2
sudo systemctl start jenkins
sudo systemctl status jenkinsi

If everything is fine, you will see something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
● jenkins.service - LSB: Start Jenkins at boot time
Loaded: loaded (/etc/init.d/jenkins; generated)
Active: active (exited) since Tue 2018-11-27 22:28:53 UTC; 2min 23s ago
Docs: man:systemd-sysv-generator(8)
Tasks: 0 (limit: 1152)
CGroup: /system.slice/jenkins.service

Nov 27 22:28:51 systemd[1]: Starting LSB: Start Jenkins at boot time...
Nov 27 22:28:51 jenkins[11630]: Correct java version found
Nov 27 22:28:52 jenkins[11630]: * Starting Jenkins Automation Server jenkins
Nov 27 22:28:52 su[11676]: Successful su for jenkins by root
Nov 27 22:28:52 su[11676]: + ??? root:jenkins
Nov 27 22:28:52 su[11676]: pam_unix(su:session): session opened for user jenkins
Nov 27 22:28:52 su[11676]: pam_unix(su:session): session closed for user jenkins
Nov 27 22:28:53 jenkins[11630]: ...done.
Nov 27 22:28:53 systemd[1]: Started LSB: Start Jenkins at boot time.

If you are seeing errors related to wrong Java version, check the first step of this article.

Now you can go to http://domain or ip:8080 to see your Jenkins app.
You will need to copy and paste the password to prove that you are the owner of this server.

1
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Now you will be able to log in and create admin users and stuff.
Next I will build a Django + docker project as an example.

Creating our First Pipeline

Now assume I have a project on github. I want to create a pipeline to checkout the source code and do something with it.

First create a new job. In the select panel, select pipeline.
For now, we will head directly to the Pipeline section. In the script part, enter following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pipeline {
agent any
environment {
}
stages {
stage("Hello") {
steps {
sh 'echo "Hello World"'
}
}
stage("Checkout") {
steps {
git branch: 'master',
credentialsId: 'github',
url: 'https://github.com/user123/example.git'
}
}
}
}

Since this is our first test, we will just do something really simple.

We are going to test if the pipeline script works, so I’m just gonna put in echo "Hello World" in shell script. If everything works well, we will see this in console logs.

Next is checking out source code.
In this case, I am using github, so I have to input my credentials for github in jenkins > Credentials > Global > Add Credentials.
Choose the one with username and password, input your github credentials, and give it ID github.
Remember, if you are using the username and password authentication, you have to use the HTTPS url for
github instead of the SSH one. The credentialsId in script is the ID that we have for credentials.

Now save and go back to the item page, and click “Build Now”. If the grid box turns green, it mean your pipeline has successfully built and run.

Congratulations! You’ve built your first Jenkins pipeline! Now move on to the 2nd part of this tutorial.