=========================== What is Build & Deployment =========================== => Take latest source code from git repo => Build project using build tools like Maven => Perform code review using SonarQube => Create Docker Image => Upload docker image to artifactory server => Deploy docker image in k8s cluster Note: Performing manual build & deployment is time taking process. => Everyday we have to perform Build & Deployment process in several environments like DEV, SIT, UAT and PILOT. ======== Jenkins ======== => It is free and open source s/w. => Jenkins developed by using java language. => Jenkins s/w is used for CI CD operations. CI : Continuous Integration CD : Continuous Deployment => CI CD is used to automate project build & deployment process. =============== Jenkins Setup =============== https://github.com/ashokitschool/DevOps-Documents/blob/main/01-Jenkins-Server-Setup.md Note: Jenkins will run on 8080 port number. We have to enable this in EC2 VM security group inbound rules. ============== Jenkins Jobs ============== => Jenkins Job contains set of steps to perform project build & deployment process. Ex: Step-1 : Git clone Step-2 : Maven Build Step-3 : Code Review Step-4 : Create Docker Image Step-5 : Push Docker Image Step-6 : K8S Deployment Step-7 : Send Email Notification Note: Jenkins Jobs we can create in 2 ways 1) Free Style Project (UI) 2) Pipeline (code) => If we have simple process to build and deploy then we can use 'Free Style project' => If we have complex build and deployment process with several tools integration then pipeline is the best option. Note: Jenkins Pipeline we can create in 2 ways 1) Declarative Pipeline 2) Scripted Pipeline (Groovy language) =========================== Creating First Jenkins Job =========================== => Login into jenkins => Select 'New Item' => Enter Name and Select 'Free Style project' => Select Source code management and configure git repo url and branch name => Click and Save and Run the Job using 'Build Now' option. ============================================= Jenkins Job with Git Hub + Maven Integration ============================================= => Configure maven as a global tool in jenkins server. - Go to manage jenkins - Select Tools - Find Maven and Add Maven Installation Name : maven-3.9.9 Note: If we configure like above, jenkins will download maven s/w for us. Step-1 : Create Free Style Project Step-2 : Configure Git URL in sourcecode management Step-3 : In 'Build Step' select 'Invoke top level maven goals' option - Select Maven name we configured in global tools - Enter goals as : clean package Step-4 : Apply ans save the Job Step-5 : Run the job using 'Build Now' option. ========================================== Jenkins Job with git hub + maven + tomcat ========================================== 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) ================= User Management ================= => In our project multiple teams will be available. 1) Development Team 2) Testing Team 3) Operations Team Note: For every team member jenkins login access will be provided. Operations Team : Responsible to create/edit/delete jenkins jobs Devlopment/Testing : Responsible only to run the jobs. Note: By default, Every logged in user can perform all operations in Jenkins. Ashok ==> DevOps team (all permissions) Rani => Testing team (only job run permission) =========================== Role Management in Jenkins =========================== => We can provide access to users by using roles concept => In order to manage user permissions using role we can install below plugin Plugin Name : Role-based Authorization Strategy => Create Role => Assign Permissions for the role => Add Users to the role ======================================== 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 public 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) ------------------------------------------- ================== Jenkins Pipeline ================== => Jenkins Pipeline is a way to define CI CD process as a code. => When we are dealing with complex CI CD process then pipelines are highly recommended. => Jenkins Pipeline contains set of stages Stage-1 : Clone git repo Stage-2 : Maven Build Stage-3 : Code Review Stage-4 : Artifact Upload Stage-5 : Docker Image Stage-6 : Push Image Stage-7 : K8S Deployment Stage-8 : Email Notification => We can develop jenkins pipeline in 2 ways 1) Declarative Pipeline 2) Scripted Pipeline (groovy) ============================= Jenkins Declarative Pipeline ============================= pipeline { agent any stages { stage('Stage-1') { steps { echo 'Hello World' } } stage('Stage-2') { steps { echo 'Hello World' } } } } =================================== Jenkins Pipeline with Git + Maven =================================== pipeline { agent any tools { maven "maven-3.9.9" } stages { stage('Git Clone') { steps { git 'https://github.com/ashokitschool/maven-web-app.git' } } stage('Maven Build') { steps { sh 'mvn clean package' } } } } ============================================= Jenkins Pipeline with Git + Maven + Tomcat ============================================= => To deploy war file into tomcat server using CI CD pipeline we will use "SSH-Agent" in pipeline. => SSH agent is used to establish remote ssh connection from one linux vm to another linux vm. => Using SSH-Agent Jenkins server will copy war file into tomcat server. => Install SSH-Agent plugin in Jenkins => Manage Jenkins => Select Plugins => Check Available Plugins => Search For SSH Agent and install it ======================================== How to conigure SSH Agent in pipeline ========================================= => Go to jenkins pipeline and click on pipeline syntax option => Select SSH Agent in dropdown => Add Tomcat Server credentials with username and private key => Generate Ssh agent code block sshagent(['tomcat-server-credentials']){ // logic } => Inside SSH Agent code block we will use 'scp' command to copy war file from jenkins server to tomcat server. Syntax: sh 'scp -o StrictHostKeyChecking=no => source is where war file is available => destination is tomcat server webapps folder => Below is the Jenkins pipeline with 3 stages pipeline { agent any tools { maven "maven-3.9.9" } stages { stage('Git Clone') { steps { git '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/maven-web-app.war ec2-user@3.110.154.247:/home/ec2-user/apache-tomcat-9.0.97/webapps' } } } } } =============================== Email Notifications In Jenkins =============================== => We can configure Email Notifications in Jenkins. => After Jenkins job execution we can trigger email to project team members regarding job execution status. => To send emails from Jenkins we need to configure SMTP properties. SMTP : Simple mail transfer protocol Note: In realtime we can use company provided SMTP properties but here we can use gmail SMTP properties for practice. SMTP Server : smtp.gmail.com SMTP Port : 587 username : your gmail id password : gmail-account-app-pwd (not login pwd) @@@ URL To generate gmail app pwd : https://g.co/kgs/P754MXf ======================================== How to configure SMTP props in jenkins ======================================== => Go to manage jenkins => Go to system => Go to "Extended Email Notification" => Add SMTP details like server and port => Add authentication with gmail id and app-pwd => Select TLS checkbox => Below is the sample pipelie to send Email notification as post build action. ------------------------------ pipeline { agent any tools { maven "maven-3.9.9" } stages { stage('Git Clone') { steps { git '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/maven-web-app.war ec2-user@3.110.154.247:/home/ec2-user/apache-tomcat-9.0.97/webapps' } } } } post{ failure { emailext( subject: "Build failed" body: "The build got failed" to: 'ashokitschool@gmail.com' from: 'ashokit.office@gmail.com' attachLog: true ) } success{ emailext( subject: "Build Success" body: "The build got success" to: 'ashokitschool@gmail.com' from: 'ashokit.office@gmail.com' attachLog: true ) } } } ------------------------------ ======================================================= How to work with parallel stages in Jenkins pipeline ======================================================= => Jenkins job contains multiple stages like below and all the stages will be executed sequentially. stages{ stage('git clone') steps{ // logic } } stage('mvn build'){ steps{ // logic } } stage('code review'){ steps{ // logic } } stage('nexus upload'){ steps{ // logic } } stage('docker image'){ steps{ //logic } } } => Inorder to save time, we can execute multiple stages parallelly like below pipeline { agent any stages{ stage('git clone'){ steps{ echo 'git cloning.........' } } stage('mvn build'){ steps{ echo 'maven build.........' } } stage('Parallel Stages'){ parallel { stage('code review'){ steps{ echo 'code review........' } } stage('nexus upload'){ steps{ echo 'nexus upload' } } } } stage('docker image'){ steps{ echo 'docker image creation' } } } } ====================================== What is shared library in jenkins ? ====================================== => Shared Library means collection of groovy scripting files that we will use in jenkins pipeline. => Shared Library is used for groovy scripting re-usability. => When we have multiple jenkins pipelines then some common logics will be available in multiple piplines like below hotels-api ===> jenkins pipeline flights-api ===> jenkins pipeline trains-api ===> jenkins pipeline => Instead of writing common logics in every pipeline, we can create shared library and we can re-use it. Note: Shared libraries we can store in git repo. ======================================= Working with Shared library in jenkins ======================================= ## Step-1 : Create shared library and store it in git repo @@ Git repo URL : https://github.com/ashokitschool/my_shared_libraries.git ## Step-2 : Configure shared library in jenkins => Manage Jenkins => Configure System => Global Trusted Pipeline Libraries => Click add to create new library => Give unique name for library (ex: ashokit_shared_lib) => Default version give as branch name (ex: main) => Source Code Management (select as github) => Project Repository : Enter above given git repo url => Click apply and save it. ## Step-3 : Create jenkins pipeline and use above created shared library --------------------------------------------------------------------------- @Library('ashokit_shared_lib') _ pipeline { agent any tools{ maven "maven-3.9.9" } stages { stage('Hello') { steps { welcome() } } stage('Git Clone') { steps { gitClone() } } stage('Mvn Build') { steps { mavenBuild() } } } } ---------------------------------------------------------------------- ============================ Jenkins Scripted Pipelines ============================ => Scripted pipelines are used to define CI CD workflow using groovy scriping. => Scripted pipeline will provide more flexibility and more control on pipeline stages execution. => We can implement error handling logic in programmatic way using scripted pipelines. ========================= Scripted Pipeline syntax ========================= node { stage('git clone'){ echo 'hi' } stage('mvn build'){ echo 'hello' } } ============================================================================= Assignment-1 : git + maven + tomcat integration in scripted pipeline Assignment-2 : How to take jenkins backup & restore using thin backup plugin @@ Ref Video : https://www.youtube.com/watch?v=5Tb-AOUFuKQ&t=2s ================================================================================ ================================== What is Multi Branch Pipeline ? ================================== => Git repo is used for source code integration. => In git repo we will have multiple branches a) main b) develop c) feature d) release => Jenkins pipeline taking code from particular branch and perform build and deployment process. => When i execute jenkins pipeline it has to build the code which is available in all the branches of given git repository. Then we can use Multi Branch Pipeline. ================= Jenkins Summary ================= => Build & Deployment => Challenges in Manual Build & Deployment => Automated Build & Deployment => CI & CD => Jenkins Introduction => Jenkins Setup in Linux => Jenkins Job Creation => Jenkins Build Trigger Options => User Management in Jenkins (RBAC) => Plugins Management => Git + Maven + Tomcat + Jenkins => Master & Slave Configuration => Jenkins CI CD Pipeline => Declarative pipeline creation => SSH Agent configuration => git + maven + tomcat + jenkins Integration with pipeline => Email Notifications in Jenkins => Shared Libraries in Jenkins => Parallel Stages Execution in Jenkins => Scripted Pipeline => Multi Branch Pipeline