Search This Blog

Thursday, 8 June 2023

Install Jenkins on Linux

 Jenkins Installation Requirement for the below specific installation:


JDK : jdk1.8.0_161
Memory: 2G
DiskSpace: 50G
Operating System Version: Oracle Linux Server release 6.6
Java Container: apache-tomcat-8.5.30
Jenkins: Jenkins 2.107.2


Jenkins is a software that allows continuous integration.
Jenkins will be installed on a server where the central build will take place

Step 1) Download jdk from oracle site:
 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html



Step 2) Exact the downloaded jdk file to the directory where you want to install java. I have extracted under /home/oracle
                cd /home/oracle
                gunzip jdk-8u161-linux-x64.tar.gz
                tar -xvf jdk-8u161-linux-x64.tar

Step 3) Setup JAVA_HOME 
            After the extraction you should see the extracted version /home/oracle/jdk1.8.0_161
            Go to the user home directory 
            cd ~
            update the .bash_profile

vi .bash_profile
--------------------------------
if [ -f ~/.bashrc ]; then

        . ~/.bashrc

fi



# Get the aliases and functions

# User specific environment and startup programs

JAVA_HOME=/home/oracle/jdk1.8.0_161

PATH=$PATH:$JAVA_HOME/bin

export PATH

-------------------------------

Restart the Unix terminal or execute . .bash_profile for setting up JAVA_HOME variable.

Verify the java version by executing the following command.
[oracle@OracleLinux6 ~]$ java -version

java version "1.7.0_21"

Java(TM) SE Runtime Environment (build 1.7.0_21-b11)

Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

Step 4) Download and install Apache tomcat application server.

Download the tomcat version from the following site:

https://tomcat.apache.org/download-80.cgi

I have selected the highlighted version for my installation.



Step 4) Extract downloaded tomcat distribution where you want to install.  

cd /home/oracle
gunzip apache-tomcat-8.5.30.tar.gz
tar -xvf apache-tomcat-8.5.30.tar 

I have the extracted version at /home/oracle/apache-tomcat-8.5.30

Start the tomcat service by executing the following command
cd /home/oracle/apache-tomcat-8.5.30/bin

[oracle@OracleLinux6 bin]$ ./startup.sh

Using CATALINA_BASE:   /home/oracle/apache-tomcat-8.5.30
Using CATALINA_HOME:   /home/oracle/apache-tomcat-8.5.30
Using CATALINA_TMPDIR: /home/oracle/apache-tomcat-8.5.30/temp
Using JRE_HOME:        /home/oracle/jdk1.8.0_151
Using CLASSPATH:       /home/oracle/apache-tomcat-8.5.30/bin/bootstrap.jar:/home/oracle/apache-tomcat-8.5.30/bin/tomcat-juli.jar
Tomcat started.

Access the Tomcat console from web browser.

http://hostname:8080




Step 5) Download and deploy Jenkins war file on tomcat

Download jenkins.war from the following URL:
https://jenkins.io/download/

           

Step 6) Copy the jenkins.war file to the webapp directory and restart tomcat instance for deployment.         

cp /home/oracle/Downloads/jenkins.war /home/oracle/apache-tomcat-8.5.30/webapps/

[oracle@OracleLinux6 webapps]$ ls -rlt
total 72924
drwxr-x---.  3 oracle oracle     4096 Apr 11 21:09 ROOT
drwxr-x---.  5 oracle oracle     4096 Apr 11 21:09 manager
drwxr-x---.  5 oracle oracle     4096 Apr 11 21:09 host-manager
drwxr-x---.  6 oracle oracle     4096 Apr 11 21:09 examples
drwxr-x---. 14 oracle oracle     4096 Apr 11 21:09 docs
-rw-rw-r--.  1 oracle oracle 74568464 Apr 11 21:17 jenkins.war

SHUTDOWN  
/home/oracle/apache-tomcat-8.5.30/bin
[oracle@OracleLinux6 bin]$ ./shutdown.sh
Using CATALINA_BASE:   /home/oracle/apache-tomcat-8.5.30
Using CATALINA_HOME:   /home/oracle/apache-tomcat-8.5.30
Using CATALINA_TMPDIR: /home/oracle/apache-tomcat-8.5.30/temp
Using JRE_HOME:        /home/oracle/jdk1.7.0_21
Using CLASSPATH:       /home/oracle/apache-tomcat-8.5.30/bin/bootstrap.jar:/home/oracle/apache-tomcat-8.5.30/bin/tomcat-juli.jar

STARTUP
[oracle@OracleLinux6 bin]$ ./startup.sh
Using CATALINA_BASE:   /home/oracle/apache-tomcat-8.5.30
Using CATALINA_HOME:   /home/oracle/apache-tomcat-8.5.30
Using CATALINA_TMPDIR: /home/oracle/apache-tomcat-8.5.30/temp
Using JRE_HOME:        /home/oracle/jdk1.7.0_21
Using CLASSPATH:       /home/oracle/apache-tomcat-8.5.30/bin/bootstrap.jar:/home/oracle/apache-tomcat-8.5.30/bin/tomcat-juli.jar
Tomcat started.

Step 7) Access the Jenkins URL on your web browser.
http://hostname:8080/jenkins





Step 8) Unlock Jenkins by entering the Administrator password from the location on your Linux machine and click on install suggested plugins for now.




Step 9) Create Admin user








Steps to install Jenkins using AWS

Login into AWS account and click on EC2


In the navigation pane, under NETWORK & SECURITY, select Key Pairs.



Create key pair and give a name and type as RSA and private key file format as .ppk for PUTTY compatibility



Click on create key pair, A private file gets downloaded automatically




The Choose an Amazon Machine Image (AMI) page displays a list of basic configurations called Amazon Machine Images (AMIs) that serve as templates for your instance. Select the HVM edition of the Amazon Linux AMI.




·         Scroll down and select the key pair you created in the creating a key pair section above or any existing key pair you intend to use.

·         Select an existing security group.

·         Select the security group that you created.

·         Select Launch Instance.


In the left-hand navigation bar, choose Instances to view the status of your instance. Initially, the status of your instance is pending. After the status changes to running, your instance is ready for use.



Installing and configuring Jenkins

Now that the Amazon EC2 instance has been launched, Jenkins can be installed properly.

In this step you will deploy Jenkins on your EC2 instance by completing the following tasks:

  •         Connecting to your Linux instance
  •         Downloading and installing Jenkins
  •         Configuring Jenkins

Connecting to your Linux instance

After you launch your instance, you can connect to it and use it the same way as your local machine.

Before you connect to your instance, get the public DNS name of the instance using the Amazon EC2 console.

Select the instance and locate Public DNS.



Prerequisites

The tool that you use to connect to your Linux instance depends on your operating system.

If your computer runs Windows, you will connect using PuTTY.

If your computer runs Linux or Mac OS X, you will connect using the SSH client.

These tools require the use of your key pair. Be sure that you have created your key pair as described in Creating a key pair.

Using PuTTY to connect to your instance

  •           From the Start menu, select All Programs > PuTTY > PuTTY.
  •           In the Category pane, select Session, and complete the following fields:
  •           In Host Name, enter ec2-user@public_dns_name.
  •            Ensure that Port is 22.




·          In the Category pane, expand Connection, expand SSH, and then select Auth. Complete the following:

·          Select Browse.

·          Select the .ppk file that you generated for your key pair, as described in Creating a key pair and then select Open.

·          Select Open to start the PuTTY session.

 



Downloading and installing Jenkins

Completing the previous steps enables you to download and install Jenkins on AWS. To download and install Jenkins:

1.        Ensure that your software packages are up to date on your instance by using the following command to perform a quick software update:

[ec2-user ~]$ sudo yum update –y

2.        Add the Jenkins repo using the following command:

3.        [ec2-user ~]$ sudo wget -O /etc/yum.repos.d/jenkins.repo \

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

4.        Import a key file from Jenkins-CI to enable installation from the package:

[ec2-user ~]$ sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

[ec2-user ~]$ sudo yum upgrade

5.        Install Java (Amazon Linux 2):

[ec2-user ~]$ sudo amazon-linux-extras install java-openjdk11 -y

6.        Install Java (Amazon Linux 2023):

[ec2-user ~]$ sudo dnf install java-11-amazon-corretto -y

7.        Install Jenkins:

[ec2-user ~]$ sudo yum install jenkins -y

8.        Enable the Jenkins service to start at boot:

[ec2-user ~]$ sudo systemctl enable jenkins

9.        Start Jenkins as a service:

[ec2-user ~]$ sudo systemctl start jenkins

You can check the status of the Jenkins service using the command:

[ec2-user ~]$ sudo systemctl status jenkins

 

Configuring Jenkins

Jenkins is now installed and running on your EC2 instance. To configure Jenkins:

Connect to http://<your_server_public_DNS>:8080 from your browser. You will be able to access Jenkins through its management interface:


·         As prompted, enter the password found in /var/lib/jenkins/secrets/initialAdminPassword.

·         Use the following command to display this password:

·         [ec2-user ~]$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword

·         The Jenkins installation script directs you to the Customize Jenkins page. Click Install suggested plugins.

Once the installation is complete, the Create First Admin User will open. Enter your information, and then select Save and Continue.



1.        On the left-hand side, select Manage Jenkins, and then select Manage Plugins.

2.        Select the Available tab, and then enter Amazon EC2 plugin at the top right.

3.        Select the checkbox next to Amazon EC2 plugin, and then select Install without restart.



4.        Once the installation is done, select Back to Dashboard.

5.        Select Configure a cloud if there are no existing nodes or clouds.


If you already have other nodes or clouds set up, select Manage Jenkins.



After navigating to Manage Jenkins, select Configure Nodes and Clouds from the left hand side of the page.

  1. a.        From here, select Clouds.

  1. Select Add a new cloud, and select Amazon EC2. A collection of new fields appears.
.        Click Add under Amazon EC2 Credentials



a.        From the Jenkins Credentials Provider, select AWS Credentials as the Kind.


 a.        Scroll down and enter in the IAM User programmatic access keys with permissions to launch EC2 instances and select Add.




    1. Scroll down to select your region using the drop-down, and select Add for the EC2 Key Pair’s Private Key.


a.        From the Jenkins Credentials Provider, select SSH Username with private key as the Kind and set the Username to ec2-user.


a.        Scroll down and select Enter Directly under Private Key, then select Add.





a.        Open the private key pair you created in the creating a key pair step and paste in the contents from "-----BEGIN RSA PRIVATE KEY-----" to "-----END RSA PRIVATE KEY-----". Select Add when completed.



a.        Scroll down to "Test Connection" and ensure it states "Success". Select Save when done






 Now you are ready to use EC2 instances as Jenkins agents.