CI/CD with Jenkins — Part 1: Install Jenkins on Ubuntu

By Liptan Biswas, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

Jenkins is an open-source self-hosted automation server. It is very popular amongst the DevOps engineers for implementing continuous integration and continuous delivery in the software development life cycle. Jenkins is written in Java and provides out of the box support for building Apache Ant, Maven, and sbt projects. It can also execute Linux shell and Windows batch scripts. Multiple version control systems such as Git, Mercurial, Subversion, CVS, etc. are fully supported by Jenkins. At least a thousand plugins are available to increase the functionality of the application.

A build in Jenkins can be triggered via several methods such as, after detection of a change in the source code of version control system, or scheduling the build at a specific time, or by invoking a build URL. Once the build is triggered, Jenkins fetches the source code from the repository and the build is started. According to configuration supplied, automatic tests are performed and the output is stored. Jenkins is also capable of delivering the packages generated after a successful build.

This three-part tutorial will guide you through the installation of Jenkins and use it for continuous integration and deployment.

In this first part of the tutorial, we will install the latest version of Jenkins automation server on an Alibaba Cloud Elastic Computer Service (ECS) instance with Ubuntu 16.04 64 bit. We will also set up a secured Nginx reverse proxy to access the Jenkins instance. In the second part, we will create a sample Java web application as a Maven project. We will also go through the process of creating a build job in Jenkins. Finally, in the third part of the tutorial, we will automate the build process. We will also learn to continuously deliver the software using Jenkins.

Prerequisites

  1. Alibaba Cloud ECS instance with Ubuntu 16.04 64-bit installed.
  2. Firewall or Security group rules configured to allow the port “80”, “443”.
  3. A domain name that needs to be pointed towards your ECS instance.

You can follow the “Quick Start Guide” to create the instance and steps to connect to your instance. This tutorial assumes that you have already created your Alibaba instance and configured “jenkins.example.com” to point to your Ubuntu instance. Once you have connected to your instance via SSH, run the following command to update the repository cache and the base system.

apt update && apt -y upgrade && apt -y autoremove

Instead of running all the commands as root user, it is best to create a sudo user to run all commands. Create a sudo user. You are free to use any username according to your choice.

adduser aliyun

Once the user has been created, add it to the sudo group.

usermod -aG sudo aliyun

Now, switch to the newly created user.

su - aliyun

Set Hostname

Set the FQDN or Fully Qualified Domain Name as the hostname of your server. Setting the hostname is not mandatory, however, Jenkins produces warning messages in instances where hostname is incorrectly set.

sudo hostnamectl set-hostname jenkins.example.com

Similarly, add the domain name to your /etc/hosts file.

echo "127.0.0.1 jenkins.example.com" | sudo tee -a /etc/hosts

Install Java

Since Jenkins is written in Java and we may need to build Java applications in the future, we should proceed to install Java Development Kit or JDK. Jenkins supports both Oracle Java 8 and OpenJDK 8. In this tutorial, we will install Oracle Java version 8. Oracle Java is packed with both Java Runtime (JRE) and JDK. Add the PPA repository for Oracle Java.

Install Oracle Java 8.

sudo apt -y install oracle-java8-installer

You can verify if Java has been installed successfully by running java -version command.

Set the default path for the JAVA_HOME by installing the following package.

sudo apt -y install oracle-java8-set-default

You can now check if JAVA_HOME variable is set by running echo $JAVA_HOME. You may also need to log out and log back in to get the desired output.

Install Jenkins

Jenkins can be easily installed through the repository actively maintained by the project itself. Installation through the repository will also ensure easy upgrades in the future, directly using the apt upgrade command. Import the key used to sign the packages in Jenkins repository. This will ensure that the correct packages are installed.

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

Create a new repository list file for Jenkins.

echo "deb https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list

Install Jenkins.

Since we are installing Jenkins from the project maintained repository, it should install the latest available version of the application. Git is required to run the Git plugin in Jenkins. To start Jenkins and enable it to automatically start at boot time, run.

You can check the status of the service using the command systemctl status jenkins.

To check logs related to Jenkins server itself, run the following command.

tail -f /var/log/jenkins/jenkins.log

The first startup of Jenkins takes few minutes to start. If you find the following line in the output, your application is ready.

Jenkins is now running on your server. Proceed to install Nginx so that we can easily access the Jenkins instance using a domain name.

Note: If you do not have the domain name configured to point at the Alibaba Cloud ECS instance, you can open the port “8080” through the security group of your ECS instance and access your Jenkins instance on http://172.16.0.1:8080, where 172.16.0.1 is the public IP address of your ECS instance. Skip the Install Nginx section and continue to follow the tutorial from Final Setup.

Install Nginx

Although Jenkins has a built-in web server to serve the application on the port “8080”, in a production system it is not recommended to expose such web servers on the internet. In this tutorial, we will use Nginx as a reverse proxy to forward the requests from the client to the Jenkins server. Setting up a reverse proxy with a domain name also increases the accessibility of the instance, you need not remember the IP address of the instance to access Jenkins.

Install Nginx web server.

sudo apt -y install nginx

Start Nginx and enable the server to automatically start at boot time.

It is also important to secure the web server with SSL/TLS encryptions as logins and other important data will be sent from the client to the web server and vice versa. If the data being exchanged is not encrypted, a person eavesdropping into the network may obtain the data. In this tutorial, we are going to use the free SSL certificates from the Let’s Encrypt CA. If you want to use a more production friendly and trusted SSL, you may purchase SSL certificates from Alibaba Cloud.

Let’s Encrypt provides an easy to use tool “Certbot” for requesting and generation of certificates. Add Certbot repository to the system and install Certbot.

For Certbot to verify the ownership of the domain, it is important that the domain is pointed towards your ECS instance. If not, certificates for the domain will not be generated and you will be presented with an error. Request for the certificates using Certbot.

sudo certbot certonly --webroot -w /var/www/html -d jenkins.example.com

Once your certificates have been generated, you will see the following output.

Create a cron job for auto-renewing the certificates before they expire.

{ sudo crontab -l; echo '36 2 * * * /usr/bin/certbot renew --post-hook "systemctl reload nginx"'; } | sudo crontab -

You can check if the cron job is created by running sudo crontab -l command.

Create a new Nginx server block for Jenkins reverse proxy.

sudo nano /etc/nginx/sites-available/jenkins

Enter the following configuration in the editor. Make sure to replace all occurrences of the example domain with the actual one.

Activate the configuration file.

sudo ln -s /etc/nginx/sites-available/jenkins /etc/nginx/sites-enabled/jenkins

You can verify if the configuration file is error free by running sudo nginx -t.

Restart the Nginx web server so that the change in configuration can be reflected.

sudo systemctl restart nginx

Since we are using Nginx reverse proxy to access Jenkins, access to port “8080” is not necessary anymore. If you have enabled access to the port “8080” through the firewall or the security group of your ECS instance, you can safely remove it. To configure Jenkins built-in server to listen to the connections from localhost only, you can change the configuration in the Jenkins default configuration file. Open the configuration file.

sudo nano /etc/default/jenkins

Find the following line at the end of the file.

JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT"

Make the following changes to the configuration.

Save the file and exit from the editor. Restart your Jenkins instance by running.

sudo systemctl restart jenkins

Now Jenkins built-in server is configured to accept the connections only from the localhost. It can be accessed securely using the Nginx proxy only.

You can access your Jenkins instance through your favorite browser by going to the URL “https://jenkins.example.com".

Final Setup

During the installation, Jenkins generates an initial password. This initial password is required to complete Jenkins setup from the browser. Once you access your Jenkins instance through the browser, you will see that it is asking for an administrator password. Print the initial admin password on the terminal.

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

You should see a similar output.

Copy the password from the terminal and paste it into the browser.

Image for post
Image for post

On successful verification of the initial password, setup will ask you for the plugins to install. Since plugins can be easily managed from Jenkins interface, we will go with the Install suggested plugins option. This will install the most popular and useful plugins to extend Jenkins features.

Image for post
Image for post

At this stage, setup will install the suggested plugins. It will take a few minutes to get completed. You will see the status of installation of each plugin.

Image for post
Image for post

On successful installation of plugins, setup will ask you to create the administrator account. Enter the basic account details asked by Jenkins.

Image for post
Image for post

Finally, you will get the message that Jenkins is ready. Proceed to log in and log in using the administrator account you just created. You will be presented with the Jenkins dashboard.

Image for post
Image for post

Conclusion

That’s it. We have successfully deployed Jenkins automation server on an Alibaba Cloud ECSUbuntu 16.04. We have also configured Nginx reverse proxy and secured it with Let’s Encrypt SSL. Our Jenkins instance is now ready and we can proceed to create our first build job.

In the second part of the tutorial, we will create a sample web-based Java application as Maven project. We will also configure Jenkins to work with JDK, Git, and Maven. Finally, we will set up a Jenkin based build project for the sample application and we will also run our first build.

Reference:

https://community.alibabacloud.com/blog/ci%2Fcd-with-jenkins---part-1%3A-install-jenkins-on-ubuntu_593717?spm=a2c41.11628087.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store