DEV Community

Cover image for Jenkins 101 - Beginner to Advanced πŸš€πŸš€ (PART -1/2)
Tanmay Shukla
Tanmay Shukla

Posted on

Jenkins 101 - Beginner to Advanced πŸš€πŸš€ (PART -1/2)

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:

sudo apt-get update
sudo apt-get -y upgrade
sudo apt search openjdk
sudo apt install openjdk-11-jdk
java -version
Enter fullscreen mode Exit fullscreen mode
sudo systemctl status jenkins
sudo systemctl enable jenkins
sudo systemctl start jenkins
Enter fullscreen mode Exit fullscreen mode
  • Finally go to:
http://ip_address_or_domain:8080
Enter fullscreen mode Exit fullscreen mode

jeniks unlock

  • Run this to get initial password
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter fullscreen mode Exit fullscreen mode
  • Select like below: Image depluginn
  • Create a first admin user Image descriegegption
  • Now your jenkins is ready to use, Welcome to the world of jenkins: Imajenk 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 8080:8080 --> specify the port(bind the host(server) port 8080 to jenkins port 8080) Image jenkins
  • 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
    • jenkins/jenkins:lts : Official docker image for jenkins
    • Now Check our jenkins container with docker ps: Imdocker ps
    • Finally, please go to: http://ip_address_or_domain:8080
    • After opening the Jenkins dashboard, follow the above steps.
    • Run docker exec -it ` 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/ CentOS

  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Install Jenkins
sudo yum install jenkins -y
Enter fullscreen mode Exit fullscreen mode
  • Enable the Jenkins service to start at boot:
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
Enter fullscreen mode Exit fullscreen mode

✨✨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
Enter fullscreen mode Exit fullscreen mode
  • 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:

5. Running jenkins as a WAR(Web Application Resource or Archive) 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 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
Enter fullscreen mode Exit fullscreen mode
  • Finally go to
localhost or <your_ip_address>:8080
Enter fullscreen mode Exit fullscreen mode

as jenkins by default runs on port 8080. Hence your jenkins is up and running.
jenkins by war

Note:

We can do this other way also by installing tomcat and then deploy the war file. 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>
Enter fullscreen mode Exit fullscreen mode

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
  2. We create jobs in jenkins to Automate app workflow
  3. Build Tools: Maven(java app), Gradle, NPM(nodejs app)
  4. Create simple freestyle jobs and Configure Git Repository.
  • Run Tests and build java application run tests

Top comments (0)