Jenkins Installation and Configuration on AWS EC2

Jenkins is an open-source continuous integration and continuous development tool. In this tutorial, I will explain the installation of the Jenkins server on the AWS EC2 instance.  Here, I am using Amazon Linux  [Amazon Linux 2 AMI (HVM) – Kernel 5.10, SSD Volume Type – ami-0ed9277fb7eb570c9 (64-bit x86) ] as my Operating System.  At the time of instance lunch, make sure you have added an inbound rule for 8080 port. Once you launched the EC2 instance, log in to the machine with the help of the private key which is downloaded at the time of EC2 launch.

Switch to root for setting a hostname for your machine and execute the below command.

[ec2-user@ip-172-31-xx-xx ~]$ sudo su -

Last login: Fri Dec 17 16:06:34 UTC 2021 on pts/0

[root@ip-172-31-88-39 ~]# hostname jenkins-server

Log out and log in to check the hostname that we set.

Installation steps.

Jenkins is a java-based program, so we need to make sure that java is installed. Execute the below command to check the Java version.

Java installation

[ec2-user@jenkins-server ~]$ java -version

-bash: java: command not found

The Amazon Linux version doesn’t have Java by default. To install Java follow the below steps.

Amazon Linux is not providing the yum package manager epel-release repository but it is epel-release is available in Amazon Linux Extra topic “epel”. To know the available packages and its version execute the below command.

$ amazon-linux-extras

In the output, you can see java-openjdk is available.

First, install the epel-release by executing the below command.

# sudo amazon-linux-extras install epel

After epel-release is installed, execute the below command for Java installation.

$ sudo amazon-linux-extras install java-openjdk11

Execute java -version to confirm the installation.

[ec2-user@jenkins-server ~]$ sudo java -version

openjdk version "11.0.13" 2021-10-19 LTS

OpenJDK Runtime Environment 18.9 (build 11.0.13+8-LTS)

OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8-LTS, mixed mode, sharing)

Installation is done. Next, we have to set the Java home path.

Find out where is JRE by executing the below command.

 

[ec2-user@jenkins-server ~]$ sudo find /usr/lib/jvm/java-* | head -n 3

/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.amzn2.0.3.x86_64

/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.amzn2.0.3.x86_64/bin

/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.amzn2.0.3.x86_64/bin/alt-java

The first one is our Java home path. Copy the path and add it to the bash profile file.

$ sudo vi /home/ec2-user/.bash_profile

Add the below code or modify it based on your profile file.

JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.amzn2.0.3.x86_64

PATH=$PATH:$HOME/bin:$JAVA_HOME

Execute an echo command to confirm we set the correct path. Please note the first time it won’t show the correct path so log out and log in again.

[ec2-user@jenkins-server ~]$ echo $JAVA_HOME

/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.amzn2.0.3.x86_64

Jenkins installation

Install the required repository and add the corresponding key, for this execute the below command. These repository details can be found at the below link. Follow the steps mentioned on the page.

https://pkg.jenkins.io/redhat-stable/

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

Next, install Jenkins service with the help of yum.

sudo yum install -y jenkins

 

Start the service

$ sudo service jenkins start

Starting Jenkins                                           [  OK  ]

Now try to access the service on the browser. Copy the server IP address and paste it on the browser.

 

To get the Admin password copy and paste the mentioned file.

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

1b2a83040c28447e865b6

Paste the password in the box and click on the “Continue ” button.

On the next page, you will get an option to install the plugins. If you required you can install the suggested plugin or you can do it later. Next page you can create an admin user or skip it. I skipped and completed the installation.

 

Next page you will get a message that displays the Jenkins installation is completed.

Change admin password

Click on “Start using Jenkins” to start it. Once you logged in you can change the admin password by selecting admin (Top right corner) and clicking on Configuration.  Add a new password in the mentioned field.

 

Set Java home path

To set the Java path click on Manage Jenkins from the left side panel.

Next page click on Global Tool Configuration and then click on the “Add JDK” button.

 

Give a name and deselect the “Install automatically” option and add the java home path. Please refer to below screenshot.

 

 

Click apply and then save it.

Execute the first job

We will create a test job on Jenkins for this click on “New item” from the left side panel.

Give a name and select the Freestyle project.

 

 

Click on to continue. On the next page add a description of your job and select Source code management as none.

Next one we have to choose a Build, I have chosen Execute shell and added a shellcode. With will display output as “Hello!! Welcome to Jenkins’s job”.

Click the Save button to save it.

To build our job click on “build now” or if you want to modify click on configure.

Click on Build now to run the code. Under the left side panel, you can see a blue ball which indicates that the build is successful. If it is in the red color then the build is failed.

 

Click the blue ball to see the console output.

From the output, it is clear the job is executed by the admin user and the output is

“Hello!! welcome Jenkins job” and it finished successfully.

Jenkins server installation and configurations are completed.

3 comments

Leave a Reply

Your email address will not be published.