How to setup a Jenkins Pipeline using a GitHub Webhook

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to another demo this time on how to create a jenkins pipeline using github web hooks this is really cool and it's probably something that all of you if you don't already know would love to be able to do and it makes a massive difference to your ci cd pipeline so being able to create and use jenkins as part of your csc pipeline is vital in the devops world it's on every resume everywhere and if you know how to do it it's only going to help your career so for this demo what i'm using is a drinking server it's already pre-built ready to go um if you want to follow a previous video i'll tell you how to install it and set it up and there's the video link i'll put that video link in the description i've also got a github account so we're going to be using a little app nice simple one on my website that i cloned i thought from someone else which is just really handy and that's my little test page it's very very good um and a spare server so this is actually running on just a server just for the purposes of this demo but you're going to need a web server for this or just your application because what the steps i'm going to do will apply to lots of lots of different situations so um what i'm going to cover so we're going to do a simple jenkins pipeline we're going to add some sort of pipeline code and add a few stages nice and simple then we're going to sort of build it up slowly but then once we've done that we're going to create the access token in github we're going to set up those credentials in jenkins then we're going to add a web hook to our github project so that when we commit our code it tells jenkins that there's been an update and then we're going to modify the pipeline in jenkins to run the script via like a jenkins file and i'll add that in and we'll see that working and then we're going to hopefully watch the application and the pipeline work in action so as i make a commit in gear that pushes out the pipeline and the workflow in jenkins and jenkins orchestrates that and we see the update at the other end and it should be fine i'm going to put a couple of helpful links in this is just a visualization of what we're going to do so we've got this is you updating your code you commit your code to github github files of a web hook jenkins then sees that pulls down the code and then runs the ssh command and everything else that we planned and we see the update happen in real time okay so let me just move this out the way and we'll crack on so starts with a simple login so once you've got your drinking server just log in [Applause] so i haven't got any jobs here it's all new um so the first thing to do is to create a job so we don't need to do anything with jenkins or any on any server at the moment we're gonna give it a name web app and we're gonna give it click on pipeline and we're gonna click okay that's it they're left with this page so we don't actually have to do very much to create the pipeline so if you come to this setting like basically jenkins getting started again these links will be in the description and you just scroll down i mean it's always good documentation definitely read it we're just going to add in this pipeline here and then we're going to build on this so you cut it straight out and that's it so i've got not doing anything else here not taking any boxes we're going to save this and we're going to build it and then we're going to see what happens so it takes a second and then it pops in and we can see stage one done so you might recognize this um more obviously there are more complicated uh sort of pipelines out there we're gonna set three stages up so in my documentation i've already got some code so and this will actually be in the jenkins file that's gonna be in my github page it doesn't exist at the moment but it will and it will be based on this code that i'm about to create so if we go back into configure and go back down here we're going to replace all of this with literally stage one two and three and that's it save it build now number two will pop in and again it's not doing anything so it's really very quick these are just dummy steps so now we need to build up our steps so i'll go over to my page so i'm going to run some commands now these commands won't be the same for whatever you want to do but for me i want to control a web page so the first thing i'm going to do is make sure i can pull down the code so if i go into configure i'm going to swap out this stuff here with mine so what i'm going to do is where is it yeah there it is right so i'm going to grab the clone address so let me grab everything here copy and paste and i'm just going to replace the stage once a bit so above those curly braces to there and then i'm just going to change this for this so this is my web url so get yours you know you can do the same thing or even use mine doesn't matter you can clone it so what it's going to do is jenkins itself is going to clone it locally and that just proves that you've got connectivity um and that it will actually sort of clone it so that's what i'm going to do so take this line out so it's going to delete the web app directory so i can run this multiple times and the first thing he's going to do is just delete that and then it's going to clone this web app one directory for me right so let's save that now build now so number three should pop in oh we got an error let's see what we've got it's probably a typo on my part okay yeah so there's an issue here this is really good to see because you might make the same mistake and go oh my god what is all this groovy errors so go back to the project go to configure scroll down and you can see here that there's no there's no single quote to close it off let's put that back save it build now which will get number four okay number four's running so you see it's got rid of them because we've changed one of the steps it's actually given us a whole new reading now so we can see that it took a second so if we go into the cart into log we can see that it did a clone so it's cloned into the web output directory so that's good so back to the project let's do configure now the next step i want to update stage two so stage two for me is going to be connecting to my server and i'm then going to do a git pull while on the server into a particular directory so again we copy the same sections so i'm going to replace these three lines with the three i've already prepared so i've changed the name of it to push repo to remote host and then i'm going to echo that i'm going to connect to the host and then i've got a pem key so this is going to do this as a jenkins user so you need to be able to ssh without you know with a key without username and password to a remote server so this is my remote server and then when i get there it's then going to run a sudo command because it needs root credentials or root privilege to be able to pull down into this directory and then it's going to do that so okay so clear this up so what i've done already so inside my var www.html where i'm going to do this git pull i already have my stuff running so if i just don't know this might say oh you can see that this images and style and index.html is is what is here and if we do a git remote minus v we can see that it points to webapp.one.get which is which is this so this is our code it's already in place you can already see it on the website and what we're going to do when we run this command is we're going to ssh with the key as the user to the ip address and then we're going to do a sudo get minus c to the directory and we're just going to basically get pull from that directory that's it and then that will update now because i haven't made any changes it should just work so let's just save it and build now so we should get number five this time we did and again it changes the boxes because we've changed the bundle of titles so we can see here if we look that it's finished successfully if you go to the console output we can see that it did it cloned locally that's cool then we connected to the remote server and we ran up git pull and it's already up to date and then stage three doesn't do anything so let's go back to the project back to configure now what i'm going to do is at the very end i'm just going to stick in a stage where it says that the webs you know it does a check to see if the um website is up now you don't have to have this i'm just trying to build up the steps for you guys so let's go back over here so check the websites up and we're just going to run a simple curl command with some options and that's just going to give us an output okay so let's save that so we've now got three stages let's do build now should get number six again it should refresh this look okay so now all the names match up and we just have a quick look at the console we know it's successful we know that we got an ok back so that's absolutely fine so that's working so without doing anything on github without doing anything on any servers we've got a working pipeline the only things you do need to consider are how jenkins is going to connect to your remote servers so i'm using a key but how you connect is totally up to you this is just my example okay so the next step is go back to my notes so we've done the simple jenkins pipeline we can see that that's working um we now need to create the access token in git so let's go over to github and then go to me into settings and then as you scroll down there's the developer settings here click on developer settings personal access token generate a new token web app click on repo it's totally up to you if you pick any more i'm just i just need that so i'm going to generate that and then i can't paste this so this isn't going to be it for long guys so it's going to get deleted after i finish okay so now that we've um save this we go back to jenkins manage jenkins configure system and we're going to scroll down till we get to git hub server here it is so you might just see it like this but just open that click on github server now it already has the api here you don't need to change anything but here we're going to add the credential and we're going to use secret text and the secret text is that the id is web app it can be anything just web app and then i'll put web app in the one below and add that i'm not going to bother to save it then in credentials tip the bottom down the drop down and do test connection okay that is verified if you don't get that then there's something gone wrong somewhere and you need to go back and check your key make sure you maybe cut paste it correctly so we now have connectivity to gitlab okay that's cool so we're going to save that and so now we've got connectivity and we've set up those credentials in jenkins we're going to create a web hook to our project so back into the personal access token page just back up and back up get back to our thing here and in here the local repo settings go to web hooks and then add a web hook so i wrote this down earlier so you can just literally get the url from here copy paste that stick it there and then we also need to add in uh the github web hook literally just that so pop in here web hook keep that the same don't need to give it a secret just push events active in it add the web hook so this should tell us if it works okay it's got tick on it success so that's good so the next thing in my list get back to that so i've got to cut another screen on the other side so now we're going to modify the pipeline so we go back into jenkins and we go back to our web app and we modify the pipelines this time instead of using this script here we're going to put it into an scm file so grab the information copy it go back to git now you have options on how you put this in i'm just going to create a new file and i'm going to call it jenkinsfile and in here i'm going to just cut and paste everything there that's everything that we know works we've already verified it called jenkins so it has to be called jenkins file i believe so now commit the new file okay so it's now here we can have a look at it and there it is so we in here now we flip over and we do pipeline script from scm and then we're going to change this to git and then we're going to give it a git repository the git repository is that one so just go to code copy the https put that in there we don't need to give any credentials i'm just going to tell it to use the master branch or main branch whichever one you want and it's going to look for the jenkins file in the root directory so you could put it somewhere else if you wanted to and it could go and look for it so i'm going to save this okay so because it's a new pipeline we're going to do build now and that will go off that will go off to github and it will check all that connectivity and then it will run so it looks like it's failed i'm able to find jenkins file ah lowercase f i believe okay let's check let's change that to a lowercase f commit okay so hopefully i've got that right so let's go back to the project and run it again okay looks like it's running so let's go to the console app and see what happens so the clone yeah so it looks like it's finished and it's got the okay right so we've run it for the first time manually go back to the project so now that we've got a web hook in place if we go back here go into the index file edit it so normally this would this is just to test the pipeline but once you've got your pipeline in place you'll do it in in the code itself not here let's add another one of these straight underneath let's do automation and cicd so it's been updated so i'm going to commit it this should kick off a pipeline fingers crossed okay so i did save it and expected it to work but i forgot one thing so i need to make one tick box exercise so if we're going to configure and we go into git hub so scroll down keep all this stuff the same we're going to add this github hook trigger for scm polling and save that so now if there's an update it should go through so if we go back into our web app and that index.html we're going to again just add just add some changes and save that and then go back to github i really hope it pumps in right number nine brilliant so it didn't do anything didn't touch it all i did was commit the code it's now pending it's running i'm just gonna leave that to run it says it's finished let's test if it the web page actually updates this is the original page let's update it there it is so it's there fantastic so one other thing you can do is if you go into settings and look at the web hook for the repo and you click on this one you can see as you scroll down that these are all the previous attempts so you can see all the times it was triggered you can pop in and then you can see the response so you can see it tells you how far it got so that's really good so let's just do one more thing i want to see it work one more time we're going to update the index.html one last time i'm just going to take these out i actually updated it realize they didn't make any difference get rid of them save it pop back into jenkins we should number 10 should pop in in a second there it is popping up here right there okay it's running and it's done fantastic let's go and check and the updates are gone so that is how you set up a jenkins pipeline i hope you managed to stick with me through all of that it's definitely worth knowing how to do you know you'll get asked questions on this in interviews do you know how to use jenkins and if you can demonstrate what we've just done with the jenkins file in git then you're on your way you know take it change it make any updates to your own application have any questions just put them in the chat i'll try and get back to you as quick as i can and yeah subscribe thanks very much have a good day and i'll see you in the next video [Music] cheers
Info
Channel: London IAC
Views: 1,589
Rating: undefined out of 5
Keywords: jenkins pipeline tutorial, jenkins pipeline as code, jenkins pipeline as a code, github, github webhook, github webhooks, github cicd, cicd, jenkins, jenkins tutorial, jenkins pipeline, jenkins pipeline setup, jenkins github integration, jenkins github, jenkins ci cd pipeline, jenkins ci cd pipeline example, jenkins ci cd pipeline github, jenkins ci cd pipeline tutorial, jenkins pipeline tutorial for beginners, jenkins github webhook
Id: 08Wwof68cxU
Channel Id: undefined
Length: 19min 23sec (1163 seconds)
Published: Thu Jun 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.