22.Jenkins Notes
22.Jenkins Notes
JENKINS
========
CI : Continuous Integration
CD : Continuos Delivery
=> Using Jenkins we can deploy any type of project (ex: java, python, dot net,
react, angular).
===========================
What is Build & Deployment
===========================
=> In single day multipe times code will be committed to git hub repository from
Development team so multiple times we have to perform build and deployment process.
Note: If we do build and deployment process manually then it is time taking process
and error prone.
=> To overcome above problems, we need to automate Project Build and Deployment
process.
=> To automate project build and deployment process we will use JENKINS.
===============
Jenkins Setup
===============
===============================
what is job in jenkins ?
===============================
=> JOB means set of steps that we are giving to jenkins to perform the task
===============================
Creating First Job in Jenkins
===============================
6) Click on 'Build Number' and then click on 'Console Ouput' to see job execution
details.
$ cd /var/lib/jenkins/workspace/
7) Go to Jenkins home directory and check for the job name --> check the file
created inside the job
=========================================================
Jenkins Job with with GIT Hub Repo + Maven - Integeration
=========================================================
Note: With step-2 configuration, jenkins will download and install maven s/w
Step-6 : Click on 'Build Number' and then click on 'Console Ouput' to see job
execution details.
=> Go to jenkins workspace and then go to job folder then go to target folder there
we see war file created.
============
*Assignment*
============
=============================================================================
Job-3 :: Steps To Create Jenkins Job with Git Repo + Maven + Tomcat Server
=============================================================================
Go to Jenkins Dashboard -> Manage Jenkins --> Manage Plugins -> Goto Available Tab
-> Search For "Deploy To Container" Plugin -> Install without restart.
4) Run the job now using 'Build Now' option and see see 'Console Output' of job
===================================================
How to Create Jenkins Jobs with Build Parameters
===================================================
=> Build Parameters are used to supply dynamic inputs to run the Job.
=> Using Build Parameters we can avoid hard coding in jenkins job configuration.
master
develop
=> In source code management, give branch name to take dynamic value like below
*/${branch}
====================================
User & Roles Management In Jenkins
====================================
=> For every Team member Jenkins login access will be provided.
Note: Every team members will have their own user account to login into jenkins.
=> Operations team members are responsible to create / edit / delete / run jenkins
jobs.
=> Dev and Testing team members are only responsible to run the jenkins job.
================================================
How to create users and manage user permissions
================================================
Note: By default admin role will be available and we can create custom role based
on requirement
-> In Role we can configure what that Role assigned user can do in jenkins
=====================================
Working with User Roles in Jenkins
=====================================
=> This plugin allows you to define roles and assign them to users or groups.
=> Click "Manage Roles" and define new roles based on your requirements (e.g.,
admin, developer, tester).
=> Click "Add" to create a new role, and specify the permissions for that role.
=> After creating roles, go to "Manage Jenkins" > "Manage Users & Roles."
=> Select a user and click "Assign Roles" to add them to one or more roles.
##############
12-July-2024
##############
========================================
Jenkins - Master & Slave Architecture
========================================
=> If we use single machine jenkins, then burden will be increased if we run
multiple jobs at a time.
=> To reduce burden on jenkins server we will use Master & Slave Configuration.
=> Master & Slave configuration is used to reduce burden on Jenkins Server by
distributing tasks/load.
================
Jenkins Master
=================
=> The machine which contains Jenkins s/w is called as Jenkins Master machine.
Note: We can run jobs on Jenkins Master machine directley but not recommended.
==============
Jenkins Slave
==============
=> The machine which is connected with 'Jenkins Master' machine is called as
'Jenkins-Slave' machine.
=> Slave Machine will recieve task from 'Master Machine' for job execution.
===================================
Step-1 : Create Jenkins Master vm
===================================
==================================
Step-2 : Create Jenkins Slave vm
==================================
$ mkdir slavenode
=====================================================
Step-3: Configure Slave Node in Jenkins Master Node
=====================================================
1) Go to Jenkins Dashboard
2) Go to Manage Jenkins
3) Select Nodes option
4) Click on 'New Node' -> Enter Node Name -> Select Permanent Agent
5) Enter Remote Root Directory ( /home/ubuntu/slavenode )
6) Enter Label name as Slave-1
7) Select Launch Method as 'Launch Agent Via SSH'
8) Give Host as 'Slave VM DNS URL'
9) Add Credentials ( Select Kind as : SSH Username with private key )
10) Enter Username as : ubuntu
11) Select Private Key as Enter Directley and add private key
12) Select Host Key Strategy as 'Manually Trusted Key Verification Strategy'
13) Click on Apply and Save (We can see configured slave)
Note: Under Generation Section of Job creation process, Select "Restrict Where This
Project Can Run" and enter Slave Nodel Label name and finish job creation.
-> Execute the Job using 'Build Now' option
Note: Job will be executed on Slave Node (Go to Job Console Ouput and verify
execution details)
=========================================
Assignment : How to take jenkins backup
=========================================
========
Summary
========
1) What is Build & Deployment process
2) Challenges with manual build & deployment
3) Automated Build & Deployment
4) Jenkins Introduction
5) Jenkins Setup in Linux
6) Jenkins Jobs Creation (free style projects - gui)
7) Tools Configuration (ex: maven)
8) Plugins management (Ex: Deploy To Container, Role Based Strategy)
9) User Management + User Roles
10) Git + Maven + Tomact (Integration Job)
11) Job with Build Parameters
12) Master & Slave Configuration in Jenkins
##################################
13-July-2024 : Jenkins Pipeline
##################################
=> when we are dealing with complex CI CD process then pipelines are highly
recommended.
1) Declarative Pipeline
=============================
Jenkins Declarative Pipeline
=============================
pipeline {
agent any
tools{
maven "maven-3.9"
}
stages {
stage('Git Clone'){
steps{
echo 'cloning git repo'
}
}
stage('Maven Build'){
steps{
echo 'Maven Build'
}
}
stage('Deploy'){
steps{
echo 'Tomcat Deployment'
}
}
}
}
================================================
Declarative Pipeline with Git + Maven + Jenkins
================================================
pipeline {
agent any
tools{
maven "maven-3.9.8"
}
stages {
stage('Git Clone') {
steps {
git branch: 'develop', url: 'https://github.com/ashokitschool/maven-
web-app.git'
}
}
stage('Maven Build'){
steps{
sh 'mvn clean package'
}
}
}
}
=========================
SSH Agent Configuration
========================
=> SSH Agent is used to establish remote ssh connection from one linux vm to
another linux vm
Ex: jenkins server should connect with tomcat server to copy war file
(Manage Jenkins -> Plugins -> Available -> Search for SSH Agent ->
Install)
=> Use pipeline syntax and create ssh-agent for tomcat server vm.
=> Configure SSH Agent details in Deployment stage steps like below
sshagent(['tomcat-server-credentials']) {
// some block
}
=> With the help of ssh-agent we will copy war file to tomcat server using scp
command
========================================
Git + Maven + Tomcat + Jenkins Pipeline
========================================
pipeline {
agent any
tools{
maven "maven-3.9.8"
}
stages {
stage('Git Clone') {
steps {
git branch: 'develop',
url: 'https://github.com/ashokitschool/maven-web-app.git'
}
}
stage('Maven Build'){
steps{
sh 'mvn clean package'
}
}
stage('Deployment'){
steps{
sshagent(['tomcat-server-credentials']) {
sh 'scp -o StrictHostKeyChecking=no
target/01-maven-web-app.war
ec2-user@public-ip:/home/ec2-user/apache-tomcat-9.0.91/webapps'
}
}
}
}
}
################################
Email Notifications In Jenkins
################################
-> With this option we can send email notification to team members after jenkins
job execution completed.
Note: Under Advanced section add your gmail account credential for authentication
purpose.
##### Note: Instead of gmail password we need to add gmail app password ######
=> For testing purpose we can use "Email Notification option which is available at
the bottom of the page"
==============================================
Declarative Pipeline with Email Notification
==============================================
pipeline {
agent any
tools{
maven "Maven-3.9.4"
}
stages {
stage('Clone') {
steps {
git 'https://github.com/ashokitschool/maven-web-app.git'
}
}
stage('Build') {
steps {
sh 'mvn clean package'
}
}
}
post {
failure {
emailext(
subject: "Build Failed: $
{currentBuild.fullDisplayName}",
body: "The build ${currentBuild.fullDisplayName}
failed. Please check the console output for more details.",
to: '[email protected]',
from: '[email protected]',
attachLog: true
)
}
success {
emailext(
subject: "Build Successful: $
{currentBuild.fullDisplayName}",
body: "The build ${currentBuild.fullDisplayName} was
successful.",
to: '[email protected]',
from: '[email protected]',
attachLog: true
)
}
}
}
====================================
Jenkins Job with Parallel Stages
====================================
pipeline {
agent any
stages{
stage('git clone'){
steps{
echo 'git clone....'
}
}
stage('maven build'){
steps{
echo 'maven build...'
}
}
stage('parallel stage'){
parallel{
stage('code-review'){
steps{
echo 'code review....'
}
}
stage('nexus-upload'){
steps{
echo 'nexus upload...'
}
}
}
}
stage('deploy'){
steps{
echo 'deployment...'
}
}
}
}
==========================================
Working with Shared Libraries in Jenkins
==========================================
=> When we are dealing with multiple projects related pipelines then we can see
some common logics in all pipelines
=> Instead of writing same logic in multiple pipelines, we can write the logic at
one place and we can re-use it.
=> To achieve pipeline logic re-usability we will use 'Shared Libraries' concept.
=> To create shared libraries for jenkins, we will use 'groovy scripting'
=> shared library related groovy files we can keep in our git repository.
======================================
Jenkins Pipeline with shared library
=====================================
@Library('ashokit_shared_lib') _
pipeline {
agent any
tools{
maven "maven-3.9.8"
}
stages {
stage('Hello') {
steps {
welcome()
}
}
stage('git clone'){
steps{
gitClone();
}
}
stage('maven build'){
steps{
mavenBuild()
}
}
}
}
========================================================================
========
Summary
========
1) What is Build & Deployment process
2) Challenges with manual build & deployment
3) Automated Build & Deployment
4) Jenkins Introduction
5) Jenkins Setup in Linux
6) Jenkins Jobs Creation (free style projects - gui)
7) Tools Configuration (ex: maven)
8) Plugins management (Ex: Deploy To Container, Role Based Strategy)
9) User Management + User Roles
10) Git + Maven + Tomact (Integration Job)
11) Job with Build Parameters
12) Master & Slave Configuration in Jenkins
13) What is Jenkins Pipeline
13) Git + Maven + Tomact - Integratio CI CD Pipeline
14) Email Notifications in Pipeline (Email Extesion Server)
15) Parallel Stages in Jenkins Pipeline
16) Shared Libraries
============================
Jenkins Scripted Pipeline
============================
a) Flexibility
b) Customization
c) Reusable components
Syntax:
--------
node {
stage('clone'){
echo 'cloning...'
}
stage('build'){
echo 'build...'
}
}
=============================================
Jenkins Scripted Pipeline with git + maven
=============================================
node {
def mvnPath
stage('git clone') {
git 'https://github.com/ashokitschool/maven-web-app.git'
}
stage('Maven build'){
def mvnHome = tool name:'maven', type:"maven";
mvnPath = "${mvnHome}/bin/mvn";
sh "${mvnPath} clean package"
}
}
========================================
DevOps project setup : CI CD pipeline
========================================
1) git
2) maven
3) sonarqube
4) nexus
5) tomcat
a) main
b) develop
c) feature
d) release
=> We can create one pipeline and we can build the code from multiple branches at a
time using "Multi Branch Pipeline" concept.
=> When we create Multi Branch Pipeline, it will scan all the branches in given git
repo and it will execute pipeline for all branches.
Note: When we run mutli branch pipeline for secondtime, it will verify code changes
happend in which branches and it will execute pipeline for only those branches.