What Is Jenkins Pipeline | Jenkins Pipeline Setup Example With Github | Jenkinsfile For Maven Build

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is mukesh showtani once again from learn hive innovation.com today in this lecture we are going to talk about jenkins pipeline okay so in the last lectures we have already discussed that how we can create upstream job and downstream job and how we can run them right so we'll talk about what is jenkins pipeline why we call this a pipeline then we will talk about what is jenkins file because we are going to use the same jenkins file throughout this video then we are going to talk about how you can create your first chat games pipeline using jenkins file and the last approach other than the second way is you can create your pipeline using jenkins file but we will keep this zenkin file in our repository okay so let's do one by one so before we start this what exactly is pipeline so let's understand in a simple words so as we say pipeline it is nothing but series or collection of jobs or certain events or you can say steps which are linked with each other in a proper sequence which will help you to build your cicd pipeline okay so these steps can be simpler steps complex steps it could be any step which can help you to build your pipeline now if you go through the official documentation of jenkins pipeline you will get to know this a technical word jenkins pipeline is a suite of plugins that supports implementing and integration continuous delivery pavlen into jenkins so basically pipeline will provide you the extensive set of tools for building simple to complex pipeline as a code okay so that we are going to see now one by one so let's understand through a proper diagram so suppose when you're creating a pipeline let's consider a cicd pipeline so if you're completely new to ci ct don't worry i will have a dedicated video on what is ci what is cd again cd we have confused delivery and deployment we'll talk about cecd in detail but for the timing just consider that we have three stages okay one is your build which is building your application this application can be in java node.js javascript php python doesn't matter so the first stage is always build your application once your application is built then we have to go with the testing so in the testing you can have integration test unit test cases you can have smoke test end-to-end test and once the testing is complete we have to deploy the application okay very basic example so we have three stages whereas the three phases build test deploy so we can create this with the help of pipeline now these can be individual jobs as well and this can be simple stages in pipeline it's totally up to you how you are configuring your ci cd pipeline so we'll see this how we can write over jenkins file and how we can achieve this for this example i will be taking some simple steps but once you move ahead definitely you can write the proper steps to build your application if you have some test already written you can run some commands which will run your test and finally some commands to deploy your application fine so now let's talk about what is jenkins file because this jenkins file we are going to use throughout this video so as per the jenkins it's a plain text file which we call as jenkins file here you can write all the commands or i will say the styles which will help you to achieve a task now this jenkins file can be written in two ways one is declarative way and second is scripted way so we are going to use a declarative one which is easy to understand and it has riches and tactical features that you will get to know in few minutes okay so let me show you this what exactly the jenkins file okay and how we can create it so for this i will go back to my jenkins so this is our jenkins so make sure you start the jenkins server let's create one pipeline now so click on new item and the moment you click on new item you will get couple of options so till now we have seen a freestyle project we have also discussed about the maven project now we are going to use this pipeline so if you see the standard definition it will orchestrate the long running process or activity into multiple or it can span into multiple build agents this is basically for building pipelines which is known as workflow and you can organize the complex activities that you cannot you know easily do with the help of freestyle job types so this will give you more control so let's click on pipeline and here i will say my first pipeline okay click on pipeline and click on ok so guys make sure you're using jenkins2.x okay so you can see i'm using 2.2 double 7.2 so make sure you use jenkins 2.x any version of 2.x so as usual we have the build trigger options that how do you want to trigger this then we have advanced project options so if you click on advanced you can write the display name what exactly name you want to display and then you can click on the pipeline section so in the pipeline section again we have two options one is pipeline script and the second option is pipeline script from sem so we'll discuss both one by one let's understand first the pipeline script part so let's give the name uh i will give as of now a simple pipeline of first pipeline you can change it based on the requirement i am just keeping some name as of now okay i clicked on enter so it's saved actually let me go back let me go back to again pipeline option so this is the display name okay that we have set now if you see we have some samples created for you so if you click on hello world so as you can see now this is giving you one snippet okay this is basically a jenkins file snippet so maybe i will show you in a sublime text editor so if you see guys this is basically the pipeline if you see the pipeline keyword so this is the complete pipeline within this pipeline you can have multiple stages so when you say agent when i say agent like if your jenkins have multiple nodes connected you can give the any node here so as of now we don't have master slave configuration at this moment so it is set to any it means if any agent is free it will run right now we don't have any agent connected so it will run on the master then if you see we have stages you can have multiple stages as of now it has only one stage which is just hello and it has only one step so one stage can do this uh like this is the stages inside the stages you can have multiple stage so right now we have only one stage which is simply running this step so as of now the stage name is hello in the second example we'll change the stages and inside this stage we have only one step which is simply printing hello world so let's copy this and let's paste here it is exactly same it is just i have formatted so that you can understand okay so this is how you can see the complete pipeline i will just expand this again and let's click on save so this is our pipeline now let's click on build now the moment you say build now as of now since we don't have any data it says no data available and once the pipeline is done so now this will show you in a pipeline fashion as of now since we have only one state so you can see only one box and if you put mouse over it says the logs and right now the log says only eco which is printing hello world right so now let's do one thing let's quickly create three stages okay so this is my one state then i will create one more stage and finally one more so i have basically three stages so the first stage i will say build and here i will say build application you can write any ssh command or any windows command as of now let's try to understand this pipeline concept first here i'll say test application okay and finally i will say this is deploy okay and here i will say deploy application finally let's copy this let's go back to our job sorry pipeline click on configure again click on pipeline so this was our previous pipeline let's remove this and let's add this three stages and again click on save and click on build now it will take few seconds and it will start running and now you can see now we have three status one is build one is test one is deploy and if you see click on this and you will see the logs for each and every stage right now if i fail any of the stage okay it's let's say build is done but now due to some extra z reasons this test is getting failed okay so what i will do i will just give some uh i will change some steps here so it's not a proper command which is echo with double o this time if i save and if i run this it should run the pipeline and it should fail the second one which is test and let's see what happens okay so you can see the second state which is test has been failed and if i click on log just a second it's not giving log okay it's not showing here so let me give a try here fine so it's not loading properly sometimes you will get this inconsistent behavior in that case what you can do you can click on this console output so you can see here it says no such dsl method which is eco okay so they have given a proper uh you know steps which you can use and these are the list of steps that you can use since this does not exist or this is not valid you can go and just modify accordingly and the advantage of pipeline deck if one of the state is getting failed it will not move further it will simply fail the complete pipeline and you can see its failure right so now let's go back and let me change back to normal so it should run and you can simply start running it so this is the jenkins file official documentation please go through with this uh documentation very very beautiful on the left hand side right now we are using this using a jenkins file but you can also you know explore running pipelines uh scaling pipelines and the best part is pipelines syntax and the best practices this is very important link apart from this you can use here multiple agents environment secrets parameters and very interesting feature here if you come down to handling failures and parallel execution you can run you know the stages in parallel as well if i click on handling failures this is very important part let's suppose once your stages are done what exactly you want to do so you can see now we have a post so in the post we can mention what exactly you want to perform so you can use these conditions like always unstable success failure change okay so if you set to always like once it is done what it should do it should send email or if the you know the pipeline is getting fail it should send email or it should archive depends on how you want to configure it so basically you can use these conditions i will try to use uh always okay and for the always one we will try to send email so let's do one thing uh so this is my stages right so you can see this is my stage and this stage is getting completed here i will add this post okay so post i will use just right post and you can simply write post here again after post you want to give conditions right that if it is failure what you want to do if it is success what you want to do i will use failure one so again write failure and this is a failure condition but inside the failure what exactly you want to do you want to publish the report you want to send email it can be any other thing right now i want to send email now the question comes how you will get the syntax right so good part about this is you can see we have something called pipeline syntax the moment you click here it will open this you know snipped snippet generator where you can simply select what exactly steps you want to perform and it will give you the snippet for example i just want to run some windows batch command and let's say batch command is hello anything and if i click on generate pipeline script this is the snippet that we can use inside of a jenkins file right so it says bad hello um so maybe i will just note it down somewhere and again let's say i want to build a job so within the pipeline if i have to run certain jobs we can do that so it will simply ask you which project you want to build let's say i want to run one job call bill i already have one predefined job here if you want to see i will show you so you can see i have multiple jobs right so let's say i want to build this job as well and the job name itself is built so if i want to run this job let's say in that case here i will say generate and you can see we got the snippet so you just need to write build and give the job which you want to execute so you can see they have given beautifully what exactly you can use now what exactly i want to do i want to send email right so if i click on extend email extension or extended email it will simply ask few things to subject body and if i click on advance it will again ask reply to type attachment pattern and so on present post send and so on so as of now i will let's say give simple details that i want to send email to this guy which is my email selenium three by mukesh ottwani subject i will say pipeline status just a second and i will write something i'm just writing some dummy body but here you can write the complete body you can also use some environment variables build url build status and so on and if i click on generate pipeline script you can see i got the complete step here right so what i will do i will just write here it means once these stages are done in the post if my pipeline is getting failed it should send email right but what is happening in our case since it's a small pipeline where we have simple status i will keep this as always so even if my test uh you know pipeline is getting passed or failed it should send email let me copy this go back and paste here and now let's see so guys any syntax okay just go to this pipeline syntax select here and you will get the list of options okay it should okay sorry i have not clicked on build so let's click on build and yes so you can see build is done test is done deploy is done now it is doing the post action and it should have sent one email so i have already opened my email here so if i show you this can you see this pipeline status and in the body since i have written only summary descending only this body which is summary but as i mentioned you can write the proper body with all the environment variables that you want to use okay so this is how you can create pipeline so right now what we did we have jenkins file now what if i want to use the same jenkins file in my repository so in our previous session if you are following this series then we have created a small maven project where we have certain tests and this test is running via pom.xml file now suppose i want to run this pipeline so what we can do we can create a jenkins file and we can keep inside our directory okay so right now guys this is just a very small grid repository github repository but in real time you will have a dedicated repository with different branches there you can keep the jenkins file and it will build your complete pipeline okay concept will remain same so now let me create one more pipeline and uh or you can just click here call let's copy this first pipeline script from scm in that case what we are telling that we have a pipeline script which is nothing but jenkins file in our scm so it will simply ask you where exactly it is hosted will say git now it will ask you repository url so this is our repository url i will click on copy paste here now since this is public repository i don't have to provide credential but if it is public a private repository then definitely have to provide the credential and since now we have everything on the master branch so i can show you so if you have different branches you can configure different branches and it is looking for jenkins file right now we need to create this file so let's do one thing it should be inside the parent directory so i will click on create new file and i will give the file name is jenkins file make sure j is capital guys and i will simply copy this and i will paste here fine and i will simply commit new file let's go back and save this let's click on build now and you can see now this pipeline is running so let's wait since it's a very small pipeline with some commands it should run very fast so now if you see since now we have our jenkins file on our repository right so the first step is declarative which is checkout scm so first of all it is downloading the repository then it is checking do we have any jenkins file yes it found now it is running build test deploy and finally the post action which is nothing but sending emails right so that's what we need to do here and what we did right now we are still running this manually but if you want to you know run this the moment we make any changes so these options we already discussed right build after other projects build periodically github uh hooks also we discussed as of now i will use polling let's say i want to poll after every minute okay or so i will use star star star star star it means the moment we have any changes like basically it is going to poll after every minute so you can see every minute and i will click on save now click on this repository and let's do one thing let's make any change in any of the file since we have said the poll sem after a minute it will poll if it any changes it will trigger this pipeline so let's do one thing click on testing dot xml file click on edit i'm just changing some random name okay so maybe i will change this to test one and click on commit changes and let's go back here it will do the polling it will definitely detect the changes and it should trigger our pipeline automatically and yes it did it detected the changes and it will start running our jenkins pipeline right which is nothing but simple pipeline so i hope it is clear now in case if you have any doubt guys please let me know and before we end this video i just want to share one more example okay you can consider this a bonus or whatever you call it so let me create one more pipeline so that you will have more understanding about the pipeline okay right now it's just three stages and we are giving eco but this time we will have some additional steps i will say simple pipeline 2 again i will click on pipeline section and i will click on ok and let's click on yes pipeline okay and here what we will do instead of selecting hello world let's select github plus maven and now again we have some you know jenkins file so let me just keep it here and let's understand this so again as usual we have pipeline so this is pipeline end here then we have agent as usual it will run on any agent now you can see we also have tools it means if you want to use any specific tool before running these stages or if you want to use this tool in all the stages you can mention here now if you see this particular sample repository with jenkins guy have given given it is using maven right so we need maven so here you can just define which tool you want so as of now maven and since i'm running on my local system i already have configured maven underscore home right so i will say use maven and this the directory then again we have stages so the first stage is basically a build for build we definitely need the repository right so if you see in order to clone this we will just say git and the repository that we want to clone so this is one sample repository you will be getting so let's do one thing uh let's go back here and so this we will keep as it is now if you see in order to run maven on linux or unix agent you have to use sh right now since agent right now it is my windows system so we have to use this bat so as of now i will not use this maybe i will remove this additional comment this is only for our understanding let's keep it clean and we are going to use this bat fine and we are going to use this mvn clean package and basically we are ignoring any failures so this is how you can pass uh parameters in maven hyphen d key and value and again they have already given in post like what exactly you want to do so since we are using ignore failures even if the test will fail if some of the tests will getting failed it will you know archive of a jar file which is nothing but our artifacts right so i will remove this and again once we get success okay it should archive our artifact it should archive in our target folder and this is the wildcard like it should end with dot jar that's all so let's copy this and let's come back to this ctrl a remove and use this click on save let's click on build and let's click on this option and you can see so it started okay and finally the repository is cloned now it is running ambient clean package it is running certain tests so total seven tests one skipped and post build as you can see it is archiving our artifact if i go back here you go so this is your artifact cool right so this is another example so in the previous example we were sending email when the pipeline was success here we are archiving that effect so as of now we are not doing anything but you can publish you know push this artifact to nexus repository and then you can use this artifact for this further deployment okay so that's all for this video guys i hope it is clear so let me do a quick recap what exactly we covered so we started with what is jenkins pipeline right and then we discussed what is jenkins file and then we have seen a quick example of pipeline how does it look like we also discussed declarative pipeline then we took have taken two scenarios first scenario was we were writing the jenkins file right so one example was using simple three stages which was this right and we also added one post condition with always where it was sending emails how we got this we were getting this step from the snippet generator then we have taken the second example where it is running uh basically it is cloning this repository then is running our test and the post simply it is archiving the artifacts so that's all for this video guys i hope you have enjoyed if you have any question please let me know in the comment section and in case this video helped you if you get any understanding about pipeline please support this channel share this video with others subscribe this channel and i will see you in the next video have a nice day bye
Info
Channel: Mukesh otwani
Views: 7,468
Rating: 4.9572191 out of 5
Keywords: jenkins pipeline, jenkins tutorial, jenkinsfile example for maven build, Jenkins Pipeline Setup Example With Github, jenkins pipeline tutorial
Id: AqITZLJ5eZ4
Channel Id: undefined
Length: 26min 44sec (1604 seconds)
Published: Thu May 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.