Building Docker Using Cloudbees Jenkins Shared Library and Template

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to my channel lot to learn so today i'm going to show you how to create a shared library and how to use a templating engine in a jenkins so here i will be using cloud b score as my jenkins so we will create a shared library as well as we'll also create a template and i will show you how you can build multiple docker projects using a same template so every time you don't have to go and create your jenkins file so you just create once your template and your shared library and you can build and deploy based on the logic of your shared library and the template you can build and deploy as many as number of you know docker projects so let's see how it's done okay so we have our cloud score jenkins up and running so i'm just going to log in here first okay um so this is my cloud b score operation center so from here i can go to uh multiple master so right now i have one master as you can see here that is cm001 uh so if you have multiple masters you can have it listed here so you have to install your multiple client master and you can add it to this particular operations center so this is how cloud bits work so i'm not going into date uh for on the cloud-based core uh so there is another uh video in my channel uh you can have a look into that so you will understand uh what is cloud score uh so this basically this is uh just a chance distribution provided by cloudpiece so now i'm going to the client master one so here i'm going to first uh i'll just show you how you can import your template as well as you can import your shared library so before i do so i will be showing you uh how you can first create a shared library so let's switch to this code so right now uh okay let me just close all these and okay so i have two repositories in my github so one is a global shared library and another one is uh template so this is the template uh this is the actual implementation where i have my jenkins file so i'll come to this uh later so before that i'm showing you uh how you can write a global shared library so when you create a global shared library uh repository so you create a folder of ours okay where you actually have your groovy file so here this is the groovy script so this is a pretty simple one so it just have uh three uh functions where from i mean which actually implements your uh steps so as you can see here i have login so to build a docker file um into a container image uh you have to log into your registry so whatever the radio state might be so i am here i'm just here i'm just using my um normal docker hub so i'm just going to log in first then i will build the docker file into a container image and then i'm going to push it to the um the registry okay so like that you can just simply have your own uh steps okay so for this uh demo i have three so now uh the first thing is that you have to create your function so here the function name which i have given is login so here i'm just using with credentials okay so this is a function when you try to use a credential from the jenkins so in my jenkins i have my credentials if you see here in the manage credentials option i have a few credentials here so i'm just using this hub.talker so this is the docker hub credential which i have already added so to use that in my shared library i'm using with credential and then this is a username password type so the class you have to mention the class okay so this is the class of my uh credential so if you see here i go to this one so as you can see uh this is my credential and this is a simple uh username password credential okay so i have my username and password updated here so i'm just using that here and the credential id is obviously hub.talker as you can see here in the managed credentials this is the id so i'm just using the credential id here and from the credential id whatever the username and the variables username and the password is there that will be assigned to these variables okay so now once this is done i can use these username and password to log in okay so this is for docker so if you have something else like maybe your nexus so you can have your credential id and you can get your username and password into a variable and so then you can use it in your steps actual states so here i'm just using a shell uh so here i'm just executing the command docker login iphone knife username and the username actually is which i'm getting from here under the iphone iphone password and i'm getting the password from here itself right so this is the first step i'm gonna perform next is uh the build step so here i'm just building uh the docker file so wherever the docker file is there so i'm just simply uh just building to building that particular docker file so here you can see i'm just uh getting a argument tag so whatever the tag is there i'm just trying to build that so here it's docker build hyphen t and then the tag name and then the particular directory so i'm just here just for example i'm using here even you can have another um maybe um another particular um variable which is uh like for example docker file folder like that we can have that as well here and yeah we can have it passed from document maybe uh docker file folder we can have that so uh in this case this one has to be passed from your jenkins file so that it can be passed to this command okay i'm not going to use this at this point of time so i'm just gonna remove this and this one as well and i'm gonna replace this uh from with the dot here okay um now uh the third step is push so here i'm just going to push it to darker push and then that particular tag which i'm going to pass from the jenkins file so this is going to run the command docker push and that particular image tag name so these are the three steps which is present in this groupie file so this is a shared library now this shared library i am pushing i have pushed it to uh one of my repository okay uh you will you can find these repository github repository in the link below now once this is done you once you pushed it to your github then what you do is like you first come to your jenkins and then go to manage jenkins from there you have to go to configure system so once you go to configure system um here you will find if you just search for global so you will find an option called global pipeline libraries okay so this global pipeline libraries is basically the shared libraries part in your setting so what i have done here is like i have given a name here docker i haven't shared a library so this name can be changed so whatever the name you want to give you can give it here and then the default version so version is basically easier uh maybe a branch or riff or maybe a commit whatever uh so you can add that so this is the default version so if you if i do not mention any version in my jenkins file uh what is how to mention a version in the jenkins file i will uh come to that when i am i will explain the template uh so by default it will take the master so i'm just going with a branch here so master and then uh just keep all these things as it is okay then select modern scm and you can select git if you have a different uh source code management uh format for me it was just github so even i can i can use this one but here i'm just using github since this github uh plugin is already installed for this particular generates so i have used this github and i've selected the github credential and the repository https url and the url i have provided here so as you can see this is the url where i have stored my uh shared library so once i put it so if i just just click on apply and save it will import the shared library into the jenkins so this share library can be used by any of our pipeline okay so our shared library is now set and it has been imported into our chain keys now the next step is the template now to create a template again we have to create another uh github repository and you can create a repository and the first thing which you have to do is like you create a folder called templates and another file called catalog to table so catalog.pml has these information so what is a version of this particular template and then type so this is a pipeline template catalog name of the template uh and what will be the display name so these are the details which will be there in the catalog.tml and then under the templates folder you have two files one is template.tml and then check its file so first i will show you the template.tml so in the template.eml i am just providing the name of this particular template so this is container build template so i'm putting that okay next is the description whatever you want you can put it okay and here i am creating a multi-branch template so the template type is a pipeline template and this is a multi-branch template so these are the details which is very important you have to put it now in your template if you want a few parameters to be passed to the jenkins file then you have to mention all your parameters here okay so for example in this parameters i have my kitchen organization so this is just an organization which you can put it of your github then what is the exact repository you are going to use so there that also you can put it here then this is few templates so these templates are variables are created by me so these names can be changed it's not um like you know uh something specific to jenkins so you can put your names here so it's just a hit wildcard uh a wildcard filter which can pin which will include a particular branch this is for excluding specific branches i have created that where i have applied i will show you next okay then github credential which credential has to be used for that particular repository uh github repository which will be implemented using this template okay so again uh this is um a variable which i have created and then docker files okay now this is something which i have created you can skip that if your docker file is in the same position so this docker file is not now i'm not using from this template if you remember in the global shared library i was using this uh tag and then the folder where the docker file is present so if i really have to use that then i can use it from here so if the user will mention the docker file location and you know that we can provide from here we can do that yeah so uh next is uh the build argument so the build argument wherever it is uh any kind of arguments you want to pass it to the builder one so you can pass it but right now i'm not using these these two so it's up to you if you want to use you can use that again this template is also there in my repository on the below link you can go and check out my code there next is the multi branch so here is actual implementation of the uh the pipeline so i want to go for a multi branch and what is the branch source uh it will be github and the repo owner okay your github organization repository is your github repo credentializer is a great upgrades okay i have used that here and the trades okay not under the trades i am just reading head wildcard filter so these are the excludes and includes which i have uh you know used here so these uh you know head wildcard filters and all these details you will get in the uh you know cloud page template uh documentation you will get that there are a few more um you know settings up there which you can you can get it from there then this is some strategy okay default branch property strategy uh then some property and then uh like it will scan the repository every one minute okay since this is a multi branch if you create another branch with your code that will you know scanned every one minute so this is the template not embl file now comes the jenkins file this is the actual implementation so here the first line if you see i am using a library here so this is the library which i have already created docker shared library right so now here if you see i have not mentioned anything i have just mentioned the library name so by default the version of this library or the library where it is stored in the branch it will be master because the default version which i have mentioned that is the master one if i want to go maybe i have a different branch in my global shared library repository uh maybe a development so in that case what i have to do is like add direct development so like this we can actually uh explicitly uh mention our versions but for now i don't have any development branch i'm just going with master so by default it will take the master so now here comes the actual implementation so if you are aware of jenkins file so you will understand how we can create so i'll just give you a brief on that so here the pipeline starts so agent i have create using a label called container builds and then some options i'm taking here okay so these are the few options okay and then the environment so here i'm going to mention some environmental variables here so it depends on you or whatever you want to take okay so you can just simply add your own environmental variables and then the stages okay the stages starts and there are multiple stages out there so first stage is like clean workspace and check out scm so here i'm just delete dir clean workspace and check out smg these are these two are coming from some plug-in and as well as this is a pre uh written script uh from jenkins which will basically check out your source code okay now this is just a preparation step okay uh kind of and this is now actual preparation step so here um i'm just getting the docker files the docker files which are coming from the uh template here you can see this is a docker file so though i'm not using it here but i'm just showing you how you can add or access your template parameters from your jkins file so this is how you can uh use that okay github repo is also coming from your template okay github ripple so like you can you can basically access that uh from here so this is the preparation step now the actual steps come that is the build part so here i'm just building so i have a condition here that only from master and if docker files is mentioned then only uh the container build stage will work so um this is just a condition and then comes the steps actual steps implementation so here uh the script under the script i am just using dockerbuild.login dockerbuild.build and docker build.push so docker build if you remember this is our global shared libraries uh groovy file name it's docker build so i have to use that and under that whatever the methods we have mentioned we just have to call that method here and if you remember in the build i have uh arguments like string tag and all right so i can mention that here now there is one thing which i would like to show you uh let's do a few changes here so uh now i do not want to pass because i'm not going to you know change this jenkins file every time for each of the project i have to make this jenkins file as much generic as possible okay so if i do not if i have my menu tag getting the tag from here it will it is not going to be generic but then in that case every time we have to change it and even if i change it it will basically not work for all the projects right so uh what we have to do we have to make this other generate so here what we can do in the template let's add um maybe here let's copy this and let's make another parameter that is called uh docker tag okay uh that should be a string and specify the container tag name so for example that should be something like um [Music] arnold 1706 uh maybe something called um smart db latest and this should be a mandatory field okay let's add this here if i add it here i have to use it in jenkins file so let's put that in the preparation part let's make it env docker tag let's make it here and it should be docker tag because we have used docker tag here so docker tag dot to [Music] lower case and here we go we then after that we just have to pass it to this particular function docker tag and here as well env sorry okay so now this will be more generic so whenever i'm going to create any particular job for any specific uh project we can just pass our you know docker file or the docker tag name from the template and we can simply build it right so let's push these changes here um add a docker tag in the template let's add that and let's push it to the repository now that is done okay now for first thing is that once you create your template okay with all these files and folders you first go to your jenkins manage jenkins then uh sorry okay you just have to go to python template catalog add a catalog add your branch okay so uh here i'm just using master so it can be master okay and then how many times it will check for uh what is the polling time to check any changes happening in your template in your github repository select your github select your credential here okay and hit up repository url and that's all you just go and you know save it that will do but for me anyway i have already uh you know imported it so this is the same way i have imported here if you see here i have the container build um multiband pipeline template now if you run the catalog import so it will go and imports as you can see it has imported multiple times here already but right right now it has again imported okay so now uh it has updated that okay um let's see uh there is uh one warning like as i can see here okay so allowed scan interval for 15 minutes okay let's go and change it that will be here itself let's go to my template catalog and this one configure catalog and let's go for 15 minutes here and okay okay the problem is in the template here if you see in the template i have uh one minute here right so let's make it 15 minutes okay um so changed to 15 minutes now let's push it to the repository let's commit our changes fine now let's uh import our catalog again okay let's ignore this right now let's go to our client master now we have created our shared library we have also created our template we have imported both of them so now our pipeline template has been imported as well as our global shared libraries are also imported right so let's go and try to build one docker file okay so i have one repository here so this is just build uh postgres uh 96 9.6 on send to s5 okay i have my docker file already created for this so this is my docker file okay so i'm just going to i'll just try to build that okay now you see i don't know i don't have jenkins file in my repository i simply have my uh source code that's all so what i'm gonna do is like so i'll just simply go to new item and you can see i have my job template here container build it's already created here okay so post chris 12 build okay so now you can see the template.tml file has been now converted into a gui you know form like this okay so github organization so my github organization here is this one so let's take it here and put it to put it in the form then my uh repository name is this one okay so let's go and put it here okay um i want to so here this is the inclusion so if i want to specifically include any particular branch so i can do that so maybe master like that and i can specifically want to uh ignore any particular branch like maybe develop meant maybe only from master i want to build i do not want to build from development branch so if this is the case we can do that if not then we can simply you know skip this even if we do not have this it will definitely going to you know get all the branches okay now i have to select my github credential id so these are coming from the template.tml file so if i select that so as you can see i have selected it and now part of the docker file anyway this is not required but we have to do you know put it here because if you remember in the jenkins file i have a check in the jenkins file the condition if the docker files is true that means if we have mentioned something in the docker files field so let's put it here like that though it is not required at this point of time this implementation is not required and if any build argument is required but i don't want any build argument and let's put this particular um you know on the tag name so our this is my personal docker hub uh account so here i have few public images you can also visit this uh registry you can download your and use viewer for your um images and then i'm just going to post press 96 and then maybe send to us seven so this is the image name which will be pushed to the docker hub here so you can see when once it will be built and pushed uh it will come up here as well in the docker hub okay so let's go and apply and save so as you can see it has created one job okay now just have to scan the repository okay if i scan okay it is success so it has scanned it and okay created a multi branch repository so now if you see here you have your multi branch repository multi branch job created for you so just let's go to master and let's try to build it so if we do so let's try here okay let's wait for a few minutes [Music] okay so as you can see uh the build is success so let's go to docker hub here and let's just refresh so if we refresh in this docker hub so you can see now i have another new image which has been pushed so this is the one so if you see the tags here so you can see this is sent to s7 so that's how this whole uh template thing work so i mean if you see here in my in my actual uh source code repository i don't have any um pipeline implementation i don't have any jenkins file here i didn't i don't even have my uh actual commands uh so everything is coming from the shared library to the jenkins template and the jenkins template can be applied to any uh you know um repository your source code repository based on implementation so obviously if it is a maven or something then obviously that cannot be built with this template you have to create your own template and you also have to add your your global shared libraries as well but here this is for docker so i have created a shared library for the docker build push login and and the template file specific to will you know build a docker container and push it to the docker hub registry so that's how it works so if i have uh maybe 10 or maybe 50 docker projects and i want to build i don't have to write and um jen can spell and push it to every project i can have a i mean generic template created and we can i can push that right so uh if i if i just go and change uh maybe let's go and change the tag here so if i just go and change my tag here to maybe something called sent to us uh uh postgres96 sent to us seven latest and let's save it and push it and now let's try to build it see if i just build it here let's see it has started building again now this time this will be pushed to the registry with a different tag let's see so here you can see stock unlogging it says build and it's pushing to this registry now once this is done we can go to docker hub and see [Music] okay this is success okay so let's go to docker hub here and if we go to the repositories here let's refresh it as you can see i have a new image here that is paul's ps4 postgres96 sent to us save it okay and here the tag is latest so this is how you create your template and the shared library and you can build your docker projects and you can try it for your maven or maybe your node application so whatever the application you create your shared library creator specific templates use that for your specific source code repository and build your project and you can also deploy it so i hope you have understood the whole process and i also hope you have liked my video so please like share and subscribe thank you for watching my video [Music] you
Info
Channel: Lot to Learn
Views: 1,389
Rating: 5 out of 5
Keywords: #cloudbees, #jenkins, #sharedlibrary, #jenkinstemplate, #template, #docker, #cicd, #pipeline, #devops, #containers
Id: 7Bz7qodSTUo
Channel Id: undefined
Length: 35min 55sec (2155 seconds)
Published: Sat Sep 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.