======== JENKINS ======== => Open source Software & free of cost => Developed by using Java Language => It is called as CI CD Server CI : Continuous Integration CD : Continuos Delivery => CI CD is one appraoch to automate project Build & Deployment process. => Using Jenkins we can deploy any type of project (ex: java, python, dot net, react, angular). =========================== What is Build & Deployment =========================== => Take latest code from Git Hub Repo => Build Source code using Maven => Perform Code Review Using Sonar => Upload Project Artifact into Nexus => Deploy code into server. => 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 =============== Git Repo : https://github.com/ashokitschool/DevOps-Documents/blob/main/01-Jenkins-Server-Setup.md =============================== what is job in jenkins ? =============================== => JOB means set of steps that we are giving to jenkins to perform the task step-1 : take code from git repo step-2 : perform maven build step-3 : build docker image step-4 : deploy docker image in k8s cluster =============================== Creating First Job in Jenkins =============================== 1) Goto Jenkins Dashboard 2) Click on New Item -> Enter Item Name (Job Name) -> Select Free Style Project & Click OK -> Enter some description -> Click on 'Build' tab -> Click on 'Add Build Step' and select 'Execute Shell' 3) Enter below shellscript echo "Hello Guys," touch ashokit.txt echo "Hello Guys, Welcome to Jenkins Classes" >> ashokit.txt echo "Done..!!" 4) Apply and Save Note: With above steps we have created JENKINS Job 5) Click on 'Build Now' to start Job execution 6) Click on 'Build Number' and then click on 'Console Ouput' to see job execution details. => Jenkins Home Directory in EC2 : /var/lib/jenkins/workspace/ $ 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 ========================================================= Step-1 : Instal git client in jenkins server sudo apt install git -y Step-2 : Configure Maven as global tool (Jenkins Dashboard -> Manage Jenkins --> Global Tools Configuration -> Add maven) Note: With step-2 configuration, jenkins will download and install maven s/w Step-3 : Take project git repo (https://github.com/ashokitschool/maven-web-app.git) Step-4 : Create Jenkins job -> New Item -> Enter Item Name (Job Name) -> Select 'Free Style Project' & Click OK -> Enter some description -> Go to "Source Code Management" Tab and Select "Git" -> Enter Project "Git Repo URL" -> Go to "Build tab" -> Click on Add Build Step and Select 'Inovke Top Level Maven Targets' -> Select Maven and enter goals 'clean package' -> Click on Apply and Save Note: With above steps we have created JENKINS Job Step-5 : Click on 'Build Now' to start Job execution. Step-6 : Click on 'Build Number' and then click on 'Console Ouput' to see job execution details. => Jenkins Home Directory in EC2 : /var/lib/jenkins/workspace/ => Go to jenkins workspace and then go to job folder then go to target folder there we see war file created. ============ *Assignment* ============ Create Jenkins Job to perform below operation 1) Take source code from git repo 2) Build that code using maven 3) Deploy war file into tomcat server (diff linux vm) ============================================================================= Job-3 :: Steps To Create Jenkins Job with Git Repo + Maven + Tomcat Server ============================================================================= 1) Tomcat Server Setup in Linux VM 2) Install "Deploy To Container Plugin" in Jenkins Go to Jenkins Dashboard -> Manage Jenkins --> Manage Plugins -> Goto Available Tab -> Search For "Deploy To Container" Plugin -> Install without restart. 3) Create Jenkins Job (Free Style Project) -> New Item -> Enter Item Name (Job Name) -> Select Free Style Project & Click OK -> Enter some description -> Go to "Source Code Management" Tab and Select "Git" -> Enter Project "Git Repo URL" -> Go to "Build tab" -> Click on Add Build Step and Select 'Inovke Top Level Maven Targets' -> Select Maven and enter goals 'clean package' -> Click on 'Post Build Action' and Select 'Deploy war/ear to container' option -> Give path of war file (You can give like this also : **/*.war ) -> Enter Context Path (give project name Ex: java_web_app) -> Click on 'Add Container' and select Tomcat version 9.x -> Add Tomcat server credentials (give the username & pwd which is having manager-script role) -> Enter Tomact Server URL (http://ec2-vm-ip:tomcat-server-port) -> Click on Apply and Save 4) Run the job now using 'Build Now' option and see see 'Console Output' of job 5) Once Job Executed successfully, go to tomcat server dashboard and see application should be displayed. 6) Click on the applicaton name (it should display our application) =================================================== 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. Ex : We can pass branch name as build parameter in runtime. => Open Jenkins Job => In General Section => Select "This Project is parameterized option" => Select choice parameter => Enter name as branch => Give choices like below master develop => In source code management, give branch name to take dynamic value like below */${branch} ==================================== User & Roles Management In Jenkins ==================================== => In Our Project multiple teams will be available a) Development team (10) b) Testing team (5) c) DevOps Team (3) => 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 ================================================ -> Go to Jenkins Dashboard -> Manage Jenkins -> Manage Users -> Create Users -> Go to Configure Global Security -> Manage Roles & Assign Roles 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 -> In Assign Roles we can add users to particular role ===================================== Working with User Roles in Jenkins ===================================== ## Step-1 : Install Required Plugins => Install "Role-based Authorization Strategy" Plugin => This plugin allows you to define roles and assign them to users or groups. ## Step-2 : Configure Security => Go to "Manage Jenkins" > "Configure Security." => Select Authorization as "Role-Based Strategy" => Click "Save" to apply the changes ## Step-3 : Create User Roles => Go to "Manage Jenkins" > "Manage and Assign Roles." => 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. ## Step-4 : Assign Users to Roles => 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. ## Step-5 : Test the user login functionality ############## 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. => If burden increased then system can crash. => 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. => It is used to create the jobs => It is used to schedule the jobs => It is responsible to distribute Jobs execution to slave machines. 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 =================================== 1) Launch Linux VM (t2.medium) 2) Install Java s/w 3) Install Jenkins s/w ================================== Step-2 : Create Jenkins Slave vm ================================== 1) Create EC2 instance (Ubuntu with t2.micro) 2) Connect to EC2 using ssh client 3) Change hostname for readability $ sudo hostname jenkins-slave $ exit 3) Install Java Software $ sudo apt install default-jre 4) Create one directory in /home/ubuntu $ 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 Note: Open pem file and copy content add add 12) Select Host Key Strategy as 'Manually Trusted Key Verification Strategy' 13) Click on Apply and Save (We can see configured slave) *********** With above steps Master and Slave Configuration Completed **************** -> Go to Jenkins Server and Create Jenkins Job 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) => Build & Deployment => Challenges in Manual Build & Deployment => Automated Build & Deployment => CI & CD => Jenkins Introduction => Jenkins Setup in Linux => Jenkins Job Creation => Jenkins Build Parameters => User & Role Management in Jenkins (RBAC) => Git + Maven + Tomcat + Jenkins => Master & Slave Configuration ========================================= Assignment : How to take jenkins backup ========================================= Jenkins Backup : https://youtu.be/5Tb-AOUFuKQ?si=iheXiejP-KeI_iCO ======== 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 ################################## => Jenkins pipeline is a way to define CI CD process as a code. => The whole CI CD workflow we will define as a code in pipeline. => when we are dealing with complex CI CD process then pipelines are highly recommended. => Pipeline contains set of stages to perform CI CD Stage-1: Clone Git Repo Stage-2: Maven Build State-3: Code Review Stage-4 : Artifact Upload Stage-5: Build Docker Image Stage-6: Push Image to Registry Stage-7: Deploy App in K8S Stage-8: Send Email Notification => We can create jenkins pipelines in 2 ways 1) Declarative Pipeline 2) Scripted (groovy) 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 => Install SSH Agent plugin (Manage Jenkins -> Plugins -> Available -> Search for SSH Agent -> Install) => Use pipeline syntax and create ssh-agent for tomcat server vm. -> Snippet Generator -> Sample Step -> Select SSh Agent -> Add -> SSH Username with private key -> Configure Tomcat server username and pem file content -> Click on generate pipeline script to get ssh-agent => 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 sh 'scp -o StrictHostKeyChecking=no target/01-maven-web-app.war ec2-user@public-ip:/home/ec2-user/apache-tomcat-9.0.91/webapps' ======================================== 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 ################################ -> We can configure Email notifications in Jenkins -> With this option we can send email notification to team members after jenkins job execution completed. -> We need to configure SMTP properties to send emails -> Go To Manage Jenkins -> Go To System -> Go to "Extended E-mail Notification" -> We will add company provided SMTP server details to send emails. Note: For practise we can use GMAIL SMTP Properties SMTP Server : smtp.gmail.com SMTP Port : 587 Note: Under Advanced section add your gmail account credential for authentication purpose. ##### Note: Instead of gmail password we need to add gmail app password ###### ##### URL To generate gmail app pwd : https://myaccount.google.com/apppasswords => Select use TLS checkbox => 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: 'ashokitschool@gmail.com', from: 'ashokit.classes@gmail.com', attachLog: true ) } success { emailext( subject: "Build Successful: ${currentBuild.fullDisplayName}", body: "The build ${currentBuild.fullDisplayName} was successful.", to: 'ashokitschool@gmail.com', from: 'ashokit.classes@gmail.com', 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 Ex: maven build code review artifact upload => 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. #### Git Repo : https://github.com/ashokitschool/my_shared_libraries.git ====================================== 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 ============================ => Scripted pipelines we will develop using groovy language 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 ======================================== => In this project we will use below tools 1) git 2) maven 3) sonarqube 4) nexus 5) tomcat Project Setup video: https://youtu.be/qJ8gUp0O25k?si=WhTqU6obnQbSUg4i ================================== Multi Branch Pipeline in jenkins ================================== => In one git repo, multiple branches will be available. a) main b) develop c) feature d) release => Creating seperate jenkins pipeline for every branch is difficult. => 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.