Build & Push Docker Image using Jenkins Pipeline | Devops Integration Live Example Step By Step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to java techy in this tutorial i'll demonstrate how to build and post docker image to docker hub using jenkins pipeline okay all right so before i start this session i am assuming that you guys have basic knowledge about docker and jenkins if yes then well and good at the end of this tutorial you will understand complete end-to-end devops integration flow if no then i would strongly suggest you to check out my docker and jenkins playlist so if you will go to my java techy channel you can find two playlist called docker and jenkins so you can refer this to playlist now without any further delay as usual let's demonstrate this devops integration flow step by step so let's get started [Music] so let's understand the integration workflow as a developer we commit the code to the github which act as a version control tool now next we need to perform continuous integration and continuous deployment so for that we need to integrate our github with jenkins when i say continuous integration and continuous deployment it means with my committed code i want to build my project then i want to create docker image of my application then i want to push the docker image to docker hop now the question here how will jenkins know what steps you want to perform whether first you want to build your project then you want to create docker image then you want to push docker image to docker hub how jenkins will know these steps as part of continuous integration and continuous deployment so that's why the jenkins declarative pipeline concept came into the picture where you need to define all the steps what you want to perform in a file called jenkins file so now going forward when you commit your code to the github jenkins will read this jenkins file from your project and will perform the request stages you mentioned how simple is this so let's quickly do this in action first let me create a project then i will integrate that with github then as per the flow will follow the same step okay so let me go to my intellij id then as you can see already i created one small application if you go to the src main nothing is there i just exposed one rest endpoint that's it and if you go and check in your palm.xml i just added one web dependency i could use any existing application rather than create a new one but let's do it from the phrase now as per the presentation first we need to integrate our application to the github right so i will go to the github i will create a separate project go to the github.com then i will go to javateki then i will create a new project here new then i will just name it debuffs automation okay i will keep it public then create repo now i just need to do this step okay go to your intellij id go to the terminal just type git init then git add all then i'll just commit this all the changes have your name with a message first commit or something like that fine then if i will check the gate status nothing is there now i need to set the origin okay so i can define this gate branch because by default the branch is main so i can define this next i can set the remote origin go to intellij add it then i just need to push my changes go to the again fine if you will repress your url you can see all the folders which we created added here also i will just add this redmi file now next if you go to the ppt we just cover the first step right we committed our code to the github now my machine code is sync with my github repo now the next step i need to integrate jenkins with my github so that i can perform this cicd step now first i need to start my jenkins so i'll go to the terminal then i can fire the command preview service start jenkins latest if you are using windows then you can directly run the batch file of jenkin now successfully started it if i will go to the localhost 8080 i can see the jenkins so it will take few seconds to load the page yeah now i need to login it java techkey then give my password just sign in now next since we want to sync our github with jenkins we need to create a pipeline or we need to create a new item okay so i will give something like devops automation i will give the name of my jenkins pipeline as devops automation then i'll just click on pipeline i'll create okay now here you can give some message this is sample jenkins docker integration flow okay now the source code version control tool is github for us here i need to give my project git url okay so i'll just copy this then i just need to copy this paste it here fine then build trigger i'll just get the snapshot on these two okay now next here i need to since you can observe your this is pipeline script declarative pipeline script here you need to tell to the jenkins what steps you want to perform as you can see in the presentation while performing the continuous integration and continuous deployment in jenkins you need to define these steps build maven project build docker image then push docker image to docker how these three steps i need to define in the jenkins file so there is two option either you can directly write the jenkins file here if not if you have jenkins file in your project you can directly input from your particular git project i will i will explain you both the way first we will write it manually then we will move it to the code and i will show you how we can load it from the code okay so here as part of continuous integration and deployment i need to tell to the jenkins hey first build my project check out my code from the github then build my project so that is what i just want to define in the jenkins file which is in this particular portal okay so first i'll tell to the jenkins this is what my pipeline okay then as part of pipeline use the agent as any then i'll specify the tools which i will use for build my project which is nothing marvin and you can define the maven which you have in your jenkins i have something like maven then three five zero i'll tell you how where you can configure the maven okay before that let me show you directly okay first let me save this go to the our dashboard or go to the okay go to the manage jenkins then you can find something called global tool configuration there you can find a option called jdk gate gradle and maven here you need to define the path of your maven installation okay either you can give the path or else you can directly install from here since i directly installed in the jenkins i can give that path directly okay so go to the project devops automation go to the configure fine so this is how i tell to the jenkins pipeline please use this tool to perform the build operation fine now next what i need to do i need to define the stages stages is nothing the steps you want to perform build maven project is one stage then create docker image is another stage then push it to the docker row is another stage okay this is how we can define multiple stages so inside stages i can define the stage which is build maven project right build marvin project or just build maven give any name that's up to you okay in this stage i just need to define the steps i just want to tell to the jenkins as part of this pipeline i want to perform multiple stages or multiple states that's why you can see here the root tag is stages inside that you can define n number of states so one of the stage i want to perform build maven as part of this build maven stage i want to perform these steps what steps you want to perform you want to check out code from the github then you want to run maven clean install that's it right so for that what i can do to get the syntax to check out code from github you can directly use the pipeline syntax here you can type something like checkout checkout from version control you can see here right use this then here you just need to give your repo url which is nothing this one just give it since this is public you don't need to give the credential here change the branch to main because main is the default branch now generate the pipeline so this is what the pipeline to check out code from your particular gate of repo you need to specify this in your configuration this pipeline okay so as per the steps i am telling this do the checkout this is what the url and this is what the git scm now once you do the checkout i just want to execute mbn clean install okay just check out the code and just do clean install it's nothing the packaging right so i'm good i need to save it then i'll just build now we'll see whether this particular stage is succeed or not if you will see the console output it's going on you can see here you check out the code now it's doing the clean install you can see the stages here right clean install this is spring boot app so it will run the test and all we don't have any test now but i will show you in my coming tutorial how you will handle in the test failure and all so you can see here declarative tool install and build maven stage is going on and it completed with 30 second you can see here now you can see the green mark here it means this is succeed okay so we have performed the first step that we just tell to the jenkins using jenkins file build my maven project now next we need to tell to the jenkins to build docker image okay so the first step we are done now you will go with the second step now to build the docker image your application should have docker file isn't it so first let's do that then we'll perform the build docker image go to your application then simply just create a file here new file and name it docker file fine add it now inside this docker file you need to define from xposed then give your jar location and command to execute your jar so we have the jar which is generated we didn't generated here so to specify the jar name what i will do i will go to the pom.xml then i will just define final name jarnam i will define dayboop's integration okay i'll just copy this name then just go to the docker file i will just define here i'll just give the name of my jar to verify whether jar is created with this name or not what i'll do i will simply go to this maven i'll go to the maven life cycle okay then i'll run install so that in target folder it will create a jar with this name what do we specify just now so let's verify that yeah so you can see here build is succeed and you can see it generated the target folder if you go and set this this is the jar name which you specify fine so go to the presentation we created the docker file now we need to build docker image fine so first i will commit these changes because we added the docker file right so i'll just directly i'll give some message added docker file i can directly do this comment and push directly push these changes there are two file changes so i'll directly push to my master repo so merge it so changes are pushed now if you will go to the browser and if you will just refresh your repo you could see the docker failure right that's fine now we need to define in the jenkins file to create a docker image read my docker file and then create a docker image so go to the pipeline devops automation this is what the item we created then go to the configure but make sure before you run any docker related stock docker desktop should be up and running in your machine okay so i just started it let me define another stage to build a docker image or to create a docker image okay so the stage is ending here i'll just create another stage i'll give the name of that stage build okay i'll just give the name build docker image fine then as part of that stage i just need to define what steps i want to perform to build docker image so as per of these steps again i want to define a script who will run to create a docker image so i'll just add a tag called script okay so guys this syntax is very simple if you understand the flow okay so as part of the script i just need to define the command to create a docker image so as you know docker build hyphen t okay then give your image name so what i'll do i will just give java techie which is my earliest name and then i'll give the image name i'll just go to this devops integration is my image name okay go to the pipeline just add it give the space dot okay so this is what the simple command right whenever you are doing uh creating the docker image from the command docker build hyphen t we are giving the image name with the version tag but same thing i am doing in a script to create a docker image hopefully my docker desktop is up and running it's up and running now i believe we are good to execute this particular stage so what i'll do i'll just apply this save it now just click on build now you can see only one stage here now once the two is started you can see the another tab will be appear here soon so build maven is going on once this will be done you will find a another tab called build docker image so let's wait what he is doing i will open in a new tab still the build is going on it will take few seconds yeah build is succeed now you can see here it is creating docker image it is executing all the four states from my docker file and finally it is succeed now if you go here and you will check this you can see build docker image is succeed but i will tell you which i faced the error before if you are using mac operating system then i have some link which you can refer initially docker command will not available in your jenkins so if you are using mac then i'll share this link you can follow this medium i don't know personally but this particular blog helps me you just need to modify the file with the docker app uh installation path but if you are using windows and still you will get docker not found then for you the approach is straightforward go to the go to the dashboard then go to the manage jenkins go to the global tool configuration then you can go down in the docker installation add docker name of it then yeah so you can give the path of your docker.exe file directly in the jenkins itself that is what you can do in windows but for mac you need to change the configuration this particular string value will help you so i will share this link so that you guys can refer if you are getting the issue okay so we are good to go now we just created build maven project then we build the docker image now the next step we need to define in jenkins file that i want to push the docker image which i created to docker hub so that is what we need to define as part of our jenkins file right so go to this i'll go back then i will go to devops automation which is my pipeline then go to the configure so here i need to define another stage okay each steps what you want to perform or each requirement is defined as a separate stage now we are performing two stage build maven project build docker email now next what i need to do i want to push docker image to docker hub so i'll just name it push image to hub something like that as as part of this stage again i just need to define the steps which i will perform as part of this stage then again i need to define the script okay now here what i need to do to push any image to docker hub first i need to login to my docker so that command i need to provide here right i can directly use sh docker login then i can give the username i can give the password but how jenkins jenkins will know what is your username and password first we need to tell to the jenkin use this username and password map it as a variable inside jenkins so that i can able to log into the docker using the script okay so to do that i don't know how we can do that so for that i will take the help from the pipeline syntax so there you can sort something with credential you can see here now here you can not this one we came the wrong one with credential this is what we need to open okay as part of this with credential you can add the secret text okay make sure to click on the secret text now here you can give your variable name i'll give something like doc or pwd okay or docker jenkins or docker hub pwd that will be more meaningful docker hub pwd okay now you need to add your credential here so click on add jenkins again drop down use the secret text the begin to add your credential always use the secret text okay just click the text here secret is nothing the password okay i'll give my password of my docker hub then id you can give any id i'll give something like again same i'll give docker hub pwd anything you can give guys okay so again make sure to click on the secret text and as per the secret you need to provide your password of the docker hub and the id you can give any name now click on add in the drop down choose the docker password i already tried it so for me there is three more already added so recently we created this docker password right i will just add it now i believe we are good click on this generate pipeline script you just need to copy this then go to your configure um declarative pipeline script then here i will just add this just remove this now you can fire the command i'll just write sh let me align it properly fine now i just need to define a command to login docker login hyphen username is nothing javateky and iphone password is nothing which i mapped as a variable just now how can i get that i can get it from here this is what the variable name i just want to map it dynamically so you can see here docker login hyphen u is nothing the username the name of my user then password that's it now next step once you've logged in successfully to docker hub then you can fire a command to push it so i can simply fire a command sh docker push then give the image name the image name which you created devops integration this is where the image name right just add it here before that first let me open the docker hub ok so i need to sign in ok now go back to the config i'll first save it so okay i'll just open it again so if you observe we just create build a docker image then we push image to the hub okay and these are the script we followed to create a image and post it to the docker hub so i believe last time the image is created here i can see devops yeah you can see here java take devops integration so i will just remove this because again i want to perform the same right i'll just remove so that again it will create click on save now i'll simply click on build now it will start the third step i'll just open the output it is performing the build maven project you can see here maven clean install once that will be done it will create the docker image as for the stage we define directly monitor from here as well you can see all the pipeline here okay it is now showing build docker image once this third will complete it will add another tab called post the image so we'll see the log yeah it restarted the building docker image again now it is trying to do the docker login you can see here okay you can see the status login succeed now it is trying to push that image to the docker we can see here docker.io javatek is nothing my id of docker have that that is the reason i added this id while creating the image as well okay so wait let let's let it complete yeah you can see the status image pushed but let's wait for a second still it's going on you can see here it added another pipeline called push image to the hub and all the four states are succeed now if you will go to the docker hub if i'll just refresh it you could see a image called java techie devos integration yeah you can see here right so we are able to create the docker image and push the docker image directly directly to the docker hub using jenkins file now if you'll go and if you will configure this this is where the jenkins file we created here right now this is what manually we created in our jenkins portal itself if i don't want to make it global i want to keep it inside my code we can do it that is the second step i will just copy this i will go to my project i will create another file new file i'll just name it jenkins okay jenkins file add it then i'll add these steps these are the steps we created there right so same steps rather than keeping it in jenkins portal you can directly add in your code so that you can customize whenever you need for a specific project now i will just commit these changes i can directly commit from the intellij so i'll just add our jenkins file just comment and post so changes are pushed to the branch i'll just refresh this i can see the jenkins file as i mentioned from the begin there is two option you can directly write the pipeline script here itself or else you can load it from your project so first we already done and we get the succeed result with all the four steps now we'll try the second approach pipeline script from scm okay so i am not going to modify this i will create another pipeline so click on new item dave oops integration one okay now click on pipeline click on ok i'll just copy the project name url of the project go here you can give any message i am not going to give it now give the project url then build github hook trigger and guys if you want in each and every comment the build will be auto initiate you can directly define the pool scm okay that already explained my jenkins playlist you can check out that you can define the star e star point to the second minute like it's kind of crown expression okay when you want to for from the um auto build now here rather than using the pipeline script now i will load that jenkins file from my application so i can give the gate i can give the url of my gate i believe let's not give the password now i believe this is the public repo so it should alter it if there will be error i will just configure the credential okay then i will just give branch as a main then we are good jenkins file fine now click on save i'll just build now it will just read your jenkins file from your code and it will just start executing yeah it's starting already you can see here there are three steps tool installed then it will do build maven then we'll execute all the steps one by one we'll also see the console output in a different tab build is going on it's building the docker image it is trying to login all the states or steps what we mentioned in our jenkins file is executing here okay same image name already pushed to the docker hub and we are trying again so that's fine that is not an issue so let's wait it to complete go back integration one yeah you can see here all the three stage is displaying here and the last one is still going on so it seems it sucks it now go to the docker hub i will refresh this i can see this few second ago because this image is override okay i could try with the different name but yeah that is fine we concluded that image is getting pushed to the docker hub so this is what all about declarative jenkins pipeline approach using that that is what the industry follow guys no one is using the plugin and all they usually write the jenkins file in their application and they just define the stages to perform so i hope you are clear about devops end-to-end integration flow begin from the github to jenkins then docker and docker hub all the four steps you are clear i believe okay so in my upcoming tutorial we'll try to push that image to the keyboard that is directly from the jenkins so we'll try to do that in my upcoming session that's all about this particular video guys thanks for watching this video meet you soon with a new concept
Info
Channel: Java Techie
Views: 162,313
Rating: undefined out of 5
Keywords:
Id: PKcGy9oPVXg
Channel Id: undefined
Length: 31min 59sec (1919 seconds)
Published: Sun Jun 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.