Skip to main content

Jenkins


Pre-requisites

1. Install a Webserver

https://gitlab.com/Azam-devops/webserver/-/blob/main/README.md

Code for index.html

https://gitlab.com/Azam-devops/webserver

2. Maven Code

https://gitlab.com/Azam-devops/imperial-maven-project

1. Install & configure Jenkins Automation Server on Linux Vm.
2. Go through at some of the important options in Jenkins.
3. Manage Jenkins.
4. Plugins
5. Global Tools Configuration.
6. Credentials
7. Users
8. Slave Nodes
9. Configuring CI pipeline using Gitlab.
10. Configuring standalone CICD pipeline using.
11. Automating the CICD pipeline.
12. Jenkins log
13. Introduction to Jenkins file.
14. Basic groovy syntax & file formation.
15. Launching a Pipeline using Jenkins file.

3. DevOps Architecture


Description of above DevOps plan.

  1. Create Maven based source code in Gitlab.
  2. Create a Jenkins job which will execute below stages.

  • Checkout code from Gitlab
  • Build/compile the source code using Maven as a build tool.
  • scan the code virtually.
  • Test the code by deploying an Apache Webserver & pointing the compiled code to it.
  • After Successful testing, upload the compiled code/Artifact to Nexus Server (centralized Server).
  • Deploy to UAT/SIT/PROD using Ansible playbooks.
  • Deployment will be done on Docker & further Kubernetes. 
    3. Nagios will monitor the above infrastructure.

Please follow below steps to install Jenkins latest version on RHEL based VM

Step 1 — Installing Jenkins:

There are two basic ways to install Jenkins on CentOS: through a repository, or repo, and via the WAR file. Installing from a repo is the preferred method, and it's what we'll outline first.
You'll need Java to run Jenkins (either method), so if your server doesn't yet have Java, install it with:
  • Become Root on the Linux VM
  • $ sudo -i
  • # yum install java-1.8.0-openjdk-devel

In general, if you need a service or tool but you're not sure what package provides it, you can always check by running:

  • yum whatprovides service
Where service is the name of the service or tool you require.

Installing from the Repo

Now, run the following to download Jenkins from the Red Hat repo:

  • wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
The wget tool downloads files into the filename specified after the "O" flag (that's a capital 'O', not a zero).
Then, import the verification key using the package manager RPM:
  • rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
  • yum upgrade -y
  • yum install epel-release java-11-openjdk-devel -y
  • OR
  • amazon-linux-extras install epel -y
Finally, install Jenkins by running:

  • yum install jenkins -y

Contents

Pre-requisites

1. Install a Webserver

1. Install & configure Jenkins Automation Server on Linux Vm.
2. Go through at some of the important options in Jenkins.
3. Manage Jenkins.
4. Plugins
5. Global Tools Configuration.
6. Credentials
7. Users
8. Slave Nodes
9. Configuring CI pipeline using Gitlab.
10. Configuring standalone CICD pipeline using.
11. Automating the CICD pipeline.
12. Jenkins log
13. Introduction to Jenkinsfile.
14. Basic groovy syntax & file formation.
15. Launching a Pipeline using Jenkinsfile.

========================================

DevOps Architecture


Project:- Install an Apache Web-server, build with Maven, Test locally/Remote & deploy on your desired option(VM/Docker/Cloud)

Description of above DevOps plan.

     1. Create Maven based source code in Gitlab.

2. Create a Jenkins job which will execute below stages.    
1. Checkout code from Gitlab     
2. Build/compile the source code using Maven as a build tool.    
3. scan the code virtually.     
4. Test the code by deploying an Apache Webserver & pointing the compiled 
     code to it.     
5. After Successful testing, upload the compiled code/Artifact to
     Nexus Server (centralized Server).                                                               
6. Deploy to UAT/SIT/PROD using Ansible playbooks.    
7. Deployment will be done on Docker & further Kubernetes. 

3. Nagios will monitor the above infrastructure.

========================================

Please follow below steps to install Jenkins latest version on RHEL based VM

Prerequisites

To follow this tutorial, you will need the following:
All the commands in this tutorial should be run as a non-root user. If root access is required for the command, it will be preceded by sudo.

System-Specific Packages vs the WAR File

Now that you know what Jenkins is, you need to understand how it is distributed. Jenkins runs on Java and comes as a WAR file — a collection of related content comprising a web application and intended to be run on a server. However, the developers of Jenkins kindly extend its ease of use through a number of system-specific packages that allow Jenkins to run as a controlled service.
A Jenkins package is available for the Red Hat family of distributions, which includes the CentOS operating systems. However, CentOS 7 in particular is a finicky breed, so it requires a different approach. Operations that work in other Red Hat-based OSes, even other CentOS versions, tend to work differently in CentOS 7, and the potential errors that result can be difficult to debug. Since the Jenkins package is generic Red Hat, rather than being differentiated for CentOS, the likelihood of running into issues is higher than on other OSs. For this reason, we won’t run Jenkins via this package. That leaves us the WAR file running through Java, which is far less convenient, requiring us to manually start and stop it through Java.
Fortunately, there’s a way around this, and even without a package, we can work with CentOS to have it treat Jenkins like a service.

Step 1 — Installing Jenkins

There are two basic ways to install Jenkins on CentOS: through a repository, or repo, and via the WAR file. Installing from a repo is the preferred method, and it's what we'll outline first.
You'll need Java to run Jenkins (either method), so if your server doesn't yet have Java, install it with:
  • Become Root on the Linux VM
  • yum -y install java
In general, if you need a service or tool but you're not sure what package provides it, you can always check by running:
  • yum whatprovides service
Where service is the name of the service or tool you require.

Installing from the Repo

Now, run the following to download Jenkins from the Red Hat repo:
  • wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
The wget tool downloads files into the filename specified after the "O" flag (that's a capital 'O', not a zero).
Then, import the verification key using the package manager RPM:
  • rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
  • yum upgrade -y
  • yum install epel-release java-11-openjdk-devel -y
  • OR
  • amazon-linux-extras install epel -y
Finally, install Jenkins by running:
  • yum install jenkins -y
5 things to remember while installing a tool/utility or an application
1. Name of  the tool/utility/application
2. Name of the service if that tool/utility or an application works as a service.
3. Port no. of that tool/utility or an application.
4. root directory of that tool/utility or an application.
5. Log file location of that tool/utility or an application.
That's it! You should now be able to start Jenkins as a service:
  • systemctl start jenkins.service
Once the service has started, you can check its status:
  • systemctl status jenkins.service
This will give you a fairly lengthy readout with a lot of information on how the process started up and what it's doing, but if everything went well, you should see two lines similar to the following:

  • Loaded: loaded (/etc/systemd/system/jenkins.service; disabled)
    Active: active (running) since Tue 2015-12-29 00:00:16 EST; 17s ago
This means that the Jenkins services completed its startup and is running. You can confirm this by visiting the web interface as before, at http://ip-of-your-machine:8080.


Please follow below steps to install OpenJDK  & Maven on both Master & Slaves

OpenJDK:

# cd /opt
# wget https://download.java.net/java/GA/jdk13/5b8a42f3905b406298b72d750b6919f6/33/GPL/openjdk-13_linux-x64_bin.tar.gz

# tar -xvf openjdk-13_linux-x64_bin.tar.gz
# /opt/jdk-13/bin/java -version

# vi /root/.bashrc
export JAVA_HOME=/opt/jdk-13/
export PATH=$PATH:/opt/jdk-13/bin

# source /root/.bashrc
# echo $JAVA_HOME
# java -version

Maven:

# cd /usr/local/src
# wget https://mirrors.estointernet.in/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
# tar -xf apache-maven-3.5.4-bin.tar.gz
# mv apache-maven-3.5.4/ apache-maven/
# vi /root/.bashrc
export MAVEN_HOME=/usr/local/src/apache-maven
export PATH=${MAVEN_HOME}/bin:${PATH}
# source /root/.bashrc 
# mvn --version

Go to Manage Jenkins -----> Go to Global Tool Configuration

1. Set JAVA_Home Path

2. Set Maven_Home Path



Create a new free style job in Jenkins & add stages as mentioned in the devops architecture. 

1. Checkout


2. Build

3. Execute Shell script for other stages.


How to create our own workspace & use it in Jenkins other than default workspace



How to use scp command in aws ec2 instance which we can use in our jenkins job under &  "execute shell

The root user is attempting to ssh to testing-webserver & copying source code & compiled code to testings webserver's document root which is owned by root. 

1. Goto Jenkins master & fire below command.
[root@jenkins ~]# ssh-keygen -t rsa

2. The public & private key of root user will saved in /root/.ssh folder. This is root's home directory.

3. Copy the id.rsa file to jenkins credentials & id_rsa.pub file to gitlab's repositories deloy keys option. Please follow below screenshots.

4. Add the private key here in jenkins.



5. Now paste the public key in gitlab's repository.

6. Put your private key id.rsa file on your source code repository to use it while running command through jenkins. 
7. Put your public key id_rsa.pub on testing-webservers /root/.ssh/authorized_keys file.
8. Now goto testing-webserver & enable root login. Update below file by removing hash sign # & save the file

[root@testing-webserver ~]# vi /etc/ssh/sshd_config

9. Restart sshd service on testing-webserver
[root@testing-webserver ~]# systemctl restart sshd

Now you use the command in your execute shell in jenkins,

Ex:- sudo scp -i id.rsa  -o StrictHostKeyChecking=no -r /my_custom_workspace/dev_env/webapp root@testing-webserver:/var/www/html

Above command will copy my_custom_workspace/dev_env/webapp folder from jenkins-master to testing-webservers document root folder that is /var/www/html using private DNS which we configured above. SCP is secure copy which ssh port 22 to copy from source to destination. 
Please follow below link to add host key instead of by passing the host key verification step as shown above. This is standard practice.


###################Error and Solutions ###################

Troubleshoot:-
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1

Solution:-

Go To sudoers file at /etc/sudoers 
or 
just type "visudo" on shell prompt
add below mentioned Red line..after below prompt
==========
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
jenkins ALL=(ALL) NOPASSWD: ALL
==========

Jenkins Logs

cat /var/log/jenkins/jenkins.log

Jenkins Installation link

https://www.jenkins.io/doc/book/installing/linux/#red-hat-centos



Comments

Popular posts from this blog

Docker In Details

  Course Contents:- 1. Overview of Docker 2. Difference between Virtualization & Containerization 3. Installation & Configuration of Docker Runtime on Linux & Windows 4. Practice on Docker commands 5. launch a Webserver in a container 6. Launch public & official images of application like Jenkins, Nginx, DB etc.. 7. Launch a base OS Container 8. How to save changes inside the container & create a fresh image(commit) 9. How to ship image & container from one hardware to another. 10. How to remove stop/rm multiple container/images 11. Docker Registry 12. Docker Networking       Check current docker network                  Docker Network Bridge                     Docker Network Weaving                  Launch our own Docker Cluster with our defined Network             ...

Ansible

  Ansible is an open-source software provisioning, configuration management, and application-deployment tool. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks. Platform support Control machines have to be a Linux/Unix host (for example SUSE Linux Enterprise, Red Hat Enterprise Linux, Debian, CentOS, macOS, BSD, Ubuntu, and Python 2.7 or 3.5 is required. Managed nodes, if they are Unix-like, must have Python 2.4 or later. For managed nodes with Python 2.5 or earlier, the python-simplejson package is also required. Since version 1.7, Ansible can also manage Windows nodes. In this case, native PowerShell remoting supported by the WS-Managemen...

Basic Linux Commands

  Linux Command Cheat Sheet Hello All, Below are the most common commands used in a day to day life of  linux user. if you are new to linux i will recommend you to go through all of the commands.  this commands will help you to troubleshoot linux issues.   Command Description ls Lists all files and directories from present working directory ls-R Lists files in sub-directories ls-a to list down hidden files. ls-al Lists files and directories with complete details like permissions, size, owner cd or cd ~ To go back to home directory cd .. Move one level up cd To change to a particular directory cd / Move to the root directory cat > filename Creates a new file cat filename Displays the content of a file cat file...

Kubernetes-Update

                                                    https://kubernetes.io/ Kubernetes (K8s)  is an open-source system for automating deployment, scaling, and management of containerized applications. It groups containers that make up an application into logical units for easy management and discovery. Kubernetes builds upon  15 years of experience of running production workloads at Google , combined with best-of-breed ideas and practices from the community. Latest Verion:-  1.19 Kubernetes Objects Kubernetes defines a set of building blocks ("primitives"), which collectively provide mechanisms that deploy, maintain, and scale applications based on CPU, memory or custom metrics. Kubernetes is loosely coupled and extensible to meet different workloads. This extensibility is provided in large part by the Kubernetes API, which is used by int...