Mega Real-time End to End DevOps CI/CD Project | Git | Jenkins | Nexus | SonarQube | HandsOn Lab - I

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to the complete new real-time project series I am vikas I am your host today and today I will be demonstrating about a new project like uh I am presenting in front of the screen like this is the code which we will have in our local that is basically a Java spring boot uh uh you can say it is a counter application like one whenever you will refresh the page it will it will start uh writing in a way like hello world one hello world 2 this will this is just a simple example of java and don't worry you can apply the same steps or stages going forward with any of the Java code so in order to worry about the code okay once we will push the code to the GitHub okay once we have the latest code we will trigger the Jenkins job we will also see how how we will write the Jenkins job it will be basically a Jenkins file it will have several stages it will contain like uh check out test integrate and integration test Maven builds and many more things along with that we will we have this Maven okay Maven will do basically what it will build your code test your code package your code either in jar or warp format correct here we have the sonar Cube so sonar Cube I'm keeping so that if you will go for a real industry level projects on our Cube should be integrated there like uh there should be a code coverage either it is meeting a quality gate status or not or or you can check your code like of how many errors bugs count these all things uh will be included inside the sonar Cube okay once you have the quality gate status is not failing if it is failing then okay we will send back the code to the developer saying hey your code is having more account it is not it is having it is having very less code coverage so they will again work on that other second scenario can be like if the code is working fine and so what we will do if the code is working fine it is passing the code coverage which we have defined it will push the code to the Nexus repo Nexus repo is something where you will store the war or jar files you will keep the snapshots you will basically maintain the version of the bar files suppose uh uh think of a scenario like currently your version the the application version on which you are working is 1.0.0 okay so for that if if it is com if it is not yet completed if you are still working on that okay so you will do what you will store it as a format of snapshot okay once this is ready then you like see if you if you are still working on that if you want to keep safe this word file then what you will do you will do 1.0.0 hyphen snapshot this is going to be the version if the code is completed fine then what will be the version okay 1.0.0 this is the one version okay and this version is not going to repeat if you do if you want to uh update the same 1.00 version again and again that is not going to happen okay so always for the next time it will be 1.0.1 okay so here we will see how we can create two Maven repos one for storing snapshot one for storing the release snapshot means as I told you if the code is incomplete right I have the code is complete we are launching that particular version then we will go for snap sorry release one once that is done okay let me show you what are the things we are discussing so what what will be the prerequisites uh you have to be ready with that so actually I'm not covering here the Nexus installation and the sonar q and the Never installation right because it is going to take too much time and you can you can go to any website and you can do the configuration and the setups right if you are facing any issue just comment down I will help you with the links and all you can find out the links I will keep inside the uh I will pin pin that link for the installation as well okay so here we are going to see the application overview application overview as I told that is going to be a basically a simple web application like on whenever it will run you will refresh it is going to show you one two three four five six okay the time you will refresh the time it will show you on the web page okay we will see the git use cases like uh we will have the code in our local okay we I already have cloned the code in my local right this is the code okay and this this you can see the branch name I have kept the code in the main the application now what I'm going to use that is a demo counter application okay that is fine once we have the code then we have to create the job so Jenkins job basically uh we will see declarity pipeline like we will have various stages so so as per the interview perspective they will they may ask you what is how many type of Jenkins files are there Jenkins jobs are there declarative and descriptive descriptive descriptive was old method like there was a node inside that node you will directly start declaring stage stage stage stage okay simply but now it is a very Diversified you have the declarative approach where you will uh you can use multiple agent with multiple stage earlier this functionality so so you can have multiple stage here so how so we will see that how we will write the declarity pipeline as well we will see once we have the code we have we can perform the unit testing if we have we will perform the integration testing with the same code we will build the chart or War files okay based on the need so in this project I will be building the Char application it is up to you in which format do you want to build your artifact okay so seventh is a sonar Cube configuration how you can configure the Sonata make sure you have you are already sitting with your sonar Cube so I will do this one RQ configuration with the Jenkins how to set up that that that also I am going to show and this you can say sonarq web configuration so this is also a most important part here because see okay we you your weapon is sending this the artifact to your sonar Cube to verify the bug count and the errors okay the what is the code coverage this all will be checked in the sonar key fine so Maven sent the code to sonar now sonar Cube should confirm to the Jenkins okay this code is fine okay this code is fine that is called Static code analysis okay it is a two-way handshake Maven is asking sending quotation R cube is checking the code if the code is fine then sonar Cube will respond to the maven sorry Jenkins okay then only the next stages of the Jenkins file will get triggered as I told okay now static code analysis is done okay what whatever I told you now quality gate status also we will check okay and come into the Nexus repo Nexus as I told you we will have a overview of that how what whatever the type of repo we are going to create that that will be a maven hosted type okay I will be creating two type of repo one for the release and one for the snapshots and we will see how we can configure and integrate with the Jenkins and 12th is error debugging if we have or some time left in at the end of this okay if we are not running out of time then sure I will go for the app deployment as well or I will create a separate video for that okay so let's go to our file and let's start building our things okay so this is my Jenkins dashboard this is my Sonar Cube and this is my Nexus okay all I am ready with that as this is as I have mentioned these all are as a prerequisite so I am going to the Jenkins dashboard I will click on new item okay okay okay but before that what you have to do okay let's go to that okay let's create a file like uh demo application then my application it it will be a type I have pipeline click on OK once this is ready we are going to you we are we will come here we are basically taking our code from there our code from source code management means GitHub so here it is the kit and it will ask you for the uh repo URL okay so just go to that because I have kept I will keep this repository as a public repo so don't worry about that just copy the URL from here and go back to your application paste here so if the if the repo is going to be private you have to provide the credential here by writing the uh if you will go here you have to provide the GitHub username and password okay but since this is a public repo we do not need these all things correct and in which branch your code is residing just go back see this is the main branch I just have us one branch so that Branch you have to mention here so just write this and apart from that he where will be the script path so I will keep this crypto path along with the code here only here only so no need to uh provide any extra data like essay will keep inside any folder like demo slash Jenkins and all but we will keep the Jenkins file with the code so no need to worry about that so just keep this so once this is done I will press apply and save so this is done now let's let's just go back to our code and I gave the overview of this code this is okay so let's create a Jenkins file here itself Jenkins file okay so this is basically a declarative pipeline declarative pipeline as I have told you pipeline isn't any stages under that stage there will be multiple stage stage okay what will be the first stage I am mentioning it check out fine under this stage there will be steps t e e s okay and here we need to check out the gate so if you don't know how to write the syntax of that you will just go back you will click on this pipeline syntax okay just click on get okay here user get I provide the repo URL again just click on that what are the name of the branch that is Main Branch fine there is no any credential because this is a private repo sorry public repo once you have this just copy go back to your code and just paste here fine once this is done let's just check this I I have added let's add some comment get checked out I am pushing it to that branch okay in the main branch so it our code should reach here let's see Jenkins file if we have got the Jenkins file here okay so we have got the Jenkins file now let's trigger the pipeline go to demo application just build the application and wait for some time so guys just keep attention keep doing with me because this is going to be a very very important project you are learning so not give static code analysis quality just gets Nexus repo configuration Nexus type of citric creation this whole thing we will see so stay tuned with me and just try from your side as well so okay so once this is done what is what will be our second stage our second stage is we will build the okay get use cases we saw we have we saw the Jenkins job creation as well right and after that what we will see we how we will do the web and unit testing correct so let's go back to demo application again go to configure okay why to go there we are riding our Jenkins file here right once this is done we will write a separate stage again correct so just copy that oh okay so what will be our second stage as I told unit testing fine and what is the command what is the command for that so for that we just have a simple command it is it is a type of cell script so mvn test make sure you have already installed the maven okay so without that that is not going to work so if we have added this just again let's update the code unit test cases updated once this is done let's push our code okay once our latest code is here what we will do we will again click on build now you can see the logs if you will click on this running Arrow now you just click on that the test is running we can see from the logs if it is successful we will see a success message so at the end of that we can see we have a success message right now the second thing third step is what we we have done the 11 unit test now we will do the integration testing okay so again just go back again just copy stage integration testing so understand what will what you have to type steps okay under steps you have to type uh the commands what whatever we you are going to use otherwise it is going to throw error because you are not using the steps inside the stages so make sure you are doing that so for that command is maven verify and these skips unit test you can get this command on directly from the internet so no need to worry about that so this is the because I am used to of it so that is why I am directly typing so Maven okay let's check the syntax as well verify D skipped There Is No Escape you will be kept caps and yeah that is fine let's check get add again unit test case then now integration okay now you have to push it to the repo so so see we we in the last once the code is ready we can do the GitHub configuration like whenever you will push the code from the local Jenkins job is going to automatically run okay but now we are we are currently in development phase so that is why we are doing these things again and again right so don't worry about that just keep typing with me and and enjoy the process Okay so now it should start doing the integration testing so once the integration testing is done we can go for you can see the command is running here right it's the command we wrote for integration testing this job is running let's see what is the output so it is also succeed so now both testing has been done so what we have to do if it is a Java application so we will do the maven build why we are doing Maven build because why because we need the water files we need the artifact right like if it is a.net code you will get the dlls okay that only like this is output you are going to put on any application by the output only your application is going to run not the code will go here it will just consider your artifact file your dlls file so in this case we will have our uh jar files right so now again we have to write the next stage stage what will be the stage now we will do our Maven build okay let's let me keep it Megan build ing of under that we have a steps what is the command simple as such 11 clean it will do well it will clean all the war files whatever is previously installed and all and it will try to install from the new Android so that is why it is cleaning and it is going to install resolver files again and again whatever libraries and packages we need we required to run the app this Java application so once Maven build is done again just click on get add update this comment as well so these are the best cases which we are following okay so don't get bother about this why we are doing again comment and also in my code you will always have the best practices I do not do anything without following the best practices approach so once this is done Melbourne Melvin build stage updated let's push it so this guy this is this is going forward this is going to be very very uh important as well as interesting just just keep just keep your eye on that and just try to learn and grab this all things so let's see so unit test is running now the number of stages are increasing it is better to let's see integration testing will will be passed only because they have it seen in earlier phase only so integration testing once this is done build is running right let's go to the logs to see what are the process which is going through okay so build this success you can see here so once the build is succeed we will have some uh snapshots so you can see this version of snapshot we have here okay so what I want let me do a quick uh change in my code so that it will be easy to you what I am going to explain you something for that I need to do this change so do not worry about that so okay so let me uh take this code as this version okay so once this is done what will be our next step once we have what we have seen once we have generated the builds we will send the artifact to the sonar Cube correct so we have already built like maybe mvn clean install we already did it has packaged the artifact now we will send those artifact to the sonar Cube so that sonar Cube can verify the number of bug count error code coverage and all fine so let's just go back here and let's do one thing let's create one more stage stage static code static code analysis fine steps and here we have to write what we have to write the command for the static code analysis things right so for that what we have to do we have to make a connection from the Jenkins to the sonar cube right then only it is going to contact with the particular sonar Cube server we can have many so not keep server but with this code it should it should it should connect with a particular sonar Cube server so for that what you have to do I have done the setup already but let me show you so for that what I will do let me just duplicate this page just go to the dashboard just go to manage and things so see for that you need some plugin okay so not your plugin you should have installed here okay so I have already installed that so once I will install I will see I can see the sonar Cube servers in the list okay so let me just show you let it load so once you will install this one article plugin sonar Cube server so see this is the sonar Cube server so let me show you which whatever all the plugins you have to install so go to manage Jenkins manage plugins foreign plugin I have already installed so I will show you here click on install Q so see these are the sonar Cube scanner for Jenkins so not keep generic sonar Cube Get It plugin after that you have to install quality get as well quality so see Sonata kits plugins quality gets plug-in so these are the plugins you have to install once you will install then only you will have the uh if you will go to configure system you will have this option when when you will install those plugin then only you will have this option so now I will show you how to configure how can I configure this Jenkins so let's see search for sonar Cube so this is what I have already created so what I what I will do let me just delete that fine so in your screen it will be something like this so just click on ADD sonarq and here you have to provide any name so what I will do I will provide sonar Cube server any name you can provide here okay now you have to provide default URL so what is the URL of your sonar Cube just copy and paste here so I have given the URL of that now here you have to provide the token of the sonar Cube okay so how you will do just click on ADD so this is not going to respond now so just save it again and again come back here go back to manage inkins go back to configure systems okay so for that token we need some uh of Secrets okay so that also we will generate so just go back go down just click on it see now the option is there so if it is not coming just refresh the page and come back here again okay and just click on that here I will pass just a secret text nothing else okay now the secret is secret how we will get the secret so this is what will help like URL already we have passed okay and this is what the it it is it will authenticate with the particular uh sonar Cube server so how you will get that go to Administration Administration click on security security click on users here you can see I already have a uh sonar key this what I will do I will just rework this okay enter token name so any token you can give like champions auth any name you can give just click on generate just copy this and copy and go back to your Secrets password API key so it is already in use so what we will do sonar Cube API instead of key I will use this add once adding is done just select the particular one that that is so not sonar API only right just click on apply and Save okay one more thing I would like to bring here one more Point like this this code this cop this uh key is going to uh once you will close this it is going to uh you cannot see back again okay so what you will do just see just save it somewhere else so that for later point of time if you need that you can again use because it is we have given the date as for it is valid for 30 days correct so we have done that setup now once we have done that setup what we have to do we need we need to authenticate so for that what we have to do we need to authenticate from the Jenkins right so we need some sort of codes again what we will do again go to the demo application click on Pipeline syntax again click here with sonar some plugin you will get because you have already installed the sonar key plugin so that is why you have here once you will click on that search for the particulars on archive API here so this is one which I have generated so not API I hope you remember don't worry about that do not bother it is it should not bother you just copy this block and go back to your code and just paste here so here you have to run the command sh so so now I have this sonar plugin okay so that is why what it will do it will just authenticate and it will try to clean and package those alert effect and it will try to send those all artifact to the sonar Cube server how it will con how it will authenticate so you can see this credential ID is there and this is the name in which you have already configured the sun RQ URL along with a secret so this is this will help the Jenkins to authenticate with the sonar Cube and send those artifact once this is done let's again add the code let's again commit the code static uh why sis so see with the time as the time is passing the code is going to be like in depth right your so once this is done push it okay many important thing is have been started from here okay this is a real industry level thing which you are seeing here okay once it is done just click on build now one more stage we can see we should see okay some letter we have this is also our part of task right error debugging no need to worry about that so missing required parameters starter field installation name with Q environment with sonar Cube environment and we have already provided the sonar Cube API what is the address installation name let's go back to our code and see static code analysis steps with say not Cube environments credential ID is there this is the credential ID I am using and after that ambient clean package so not give anything I am missing cript is maybe it is not inside this block because let's not check okay so let's try one more time let's see what is the output we are getting so okay let's just go back let's just click on build now again same required parameter installation name okay let me just debug this error and let me come back to you with the solution because it is it may take time so hello guys I'm back here and see I haven't done any any changes here okay like a stage I just I have I have just changed the name okay that is not any issue okay but I have just replaced the code again okay I have saved and again replace the code and it is working so we have wrote correct maybe some cache issue we can say but you can see the code I have wrote that is as it is okay so Maybe so let's see our status first so it is doing the sonar keratic code analysis right so let's just go back and let's just see the status what is the status we are getting it's not opening let's just wait for some time and you can see the logs from here as well if it will open why this is not okay I have refreshed the Jenkins let's see if maybe the issue with the server that is why server Rebooted apologies for late guys but uh actually the Jenkins server crashed so that is why I have to do the setups again and that is why we but we can see uh the main reason behind it was keep filling uh by giving the correct code also uh maybe that that could be a case but you can see now the sonar Cube analysis has been done so once it will once after that our part of code it it should have sent some code right the what the name of the same name code it should have sent somewhere so if we will go to the sonar Cube if we will click on the projects something should come here so you can see this is your application you have got okay so so so your Maven has this one R cube a plugin okay and so not you can see the Sonata I have wrote here the the maven using Sonar has packaged and published your code to the sonar Cube so that is why you have this okay so since this is a very very small application so it do not contain any coverage here you can see do not contain any duplication but you can see the lines of code if we have it is 72 and the code smells we have so this is this is the result you will get for bigger bigger code you will get many things the bug count the vulnerabilities The Hot Spot the code smells okay so our pipeline will not sell because it did not have any because it is meeting it this you can see this is passed so that is why it will go to the next stage okay so let's go again go back again and let's work on the what what is the next stage that is sonar Cube uh sonar Cube configuration so with the sonar Q static code analysis we we have this quality get a status check as well so for the real time project on which you will work this this scenario will definitely come that is why I have marked it as very very important so make sure make sure any project any application you are building this quality gate status should be maintained for that code this is very very important so let me show you let me show you by adding the stage okay quality get status understood keep your code very clean guys okay under steps we must try to must try to write scripts as well okay okay so now how we will get the quality gate status correct so for that you can directly go here to the Jenkins and click on the pipeline syntax cool wait for Quality get this one we need okay you just need to provide your authentication which we have given there just click on generate and just copy so I guess this is not given in correct format okay so we will do it not a issue under this it is not giving in proper format let's search quality gate so what are the sum for some of the plug plugin it is going to expire so that is why you may face these all issues okay so directly it is giving wait for Quality yet and about pipeline do we have anything else maybe this is updated so that is why I thought okay so maybe this has been updated so let's go back and keep it in our code let's see let's see what what is the output we are getting on the basis of that we will modify quality gate status okay earlier it used to be like a function you have to Define and you have to wait for the output but okay let's go back let's run the pipeline again and let's see so you can see this as well right when the last analysis has been done six minutes ago if we will run this pipeline we can see the latest latest ones just wait for this wait for the output foreign just check the logs now when things are going on step it is packaging those all things okay so we got some error let's check build succeed okay when it failed but it failed for the the CC no such DSL method scripts found it is there crypto found inside the quality gate status it is not a scripts it is a script sorry foreign the pipeline you can see here also uh you can see like this quality gates are passing or not right so instead of directly going to the Sonata if you do not have the access of that directly you can check from here also right as a developer as a devops engineer suppose you do not have access of that okay maybe the Java development team has the access of that they are setting the code coverage and all in that case this is what uh the board on which you will get your result instead of visiting there correct so let's see I'm waiting for the quality get status new code how that is functioning I'm excited to know if it is real then we will go for the older approach to not worry about that what we use in our code Okay so now tube task this status is pending so tell me uh before starting up the project only I was discussing something like there should be some relation between the Jenkins and the sorry sonar Cube and the Urgent kids cut it but here it is still waiting for the handshake because we have configured it so now Jenkins is authenticated with the sonar q but how about the like once it will verify the code count and also not queue so not Cube should respond to the Jenkins right like the result is success or fail it is not yet configured so for that we have to do the configuration otherwise it will it will be trapped inside this and less Loop right so let's just go back to the sonar Cube and build that compatibility click on Administration go to configuration you will see this web hooks part just click on that and there is no any web hooks right because earlier I have created but the server got crashed so any name you want to give Jenkins Web book I give okay here you need the uh this Jenkins URL okay okay once this is done just click on create that's it that's it now let's cancel this job and let's try to build it again just try to remember each and every steps what we are following that is going to help you a lot in your project as a devops engineer if you want to switch you can easily switch if you are aware about these all things you can explain this full scenario if somebody will ask you if you're in your previous project what are the things as a devops engineer you are managing so this is the result this is the scenario if you are going to explain right so the status is pending C the quality gate is okay and this is succeed okay so we have learned a new thing this part I have learned a new thing you also so quality get till quality get is done now we have a good artifact we have a good Czar files that we can by which we can go ahead with this artifact right this is a good artifact okay which is up to the mark correct static code analysis was succeed and the quality gate is also succeed right so that is why we can we can go ahead with this okay now next thing is what we have to go for Nexus repo so we have seen this configuration we have seen this webho configuration right two-way handshake we have seen the stages like static code analysis we have seen the quality geared status as well now very very important part guys Nexus repo overview so Nexus repo as I already told we will use for two things one to store the release and one to store the snapshot releases something when your when your software at that particular version is okay it is ready for the uh for the like Public Access right you will build some app you will deploy this application it is deployed Deployable ready application then it is a release but some more such features need to be added still but still we want to keep the snapshot of that particular then we will we will store this now we will store the artifact as a snapshot and into a different Nexus repo okay so let's just go ahead and let's just create a Nexus repo how we will create just go to Nexus repo make sure you have logged in so it is loading okay click on sign in let me so I am in so if you will click on browse you can see your all of the repositories correct but here I do not see any custom repository which I have created just click on the setting mark after the setting Mark just click on repository what type of repository we need we need so after that we click in this create repo we need a custom repository of what type I'm even hosted type right it should be hosted somewhere where we will keep our snapshot correct through the Jenkins Maven will build the artifact okay and Jenkins will push the artifact particular version of artifact which Maven has generated to this Nexus Repository double click here name something like demo app release perfect and see this is what defect this is what differ either you will choose release or a snapshot or mixed so this repo I have given the name as a release because I am going to use condition right so that is why I am using this so just click on leave it as it is just click on create repo simply that's it that's it guys so once this is done which one is our just double click on that perfect now what you have to do you have to uh make a connection between these Jenkins and the Nexus repo for that what do you need you need a plugin correct so since I have to install the plugin because again I have created the server so just click on available Nexus artifact Nexus artifact uploader click on that install without restart great Nexus artifact uploader is done just go back to the manage Champion section okay go back to the dashboard okay open in a link so pipeline syntax we will generate in a new tab right it is taking time if you will click earlier it used to it should open in a second tab but now do not know here we will we can see some Nexus things see we have once we have installed the plugin we will get this Nexus artifact uploaded click on that and let's see what the output area is asking so Nexus version which we are using that is Nexus version 3. make sure you are choosing this protocol will be same we are using HTTP only right it is not https that Nexus URL coming to the Nexus URL so how you will get the Nexus URL just copy this part go back paste here okay you do not need any URL must you do not need this correct see after that you need some credential correct so here what you have to do you have to pass the username and the password of your Nexus correct what is the username I am giving admin password of my Nexus Nexus Earth fine I'm adding Nexus are done now here you have to add the group ID how you will get this group ID just go back to the code go to palm.xml and see here this this this one you will use okay so you will use this com dot example this will be the group ID okay what is the version version must not be empty so current version I am doing hard coding so I will show you how we can write it dynamically as well so do not worry about that but currently our version will be this what is the repository so just go back here see this is the repository you are hosting right for maven go back go back paste here once you have done this you need to go to this artifact right so what will be the artifact ID type classifier and file so for that artifact ID again just go back to the code and see this is your artifact ID copy paste here as I have told what is the type of forward application so it will be zar so I will not use any classifier so file so usually usually if you have used Maven earlier you are aware about that whenever you will build install and do Maven will generate a install or this target folder right so inside the target folder you have what you will have according to my application you can see I will get something like this is the final name I will get and what is the type uber.com Uber maybe let me check this spelling please Uber dot Zar once I have filled everything just click on this generate so see this this is what the Wonder this pipeline syntax generator do just go back to the code Jenkins file again you have to create a next stage right upload Word file to Nexus stage upload wire file to guys try to try to do Hands-On okay that that will give you a confidence to speak in front of the interviewer or perform any tasks if you are not doing anything just watching that is not much going to help you you will just have an idea but when the and when you will go to the Real Time Project you will be silent that time so just try to follow me and do these all steps so under steps what I have told you script not Scripts okay once this is done inside this is paste the huge command so now what we will do let me just format this for readability right so anybody can easily understand what we have wrote credential group ID what is the Nexus URL what is the Nexus version HTTP protocol which ripoff Nexus this repo of Nexus what is the version of your upgrade version of your application so once this is done we can run this application and let's see you get add so what what what we have added now Nexus stage updated so let me push it to the origin and let's see it should push the particular artifact like 1.0.0 so the demo release repo of the Nexus correct so let's just go ahead and let's just run this pipeline again let's wait for the Wonder so this looks very beautiful whenever you will start adding the stages right and if it isn't showing green Mark you will be really happy okay you can check the logs from here as well correct so you can see this build is succeed you can cut down from here Maven build you can click on that you can see the commands here it's not updating yet so better just get inside this okay building the jar files okay so sonar Cube things are getting done disqualified it has pushed over pushed our spring boot 1.0.02 where to the Nexus repository demo app release correct so just go to the Nexus repo how you will check that if you will click on repo this is what you have created but how you will get the data so for that data click on this box now go to browse and what was the name of your demo app this is one demo app release just click on that see this was the group right com dot example this was the application name and this is the 1.0.0 you have passed correct this in this repo correct this is the group ID com dot example and this is the artifact ID right spring boot correct so this is how you can upload the war files correct but now what do I want suppose think of a scenario like are you going to again and again change this value this is not your job right whenever the Java Java developer will do the modification to their code and they will they will start building a newer version correct so your code should automatically pick that version correct right now the version you can see go to com.xml 1.0.0 next time the they will push a code with 1.0.1 so this is not looking good right you you will again check this version you will go back to the Jenkins file and you will update the value now that should not be a good approach so what we will do we will do it like in we will do it in a dynamic way correct so for that what do you need first step First Step guys just go back to the dashboard you need a plugin again for that okay so that's what make Jenkins very beautiful okay okay just go back to the manage plugins Okay click on available and you need a plugin named pipe to line utility steps Yep this one this is one so now you can click on this install without restart okay so it's been installed again go back to your code where is your code our code is here okay so we want to write it dynamically correct so just search search how you will search that read form dot XML using pipeline utilities click on that it should show okay this is the plugin just go inside search for uh Palm click on that this is what will help you so it is simply you can write Now function correct see you can simply write your own function so how it how you can read that file let's check okay so see read file equals to rear digestion that this so we will use this this one okay read Maven Palm make sure you remember this keyword so now where we will write in the script section we will Define a variable Define like read uh what will be meaningful Maven Palm Verizon equals to how how we will get go back here copy this simply and paste here correct so it will automatically read your form.xml and it will get the version okay how it will get the version that is a question right so copy this form version and where we want this just go here foreign that's it fine now no need to change here now I hope you you saw right I have changed it to okay I haven't changed I will make this developer has committed the code with 1.0.0 version fine so now let me push this so what what will be your commit new version updated fine just push this code to the repo now we will release this version of code so we are releasing correct it will go to the release repo so just go back just go back to this just click on this and rerun the job foreign testing is all going just keep patience guys because whatever I am showing that is very very important now and when clean install is going on so we are basically cleaning the previous packages and again we are Packaging with the new codes so this is the sonar Cuban environment variable I have been called here okay so the jar file is here so okay so now to keep things is going so it failed maybe some issue scripts not permitted to use model get version approve a reject maybe some administrator permission it requires approve approve I don't know why this came but okay let's run this pipeline again foreign file now let's go back to our repo okay let's refresh okay let's check 1.0.0 refresh 1.0 dot one came here right now we will see the most important part of this as well so what do we want what do we want whenever snapshot snapshot is ready how we will make sure if we are releasing a snapshot so see this if if it is in this way like it if it is in numerical format 1.0.1 this is a release if you want to release a snapshot you will do if you you will write like this snap shot correct so let's do one thing let's generate let's generate a snapshot of this version okay 2.0.0 version is we want to generate this is Snapshot correct how we will identify by this keyword correct so now what we have to do we have to write conditional so before writing the conditional okay let's let's write the conditional first like def accepts uh uh here we need we need to choose switch between to repository correct using that particular keyword this is our condition so Nexus repo equals to read form dot version okay in this version read if this version ends with this version ends with which keyword if this person ends with snapshot keyword snapshot keyword then what you have to do if this version is ending with snapshot keyword then choose this repo otherwise not then choose this repo okay so otherwise not then which repo we have to choose this release one right this really release things will not have this snapshot keyword so just test this just check copy this and simply pass this pass this here okay and we need one more repo right to store the snapshot just go back again just click on the setting icon click on repositories click on create repositories go to Maven to host it correct click double click on that and Now demo app name something meaningful snapshot so that we can identify now see which type of version policy snapshot type this time earlier we chosen release this is Snapshot one create a repository simply what do you need from here use every value variable we already have the repo and all see these all things will be same right with this is this belong to the Nexus you just need to change this demo snapshot just go back where is your code okay this repo correct great great so let's check either our conditional is working fine so now this version of a snapshot 2 2.0.0 hyphen snapshot should be stored into the new repo demo app snapshot correct okay let's push it to the origin man so whatever you have done till now what else you can do you guys like you can just parameterize these all things you just parameterize these all things okay using environment variable this this is going to be your task okay otherwise if you're facing an issue doing that also I will I will show you just drop down into comments any type of issue you are facing just drop down your queries in the comment I will try I will try my best to answer those all queries okay because whatever errors we are facing we are already discussing during the project time only so so that you can do your smooth building when you are doing in your system build is succeed ambient clean install is going on okay it should push to the sonar now so that is why it is packaging the code once it will package it will push this artifact to the sonar Cube it it has pushed you can click on directly on that link okay you can directly click on that link okay don't worry about that let's see if this go to projects it should be updated so if 24 seconds ago right okay now let's just go back to our error no such property read Maven Palm okay what wrong we have done read melon form okay it it is it is read Maven Palm version correct guys I hope you have wrote down correctly okay okay so just add the code so I love to do the errors in front of you guys so that you can also able to debug with me okay so that for you it will be easy to do that so just don't get bored try to learn on every Steps From the failure okay that only will help you to grow and improve yourself okay without error if you will do you will not learn so much thing okay you because you are working on a frame but errors are something which will help you to uh come work beyond your limitations correct okay so let's wait until this code is done and we have already crossed like one hour plus so we can build and publish this application as well what do you say okay let me let me do that as well let me do that as well let's just cover let's just make this project completely end to end now so it is succeed so let's just see the Wonder now just here go back click on browse click on a snapshots see perfect right so today we have learned so many very very important things guys so now what we will do we will try to build a Docker file so that we can deploy our application as well fine so let's just go ahead and let's just create a Docker file here so I will simply create a multi-stage Docker file okay so using that Docker file so so see what you can do you can directly uh whatever code you have built in your system directly package those code and send it to the so directly like you are already building this snapshots right so that also you can use in your uh in your code right so what I will do here I will show you like how to write multi-stage the docker file so that is why let me go ahead with the multi-stage things like here also we will generate the artifact and we will copy the artifact to the second stage of the Docker file okay so how to write that we need first base image as a maven right using Maven only we can build our artifact so let's name something so this is one which will help you to differentiate as a multi-stage thing this is called Alias so whenever you want to call this first stage Docker file this name you will use okay and let me keep any work directory so work directory I will keep as an app so in container in container anything we will store or do anything it will go here only in the slash app if it is not there in container it will create once this is done what you have to do you have to copy what you have to copy what whatever you have to build what you have to build this code correct copy all the thing from present working directory to the slash Sam okay slice up already you have to write in the container it will do what this is your current working directory means this code current working directory and this present working directory is what for the container that is already slasher so this dot means slash swap of the container so once this is done you have to run some Command right what command we will use mvn install simply this stage is done now we need a second stage correct because we are building a multi-stage Docker file so I will use open jtk open jdk version let's take 11.0 and this this you will get from the docker Hub so just search this image name open jdk version you can use the latest as well but I am using this uh of this version okay so now for that also I'm setting a work directory what will be the work directly Slasher so we will copy we will copy the artifact whatever is built from first to second so this is best practices guys okay this is a best practice instead of instead of copying these all things instead of creating one Docker Docker file to build your artifact and want to build your copy your code correct copy to compile your code instead of that what you will do you will multi-stage Docker file does what simply it will generate the artifact and in the second stage we will copy that effect so the image the image whatever we will create using this that is going to be a very very lightweight okay otherwise if otherwise if you will write two Docker file the image size will be very very high current so it will get extracted once this task is done it will generate the artifact we will copy the artifact and it will automatically get destroyed so let's copy how we will copy from from which I told you from the Alias what is the name of the Alias this fine and you have to provide the name of the like from where from where you have you want to compare so it will be in Slasher Target correct and what will be the name it will be uber.zar hope you remember uber.com from where it has to copy here or you can write directly dot as well correct so okay so let's expose this at what port do you want to expose this let me take 1990 and you have to run some executable right so that the your container will be always running correct otherwise it will do a task and it will get started but some thing will be running inside the container then it will be always up and running so just run the command Java hyphen char or what is the name of your jar file Uber dot Tsar so it looks good exposes nothing guys that you're writing in a Docker file so when whenever someone will read this Docker file he will know like okay this this port I have to open okay this port I have to open so that the image will be accessible so once we have done with our Docker files so what we have to do we have to compile this correct so let's build just let's just go to Jenkins file again where it is ending check okay stage uh docker image build stage under steps it is steps only right steps under steps where here it will be script correct inside the script you will write script what will be the script the command the command of the docker file so and it under Docker make sure Docker is installed on your Jenkins then only you will be able to run this Docker command so for for me Docker is already installed so that is why I am directly keeping the command Docker image Docker image built I send t and here what I will do what I will do I will just uh I will just make this version the tagging version dynamically like whenever a Jenkins build a number run right see this is called the build number 99 right so whenever we will build the pipeline based on that the tag should get updated correct so for that what we have to do we have to we have to tag in that way so we we have some Jenkins variable okay so let's uh let's build that image first so that variable name I will use like with the Jenkins uh it is done job name and uh any version Dot build ID so what will happen guys it will build the image in this way correct and what is the job name what is the job name job name is demo app demo application demo application and build number is nine cut it so this is this this will be the name job name dot version 1.9 okay so once you will build that image you can tag those image as well so that you can push it to the docker Hub so how you will do that so first tag these images so I will I will basically maintain two version of the docker images correct one with the one with the version number like dot 1.9 1.10 and one with the latest okay so for each version I will have a latest image as well so let me tag those images as well search Docker image tag which image to tag this image to tag Docker image tag to which so because we we want to push this image to the docker Hub correct so [Music] so make sure you have logged into your gen Docker Hub account as well so see guys this name this name you have to this name you have to use and tag your Docker images then only it will go and sit inside into this particular account correct just go back just keep this tag and after that what I told I will just maintain version first so just copy this okay along with the version what I will do I will maintain the latest image as well so instead of build ID or version what I will do I will simply write let test with this image One latest version correct so the system I'm building that image and I am tagging you can include this in single way like while building only you can directly two times build the image and you can tag by this name simply directly but for simply Simplicity I have done this okay now what do we have to do we just have to run this code okay get add git commit image updated okay okay but for building you missed one thing right we have to build the doctors file so where is the docker file we have the docker file here only in the current working directory okay so let's run it again otherwise it should throw error correct because it will not be able to find the docker file once it will push let's rerun the Jenkins image let's go back click build now so right now we do not we should not have any images let's check docker okay Let me refresh this meanwhile let's check our status okay it's fine okay we haven't changed the snapshot version okay Dockers LS so we just have our sonar Cube running here right we do not have a image for the web application so it should get created let's see so it is pulling the image right see docker image build this application it is running the first step is running from webinars build okay see it is generating the artifact now whatever we have wrought in the first first stage of our Docker file correct so let's wait until it create our images we have already showed we did not have any images so it is currently packaging those all things whatever is required to build the part okay so now see we are in a stage two it will copy all the artifact it has generated okay it is pulling so see guys what are the best practices of writing a Docker file first is okay it failed we will see this file okay okay what are the error you can see here Target okay so see what are the best practices of writing a Docker file first uh check like with your code with your code uh extra files are not there right because any extra file you will keep here now apart from your code that all will be sent to your Docker diamond and when you will create the image the image size is going to be very very huge first second is always prefer uh always prefer Alpine images okay because that is very very in lightweight and third thing is always use multi-stage Docker file okay whatever is not required first step it will generate in second step it will it will remove so what was the issue Target so let me build the code and come back to you once this is done because again it will take a lot time once the image is built I will come back to push the pushed it let me build the pipeline I have ran the pipeline let me come back once the system so guys we can see now it's been done got it so now it has been it should have been built two images so let's just go to our reset instance and check Docker container LS okay Docker image right we have not yet created content at docker image LS so see this is what demo application with latest and this image with this build so this is our Docker Hub account name this is our job name right and this is our version of the Jenkins current build number current wave number is 11 and this correct so we are on the right path so now once this is done once this is done what we have to do we have to push the image to the darker half correct so for that again we need to create a stage stage push image to the Dockers correct so okay so for that add steps add a script and here you have to write the command so before pushing the image to your Docker Hub what you have to do you have to login you have to log into the uh your Docker have first right so for that you have to use you have to store the credential of your Docker Hub to the Jenkins so for that just go to the Jenkins this is your Jenkins just go to the plug-in side okay go to the dashboard okay so we need a with credential option correct so basically what you will do okay just go to the syntax generator simply syntax generator here with the credential with and see here you can bind your variable okay so here you have to add like what do you need you need username with password correct so it will not be get but why it is okay let's see Docker Hub account name is okay here I have to type the password and here Docker hub okay once this is done just select that particular generate git username okay maybe we have chosen something wrong okay binding simply okay let's do one thing I just need to hide the password right so what I will do I will just click on secret text variable will be docker hub grade correct let's add the credential click on that choose what secret text only bind the secret your password of the docker hub git creates git grades select that generate that this is what I was looking for now I will go back I have to I have I have the password of my Docker so before pushing it will it should authenticate with a particular account right otherwise it will start pushing anywhere so that is why we have tagged the image with a particular account ID now once this is done you have to write the command to push correct so what is the command to push that Docker image simply so first you have to login right so as such Docker login I send you what is the username type here type here your Docker of account username guys not mine that is not going to work and here you have to type the password so how where where is the password this is the variable which is containing the password correct so let me show you how this is working if you will go back to this citro instance see Docker login I send you username password unauthorized username or password correct so I just shown you the way how to do it do not try to access that account because you won't be able to do that because the password was incorrect so now I have to write the command so that I can push the image to the docker so what is the command docker which image so select those particular image this is the first and uh second is the dot tag once this is done we can push our image to the dark cutter Dockers have image push I have given a wrong password so I need to change that so let me do it first so this is my Docker hub I will click on that click on this Global credential just go here if you want to update from your site okay so this is this is the way you can do so let me just change my password so so let me rerun the pipeline now okay now it should push our Docker image to the docker Hub correct so let's just be patients keep patience for a moment until this is done maybe some issue may come so we will resolve that so do not worry about that you started passing the test cases so build a succeed now now sonar came in picture again it is packaging those all codes so that it can send to the sonar queue for the code validation the bug count and all okay so now quality git is running it has sent no Docker image it should not take much time because already it's been stored into our cache many things so it will not take much time okay so it failed why because of the invalid username and and correct username or password so let me just check and resolve that issue so we are able to push now I will tell you what was the issues I was there I was doing like see the credential idea I was taking here like git credential okay instead of that I have to use the particular credential ID which I am using for them this right so that is why a tissue was there right so now coming to that like I it you can see the pipeline is successful now we should have seen our image should be there into the docker app so you can see a few seconds ago it's been pushed here so I will just go inside this so see I have the latest version of that image along with that I am having a version particular version of that image correct so now this is the thing so we have seen till Docker up push okay so now in the next video I will be deploying this image to the E case kubernetes eks cluster hope you remember like we have created an infrastructure using data form so on that I am planning to show you how to deploy I like deploying on a Docker or or kubernetes cluster mini Cube if you are if you have a random kubernetes cluster using mini Cube that is very very easy but for deployment on eks cluster there will be some challenges I would like to share with you all so that is why I am planning to make a second part of this video So based on your responses guys I would be I would be able to record that video as soon as possible so please please like like share and subscribe with your friends and must comment about what are the changes do you want in this project what are the more thing I should include and one more thing guys please if you like my video if you like my video if you are learning really a lot just drop down your feelings in the comment section I will I will respond to that thank you very much keep learning keep crunking bye
Info
Channel: Mr. DevOps
Views: 117,705
Rating: undefined out of 5
Keywords:
Id: Yk7k3yEguQA
Channel Id: undefined
Length: 102min 40sec (6160 seconds)
Published: Sat Oct 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.