DevOps CI/CD Jenkins Pipeline, Ansible & Docker | For DevOps Online training call +919886611117

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody welcome to this lecture in this lecture we'll see ci cd using git jenkins ansible and docker we're going to implement something like this developer will commit his changes to get that triggers jenkins pipeline job it's going to build using maven and it's going to build docker image upload docker image to docker hub ansible playbook deploy docker container onto a development server for implementing this demonstration we have jenkins and a dev server we're gonna execute jenkins pipeline job here which is going to deploy docker container onto dev server let's open our jenkins let's log into it first let's make sure we have all necessary tools installed on jenkins machine let's copy jenkins public ip let's do ssh into that let's check get it is there we do need maven i installed maven from jenkins ui let's go to manage jenkins global tool configuration let's scroll down and check maven installations maven is automatically installed from jenkins ui now we need ansible because from jenkins we gonna execute ansible playbook for deploying containers onto dev server we are using amazon linux machine so it's not directly available under em it's giving some suggestions let's pick up this command execute that so that's going to install ansible for us ansible is successfully installed let's check its version yeah i want this executable location slash usr bin ansible because i'm going to install ansible plugin in jenkins there i need to provide this executable location so back to global tool configuration uh right so we need to have the configuration for ansible i need to install a plugin for that let's again go to manage jenkins manage plugins and install and simply click on available select ansible click install without restart ansible got installed let's go back manage jenkins global tool configuration so we need to yes ansible installations add let's give a name and path to ansible executable directory we should not include command in the path we should only provide the folder where that executable is present so this could be any meaningful name save it so i want to take this project it's java and maven based i need to build a war file first after building war file i'm going to build docker image so in that image i'm taking tomcat as a base image and the wall file which was built in previous step is added to this image under tomcat web apps location so let's go ahead and click new item i want to use pipeline project so general section build triggers and other options so for now to develop this code let's use it in line after completion of our demonstration i'm going to keep this script inside this project i mean git repository you guys can refer from there i want to use declarative pipeline let me say agent any so i want to do git clone let's go to pipeline syntax choose kit provide a repository url the branch and also the credentials for cloning repository from github so click on add jenkins choose username with password username is java hometech the password id description add select that generate pipeline script copy the code put it there so if you want better readability we can organize the code in multi-line i want to add one more step for maven build so new stage added steps maven command to clean existing output and freshly create a new package by compiling a code so let's test this before proceeding further something got wrong maven command not form okay i know how to fix this one so what we have to do is maven is not in the path but it's installed through jenkins it's not in the path what we should do is go to jenkins pipeline syntax declarative directive generator tools choose the tool maven and the name we given so under jenkins configuration generate take this piece of code come back so under agent paste it so here what happens because of this block maven will be added to the path so that it won't complying maven command not found so save this build and this time it should definitely work it's working now nice build got succeeded and yes it's building the wall the next stage is to create docker image let's go to configuration let's copy paste docker build docker build iphone t so i want to build docker image and upload it to docker hub we need to tag docker image accordingly so that i can push my docker image to docker hub [Music] so it must have a prefix of docker hub id my docker hub id is command followed by your image name and colon version so usually it could be zero zero i mean [Music] usually it could be 0.01 whatever versioning convention you guys are following but this version should not be static i need to generate this version dynamically so for that i want to introduce a utility method this function returns back latest commit id in the short form that we're gonna use for tagging our docker image let's scroll down get version so let's find out a git command to pick up latest commit id this one this is a command which is going to written the latest comment in short form so let's take this command and let's use that commit hash id which is in short form for tagging our docker image when i execute this command it returns back a value to standard output i want that output to be captured in our jenkins pipeline variable so pipeline syntax snippet generator sh fine the script i'm sorry yeah so go to advanced see written standard output so select this so that i can store written value in a variable copy fine store it in a variable so written that commit hash so let me call this and let me store this value globally so that i can use it in multiple stages so i want to use environment block let's go back to declarative generator go to environment block fine add name docker tag for value i'm calling the function let's take it so let me try this first of all save and do a build okay this is different story so docker command not phone we need to install docker on our jenkins mission i am on jenkins mission i am installing docker i have installed docker using sudo but i want docker commands to be executed from jenkins user for that i need to execute following commands i'm adding jenkins user to docker group so that jenkins user can directly call doc commands without using sudo so we need to restart jenkins for this command to pick up let's make sure docker auto starts when jenkins server reports let's start docker service it's restarted correct let's refresh it has to prompt us for username and password username and password click sign in place let's make sure docker build command is correct right so we missed dot yeah docker build hyphen t stands for tagging i want to push it to docker hub account so my account id is a prefix application name and this tag i want to dynamically pick right so let's pick up this one and use it here followed by dot which is our docker context save build let's open the console we got one more issue i faced this issue before see but we're able to pick the latest commit id for tagging docker image that's working fine but we are getting issue in some other place i know this issue i faced this issue several times let's fix that one so usually we use dot in the end but in jenkins i think this is a bug let's take dot from there put it after build save it and run the build yes now it's working fine good it's success next step i want to upload my docker image to docker hub let's go to configure go to pipeline let's copy paste so docker push docker hub id slash uh image name followed by the version i can't do this directly so first of all i need to login to my docker hub account for logging into docker hub account the command is docker login iphone u iphone p the password so after this command we have access to docker hub and we can push this image i don't want to keep password as a plain text here i want to store password in jenkins credentials pull from there use it here snipper generator you'll find with credentials with credentials it's going to bind credentials to a variable so let's add i want to keep only my password so let's choose secret text add jenkins secret text my doc hub password fine add docker hub right so the variable name of our choice so give any descriptive variable name followed by generate pipeline script let's pick this code let's put it here let's run docker login command so as per this description it's going to bring our fetch docker hub password and assign that to this variable so here instead of hard coding it let's use expression we're done right so that way we are securely managing passwords we're not exposing them as a plain text here let's save this let's build and make sure it's working right even docker push is success our next step is to write ansible playbook for deploying docker container into dev server let's go and refer ansible documentation for ansible docker container that's ansible module for creating containers let's go to examples name module name name of the container image if you want to run any command in the beginning that command state let me prepare ansible playbook in the background see i'm writing a playbook i want to run that playbook on this group i will configure this group in inventory which i will show later become true because i want to install some packages i need to have suda access so first i'm installing pip because the module we're gonna use docker container module depends on docker pi but python module let's make sure pip is installed using pip we're installing docker pipe i am done writing ansible playbook if something goes wrong i will fix that at run time for creating docker container i am using docker container module name of the container image it should use state i am saying should be started and published port nothing but the port mapping commit commit the file now let's write inventory file let's add file create new file so as per convention i'm calling it dev dot inv so because we used a group name in playbook group name is dev so there let me put the private ip address the ip address of dev server and the username for ssh so password ssh key otherwise i'll be passing through my jenkins pipeline code commit this file we have ansible playbook deploy docker yaml file we have inventory file the next step is to use ansible plugin right so ansible play plugin so ansible tool that's the tool we configured playbook file path in workspace deploy let's copy that and the inventory file path it's also in same location dev dot inv so ssh connection credentials for connecting to dev server i'm using hari pam file let me configure same so um let's add jenkins it is so sh username with private key id dev server description dev server username ect user and private key so for connecting to this i have this private key on my laptop so go to downloads this guy so right click let's open that using some editor and let's copy the content close it so come back select enter directly click add paste it and say add again so we want to use that here we don't have any wall credentials we don't want to use become option there and make sure this is checked otherwise it's going to prompt us in the background and we can't respond to that prompt generate and we also need to pass one extra argument nothing but the variable at command prompt i mean this guy okay let me show that let's open that playbook it's expecting this variable at command line so fine cool so i will complete this logic after getting the syntax so copy that let's go to configure pipeline okay so let me have it here for a while let's copy paste it i mean i want to create one more stage [Music] deploy all right so here i want to pass this guy correct so let me use interpolation under or i'll do one thing let's do this oh fine so i'm using uh interpolation so it value is dynamically picked and passed it as a variable to our playbook let's save let's build and see what happens something went wrong it says unrecognized arguments all right i troubleshoot it in the background and i fixed it here just keep double quotes and don't have any single quotes in between and hyphen e which is a direct ansible command option for passing arguments variable name and its value fine let's go back and run it okay i fixed variable arguments issue but we got into some other problem let me show that yeah it's connecting to dev server it's able to execute things so while starting the container it says error while fetching server api version the problem is docker not installed on dev server let's install it using ansible playbook okay i added this block of code install docker using m package name and the state i need to add one more task for starting doc daemon all right starting docker daemon using service module name of the service the state it should be started and enabled yes which is going to make a docker service to auto start whenever our dev server reboots so okay let's go back and build let's wait and see what happens again it failed to see what happened okay change is not reflecting oh my bad i didn't commit that commit trigger and weight this time it worked okay so to test it let's go to a public ip of dev server 8080 slash we have given some image name i mean go to docker file see inside web apps this is our war file name sorry it's not image name war file name yes we are getting response from the application we deployed i want to slightly make changes to the code and show you how to redeploy it so go to the back next.jsp so edit so okay i'm slightly changing my output commit it's done now build again let's wait for this job to complete job completed successfully let's refresh yes we see the changes we made or reflecting that's all from this video guys thanks for watching
Info
Channel: Java Home Cloud
Views: 30,530
Rating: 4.9747233 out of 5
Keywords: java home, devops, java home cloud, devops tutorial, devops by javahome, devops by java home cloud, jenkins ansible integration, jenkins ansible docker integration, ansible demo with jenkins, jenkins ansible docker ci cd demo, docer ci cd, docker jenkins integration, devops demo, devops training, ansible and jenkins training, jenkins credentials, jenkins ansible plugin, jenkins declarative pipeline demo
Id: 13FpCxCClLY
Channel Id: undefined
Length: 34min 48sec (2088 seconds)
Published: Thu Sep 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.