Jenkins 101 - Beginner to Advanced 🚀🚀 (PART -1/2)
Tags: devops, jenkins, cloud, aws
Published: 2023-03-14 15:41:03

### Introduction
**Jenkins** is a free and open-source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery.

### Installation of Jenkins
There are multiple ways to install jenkins
#### 1. Jenkins On [Ubuntu](https://phoenixnap.com/kb/install-jenkins-ubuntu):
- First, [install java](https://mkyong.com/java/how-to-install-java-jdk-on-ubuntu-linux/):
```
sudo apt-get update
sudo apt-get -y upgrade
sudo apt search openjdk
sudo apt install openjdk-11-jdk
java -version
``` 
- Now go to [Jenkins download](https://pkg.jenkins.io/debian-stable/) and follow instructions. Then run: 
```
sudo systemctl status jenkins
sudo systemctl enable jenkins
sudo systemctl start jenkins
```
- Finally go to:
```
http://ip_address_or_domain:8080
```
![jeniks unlock](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vr2wjk9ovfrhmsuarcw.png)
- Run this to get initial password
  ```
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
```
- Select like below: 
![Image depluginn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8icu9bkeukrw4kzzxnn.png)
- Create a first admin user
![Image descriegegption](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx5vfv5a9vo8hhb68cm9.png)
- Now your jenkins is ready to use, Welcome to the world of jenkins:
![Imajenk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cag74mla6tr2hzt0imzq.png)
Note : Make sure you have enabled 8080 port(by enabling custom tcp/udp as 8080 inside inbound rules in your security group if using EC2) 
 
#### 2. On Docker container:
-  Install docker: 

```
sudo apt-get update
sudo apt-get -y upgrade
sudo install docker.io
```

- Now to download an jenkins image and run it as docker command we need to run the following command

```
docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home
```
   - **8080:8080** --> specify the port(bind the host(server) port 8080 to jenkins port 8080) 
![Image jenkins](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zebvxak004cpnc7e48jc.png)
   - **50000:50000** -->  port where jenkins master and slave will communicate(as jenkins can be build and started as a cluster). If we have large workloads that you are running with the jenkins, so this port is where communication between master and worker node is happening. 
  - `-d` :  To run container in detached mode(background)
  - `-v` : To mount the volumes jenkins_home
     ![Image volume](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd2bwy5nxgsil82h0uc9.png)
  - `jenkins/jenkins:lts` : [Official docker image for jenkins](https://hub.docker.com/r/jenkins/jenkins)

- Now Check our jenkins container with `docker ps`:
![Imdocker ps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3uyu39ho41910n1yux5.png)  
- Finally, please go to: `http://ip_address_or_domain:8080` 
- After opening the Jenkins dashboard, follow the above steps.
- Run docker exec -it <Container ID>` bash --> To run a command in a running container(i.e. login as jenkins user)
- Run `docker volume inspect jenkins_home --> To get info about our created volume.

#### 3. On [Amazon Linux 2](https://www.jenkins.io/doc/tutorials/tutorial-for-installing-jenkins-on-AWS/)/ [CentOS](https://phoenixnap.com/kb/how-to-install-jenkins-on-centos-8)
-  First install java: 
`sudo yum update -y
sudo yum install java-1.8.0-openjdk`
- Now download latest jenkins package:
```
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
```
- Import a key file from Jenkins-CI to enable installation from the package:
```
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
```
- Install Jenkins
```
sudo yum install jenkins -y
```
- Enable the Jenkins service to start at boot:
```
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
```
#### ✨✨Note: For CentOS make sure to configure firewall as, Jenkins service uses port **8080** to communicate. If you’re using the default firewalld service, enter the following commands to allow access:
```
sudo firewall-cmd ––permanent ––zone=public ––add-port=8080/tcp
sudo firewall-cmd ––reload
```
- Open a web browser, and enter the following URL: `http://ip_address_or_domain:8080`  

#### 4. Jenkins on Windows 10:
While it is recommended that you install jenkins controller on linux based server as windows has some complication like locking file sysytem semantics, but we can also install it on our local **Windows** machine also.
Just Go through these videos to get up & running:
  - [Install jenkins on Windows server 2022](https://www.youtube.com/watch?v=XuMrEDA8cAI&t=409s)
  - [Install jenkins on local windows 10](https://www.youtube.com/watch?v=I--rP8MdQFE)
  - [Blog on jenkins on Windows 10](https://phoenixnap.com/kb/install-jenkins-on-windows)

#### 5. Running jenkins as a WAR([Web Application Resource or Archive](https://www.javatpoint.com/war-file)) file
- Before installing jenkins make sure you have following
  - Running jenkins using war file is platform/OS independent. 
  - Only requires that JRE or JDK be insatlled on target.
  - Java 8 and 11 are supported only.
- Go to [jenkins download page](https://www.jenkins.io/download/) and copy the link of Generic Java package(.war) file. 
- Then use `wget <jenkins.war link>` to download the war file inside your home directory.
- Now all you need to run jenkins is invoke java by running:
```
java -jar jenkins.war
```
- Finally go to 
```
localhost or <your_ip_address>:8080
```
as jenkins by default runs on port 8080. Hence your jenkins is up and running.
![jenkins by war](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g7cvdj7n96s1eqlmflsw.png)
 
## Note:   
We can do this other way also by [installing tomcat and then deploy the war file](https://www.edureka.co/blog/install-jenkins/). As eventually when we run java application on our servers. We require tomcat (Apache Tomcat is a web server and servlet container that is used to deploy and serve Java web applications)

- `sudo apt install openjdk-11-jdk`
- `sudo apt install wget`
- Install tomcat 9 with
`wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.0.M10/bin/apache-tomcat-9.0.0.M10.tar.gz`
  - Extract the files `tar xzf apache-tomat-9.0.0.M10.tar.gz`
- To make it simple we will move this extracted file to a new directory Tomcat9 using the mv command and to do that I will execute the following command: 
`mv apache-tomcat-9.0.0.M10 tomcat9`
- Our next step is to provide a username and password for Apache Tomcat ---> `vim /home/edureka/tomcat9/conf/tomcat-users.xml`
- Now replace the tomcat-users.xml content with following. This is to create tomcat users
```
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <role rolename="manager-jmx"/>
    <role rolename="manager-jmx"/>
    <role rolename="admin-gui"/>
    <role rolename="admin-script"/>
    <user username="user1" password="password1" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>
</tomcat-users>
```
In the above code, as you can see that I have defined several roles and for all these roles I have given one single username called `user1` and password i.e. `password`. If you want to assign different username and password for different roles you can do that as well.
Now save it and close the file to go back to the terminal.
- We need to start Apache Tomcat now, but before that I will change my directory to Tomcat9 by executing the below command:  
`cd tomcat9`
- To start Tomcat use the below command:  
`./bin/startup.sh`
- Now we need to download Jenkins war File:
  
 
---

### JENKINS FUNDAMENTALS  
1. **Roles** in jenkins : In Jenkins we have two kinds of Roles.
![Image roles](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2gtpxwdnfugnarfa1e2.png)
- We create jobs in jenkins to Automate app workflow
2. **Build Tools:** Maven(java app), Gradle, NPM(nodejs app)
  - 
  -  [Install Nodejs and NPM on ubuntu:](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04)
- Create simple freestyle jobs and Configure Git Repository.

- Run Tests and build java application
![run tests](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enq67iky91x4p1mezix0.png)