Learn Jenkins Fast! A Simple Jenkins CI Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you want to learn Jenkins and you'll want to learn Jenkins fast well you've come to the right place hi I'm Cameron McKenzie I'm the editor-in-chief over at the serverside.com and I've put together this Jenkins crash course tutorial to get you up to speed quickly if you give me just 90 minutes or so of your time I'm gonna make you very very dangerous when it comes to this Jenkins CI tool and I guarantee you that you're going to learn more than you might learn in a week-long Jenkins class if you stick around I'm going to teach you how to install Jenkins but we're going to do that quickly because I want to get into the real practical stuff we're going to create build jobs from scratch yeah from scratch so if you're a beginner this course is perfect for you but after we create our first build job we're going to create our first pipeline so if you want to learn about Jenkins pipelines this is the right course for you as well we're then going to go back to Jenkins build job and look at how we can parameterize them we're going to then go back to Jenkins pipe blind and see how we can explore environment variables we'll then start creating CI build where we continuously integrate code we pull from GitHub git lab bitbucket we'll then compile that code with Maven we'll run test with Gradle we'll use static analysis codes to make sure that the code meets quality Gates that we have set for it well even go so far as to pull down some source code for a spring boot developed micro service we're going to take that spring boot microservice compile it package it put it in an executable jar file create a Docker image out of it and even push that Docker image into the cloud get it hosted up there on Docker Hub and really truly create a continuous integration continuous delivery Pipeline with Jenkins and all you got to do is give me 90 minutes maybe up to two hours of your time and we'll cover all of this but first we got to get Jenkins installed and and that's what we're going to do right now so the first thing you got to do if you want to learn Jenkins is install Jenkins and in fact that's the second thing you got to do because you got to install the jdk first because Jenkins is written in Java now I'm going to go over the highlights here I've got a tutorial on the server side and a video on YouTube about installing Jenkins so if you want to uh leisurely pace of how to install it go check those out I just find people that come to my crash courses want to get into the Practical quickly and I feel the same way but as far as installing Java goes you got to do that first just go to adoptium.net download the Java 17 installer Jenkins doesn't support Java 21 yet and then just double click it and specify an install location specify that you want Java added to the operating systems path have Java home configured and within just a moment you'll have Java installed on your local operating system then go over to Jenkins download the installer for your operating system I'm on Windows a quick quick double click opens up the Jenkins installer it asks me where I want to install Jenkins it asks me how I want to run Jenkins I say that I want to run it just as a local system user which is fine for learning then I'm going to test to make sure that port 8080 is available because Jenkins runs as a web app on Port 8080. if it is I click next it verifies that Java home was found ask if you want to start everything up as a service which of course you do and then that's it Jenkins gets installed now there is a configuration step that happens after that after the installation happens you'll want to go to localhost 8080 give it a bit of time because it'll take a while for everything to be configured but when you go there the first thing it's going to do is it's going to ask you to go and open a file on your file system and type in a special secret key that it planted on your OS to make sure that the first person to log into Jenkins is is someone who has access to the server that Jenkins was installed on so get that key paste it in then after that well it'll ask you to install plugins just install the suggested plugins that's the best way to start that'll add things like I don't know I think it's like like a git plug-in and an ldap plug-in and a pipeline just accept the defaults click next next next next next when that installs it's then going to ask for a user and if this is just a local installation for training just Jenkins is the name Jenkins is the password Jenkins is the full name Jenkins at example.com is fine for the email address then just finish the installation it'll say that you know the instance will be configured and up and running in a moment just click the button to say start using Jenkins and then all of a sudden Jenkins will appear on localhost 8080. and there you go that is the installation of Jenkins and once you got that done well the first thing you should do is create your first Jenkins build job which is exactly what we're going to do next so we've got Jenkins installed we've got Jenkins running on Port 8080. why don't we kick things off by creating a new Jenkins build job now I'm going to create a just a simple hello world Jenkins build job to start off but I'm gonna follow it up with a Jenkins hello world pipeline I see a lot of tutorials where they do nothing but build jobs and then do pipelines when everybody's bored or they do nothing but pipelines and never address what build jobs are both are important so I'd kind of like to do them both at the same time but we're going to start off with a basic build job build jobs are designed as freestyle projects using the GUI tools you click on that new item link you type in a name I'll call it my first Jenkins build job it'll be a freestyle project notice the option below pipeline so pipeline is a scripted pipeline I declarative pipeline a build job would be a freestyle project that uses the GUI tools both are great and I'm going to do the Jenkins build job right now after I've given it a name I'll click OK you notice that the initial configuration just asks you for some standard stuff you know you want to discard builds after they're done maybe your builds are going to take up a lot of resources do you want to throttle them a little bit so some interesting configuration at the start remember Jenkins at its hard as a continuous integration tool continuous integration means getting code that's checked into a repository pulling it out compiling it running tests make sure that it all works so one of the important things you can configure here is a source code management tool now we're not going to get into git quite yet but we will but right now I'm going to leave that blank I'm going to look at the build triggers now as you can imagine if you're using Jenkins to automate a task if you're using Jenkins to do continuous integration the question arises when should these Jenkins build jobs be triggered should they be triggered every day every night every time somebody checks some code into GitHub well all of those options are available notice one of them is GitHub hook trigger that will fire off the Jenkins build job anytime somebody checks some code into a particular Branch maybe the development Branch maybe the master Branch poll SCM Jenkins can query GitHub or git lab or whatever source code management tool you're using then check to see if there's been a new commit and if there has it'll run the build job if not well just leave things alone there's also build periodically this uses a Cron job syntax of minute hour day month day of week so if I did something like this I said star slash three that means this job will run every three minutes I'll just put a star in for each of the other there's got to be a space in between those bug Splats there one two three four five so that's actually going to schedule this job to run every three minutes now what are we going to do in this job oh boy that's a lot of documentation there click little question mark to get that to go away notice that Jenkins gives us a little static here it says every three minutes is a lot to run a build Maybe star three on the hour instead of star three on the minute but I'm like no I like to live dangerously I'll do it every three minutes um and then you'll notice that some information about the build environment so we can set up secret files credential files terminate a build if it's stocked right sometimes you'll might have a hard time connecting to a network resource and your build might might hang for a little while so you can have it terminated what I'm interested in right now is adding some build steps to this Jenkins build job that's going to run every three minutes and notice that one of the options is to execute a Windows batch command there's also if you're running on Linux Ubuntu you would use execute shell obviously there's also options to invoke ant invoke Gradle invoke Maven right those are your your most commonly used build tools and there's plugins that come pre-configured in Jenkins for Gradle ant and Maven and that's why those options are there if there's something that you want to use that's not there just install a plug-in and it'll magically appear but I'm going to do a little Windows batch file magic and I'm just going to Echo you know hello world from my Jenkins build job from my first Jenkins Bill job or something like that and that's about it um I'm gonna click save there and click apply you can click save apply does a save but it keeps you in the Wizard save it doesn't apply but then it takes you back to the landing page the place you previously were in Jenkins here it's take me back to the display page for the job I notice there's a bunch of links here there's not much status because I've never run this job but there is a play button there that says build now and I can't resist a button that says build now so I'm going to click build now you'll see that I got a green check mark in the bottom left hand corner there I'm pretty happy about that I can click on that that'll give me some information about the build specifically I want to see the console output and there you go hello world from my Jenkins build job and that's how easy it is to create your first Jenkins build job and run it and see the output of it and if you go back to the dashboard over here you can see it listed there on the landing page by the way if you ever get lost in Jenkins you know you get deep into some links I always say just click on the Jenkins head the Jenkins head will just take you back to normal take you back to the landing page and you can see all of your different Jenkins build jobs you can see there haven't been any problems lately so the sun is shining on it and uh it looks like the last time it ran was 44 seconds ago and everything was good and there you go that's how easy it is to create your very first Jenkins build job stick around because what's next building your very first Jenkins pipeline so we just finished building our first Jenkins build job using the GUI tool nice little hello world application now I want to go in and do the exact same thing but do it differently I want to create a Jenkins pipeline so I'm going to create new item click Pipeline and call it my first Jenkins pipeline now what are we going to do in this pipeline what we're going to do is what we're going to print out hello world just like we did in the build job so you can see the difference right away the other thing I'd like to do I like to play with some environment variables I'll show you some of the environment variables that you can access when you're working with Jenkins and show you some of the environment variables that are available now if you want to create a pipeline job you give it a name you click the Super Mario's pipe on there you click OK and well the page that you're taking to looks very similar to the build job page right because a lot of the things are going to be the same basic configurations like discarding old builds is there build triggers right you know when you're creating a Jenkins pipeline what triggers it is it triggered every hour is it triggered by a commit to git should we pull our source code management tool poll SCM and see if a new commit has happened to it so we saw all of those options over when we created a regular build job but here's where it gets different notice there's no option to add a build step or add a post build action instead it's says you want to do something you better start writing some code or at least writing some groovy Jenkins DSL stuff that describes what your continuous integration build should do now how do you get started here well you get started by clicking on the samples there there's a hello world sample a GitHub and Maven sample a scripted pipeline sample let's just start with the hello world sample okay and you can see it starts off it uses the word pipeline there's a something called a declarative pipeline there's something called a scripted pipeline we're trying to stick to declarative pipelines today where you don't put a lot of code into your pipelines in the past you could put a bunch of groovy or kotlin code into your pipelines and that was very effective but they would become unmanageable over time now we like to declarative pipelines start with the word Pipeline and if we do need some complex code we use a shared Library so you can see the agent is any that just means this can run anywhere sometimes you might have a build job that requires I don't know some special GPU so you could say this should only run on machines that have the special GPU on it but this can run anywhere there's stages here we've only got one stage the hello stage because it's welcoming and there's only one step it's printing out hello world from a Jenkins pipeline from my first Jenkins pipeline okay let's start there let's click apply click save click save click apply then uh build the job let's just make sure it works then we're going to come back in we're going to add another stage and we're going to see if we can Echo out some environment variables see what environment variables are available to us so apply that saves but leaves you on the page save that saves but takes you to the the build Pages landing page you can see the option there to build now that's what I'm going to do I'm going to build it right now and as soon as it runs you see a slightly different GUI it shows you the stage that ran hello it I can click on this it gives me an option to look at the logs do I want to yes I do and there's that log file there hello world from a Jenkins pipeline so we know that our Jenkins pipeline rant if you want more details you can always go where that green check box is right over there click on it and then you get the same sort of stuff that you see for a regular Jenkins build job right so it's a different approach to developing your build rather than the GUI tool it's giving you the ability to put your to describe your build job your continuous integration continuous delivery cycle in code the GUI tools are great I love them I recommend them I use them but one of the problems with the GUI tool with the Jenkins build job like the Jenkins like the first Jenkins build job we created is it's very difficult to put that into a source code management tool right if you have a Jenkins build job that is just a piece of code just a a chunk of DSL domain specific language you can check that into source code you can check it into source code along with your Project's code you can version it you can maintain a history if something goes wrong you can see who made the commit that changed it so those are some of the big benefits of working with pipelines right being able to manage it through Version Control maintaining a history of it very compelling in an Enterprise environment now I want to go and edit that Jenkins build job in a completely lost as to where I am inside Jenkins if I said before whenever you're lost always just click on the Jenkins head and it takes you back to the landing page now there's my first Jenkins build job in my first Jenkins pipeline I'm going to click on my first Jenkins pipeline when I come in here I've got the option over on the left to configure that's got the Cog wheel on it I'm going to click configure scroll down to where all of that code was and I'm going to make some changes I'm going to add a stage it's a little copy and paste here make sure you copy all the right braces and now the steps it's not going to be hello I'm going to call it environment and I'm not going to Echo out text I'm going to Echo out some environment variables and I've actually brought up a page in Jenkins it's actually on your localhost and localhost 8080 nv-vars.html show you all the different variables that are available to you and this list grows the more plugins that you add into Jenkins so if you're using the git plugin you can get the branch name whether the branch is primary you can get tag timestamps you for Jenkins build job you get the job display URL the Run URL the build number the job name the build tag the node name the there's a lot of environment variables here and you can use these in your scripts and commonly people do I'm just gonna Echo out the environment and V Dot build ID I'm going to Echo out the EnV you can do Dot and control space and you can see like not just the variables that are available to you but all of these different objects that are available to you through the the whether it's kotlin or groovy programming language right so it's a whole bunch of of classes packages components that are available that you can code with I'm going to have job name right now let's print out the job name and Echo NV e n v NV dot Jenkins home that's an interesting one so when Jenkins runs and Jenkins creates build jobs and when Jenkins pulls things from git or GitHub or gitlab or bitbucket where does it put it puts it all in Jenkins home okay that's fine where is Jenkins home that Echo is gonna tell me now I'm just gonna check to make sure I spell it all of that correctly and I think I did but again we've got a pipeline we've got two stages one prints out hello world one prints out environment variables I'm going to click apply I'm going to click save I'm going to click save I'm going to click apply I'm going to click build now the display is going to change a little bit a new stage has been added environment I'm going to click on the logs for environment this will show me well it shows me where Jenkins home is shows me the name of the build job and it shows me the ID the number of the build that just happened interesting about the Jenkins home I'm going to copy that control copy paste that in there and that copy was not good I have to highlight it all click Ctrl C then I can paste it in and yeah you can see this is all information about all of the different build jobs that have happened I can click on workspace there notice there's a workspace for a variety of different Jenkins build jobs that have been created for plugins for nodes for logs as well if you lock yourself out of Jenkins sometimes you can go in here and find some of the security components and turn security off but yeah that's your your Jenkins home and so there we go that is a look at well not just Jenkins pipelines but also writing some cool code that takes advantage of Jenkins environment variables so you know uh we're getting into some advanced stuff right now one of the things I'd also like to take a look at is maybe parameterizing a build job we can handle environment variables well let's pass in parameters as well I think you're going to find that very cool that's next so we've created our first Jenkins build job and our first Jenkins pipeline now I want to advance our knowledge of Jenkins and create a parameterized Jenkins build job the idea here is we want to print out the version of java maybe when the program starts you want to verify that Java is installed we want to know that we're using the right version so we'll create a Jenkins build job to print out the Java version it's just a simple Java command Java Dash version now in order for that to work you have to have the Java jdks bin directory on the path so just you might want to do this check first and I don't have it on the path so I'm going to have to find out where the Java's bin directory is looks like it's on C tools jdk 17 bin I'm going to add that over here to the path and then everything should be good in the world but as I was mentioning I want to be able to print out the version of the jdk that's installed and it's active but I want to turn that off too like we may hit a situation where you know the job is busy or we're we don't want to know the version um so it's a feature I want to turn on and off so here's how you do this you create a new item and I'm going to call this the parameterized version build job it's a freestyle project not a pipeline but this concept applies 100 to pipelines too you'll see a lot of parameterized pipelines if you start doing a lot of devops work with Jenkins you'll see them with Jenkins build jobs as well I'm going to click OK and then I'm going to find that option that says this project is parameterized click the check box and I'll look at the different parameterizations Jenkins provides you can do a Boolean parameter credentials parameter file parameter password parameter run parameter string parameter and Choice parameter and I'm a big Devo fan Freedom of Choice was the first album I ever bought I'm going to select a choice parameter and what I'm going to call it I'm going to call it should we print it out don't laugh at the way I say out should we print it out so we're gonna have a parameter that says either yes or no to printing out the Java version and the choice are going to be yes and no now by the way you might see that a red exclamation mark do you see the red exclamation mark there on the page okay if you do get your eyes checked because it's a white exclamation mark inside of a red circle that exclamation mark is not red the point is it's saying to me these requires choices sometimes Jenkins if you like that joke say something in the comments sometimes Jenkins will say there's an error when it's been fixed you have to move to another form in the tool so if I tab away Boom the error goes away so just Jenkins throws you off a little bit from time to time but pay attention to that choice parameter should we print it out yes or no I'm not going to create a build step so I'm going to add a build step execute Windows build step and in here I'm going to say if that parameter and I'm actually going to go up and copy that parameter just so that there's no funny business here right because if I do this if I spell it wrong it's uppercase lowercase something like that and I'll mess it up so um I'll say if should we print it out equals yes then what we will do is that Java Dash version command and if percentage should we print it out percentage quote equals equals no then we will just Echo out um okay I'll be quiet with an exclamation mark to indicate shouting okay and and there we go we've now created uh a little build step here that performs some logic based on a parameter that we've supplied to the shop now anytime anybody runs this job they've got to provide that should we print it out parameter either provide yes or no now how do they do that well hold your horses I'm going to show you now sometimes I've seen it where the the first execute batch command doesn't run so we'll see how this goes I'm going to click build with parameters and notice now it actually asks me to provide input on that field yes or no I'm going to say yes and now I click build previously you just hit a button that said build now it wouldn't stop you for parameters now it does so I'm going to click build now keep my fingers crossed that this runs excellently the very first time as I said I have seen sometimes we have to do the second build step but I'll do the console output and there you go open jdk version 17 Java 21 is coming out soon you should pay attention to that but 17 is good as well okay and let's run this again build with parameters and let's say no and click build wait a second click on that check the console output it says okay I'll be quiet and so there you go that's how easy it is to add a parameter to a Jenkins build job now just so you know you know some people are doing this is like I only want to see pipelines this applies to pipelines too so if I looked at my first Jenkins Pipeline and clicked on configure you'd see that there's also that option to parameterize a Jenkins pipeline as well so syntax might be a little bit different because you're going to be accessing the variable inside of a pipeline so it's going to be more groovy based but the concept is the same you can parameterize Jenkins jobs normally or you can and you can parameterize Jenkins pipelines anyways there you go that is how you parameterize a Jenkins pipeline now we're going to do something very uh Jenkins build job we're going to do something very very similar but also very very different in the next exercise which is getting input from the user in a Jenkins pipeline now again it looks like we got input from the user with a parameterized Jenkins build job but this is going to be different so it's going to be exactly the same but it's going to be completely and totally different okay and that's what we're going to take a look at next okay we successfully created our Jenkins parameterized build job that printed out the version of java that's installed what's next is creating a Jenkins pipeline that will print out the version of Maven Gradle and git that's installed and you may be wondering when did I install Maven and git well I did that just a second ago and not only did I do that but I also configured the appropriate environment variables and I even added them to the path so that's all done by the way if you do that and you want to run this particular example if you do install Something and update the environment variables you're going to need to do a restart on Jenkins it only reads those envirables right when it starts up so with that done it's time to actually go in and build our pipeline but not before I talk a little bit about what Jenkins does you see I have you ever looked at the Jenkins mascot the Jenkins mascot is a butler and Jenkins was a fork of Hudson now I get in trouble for calling Jenkins a fork because the people on Jenkins say it wasn't a fork but that's a whole other matter the point is before Jenkins the product was called Hudson and both the name Hudson and Jenkins are the names commonly used for Butlers in TVs and movies and the inside joke in the devops community in the Java Community is that Jenkins does nothing because Butlers do nothing and if you think about it it's true right a butler doesn't do anything despite what you may have seen by watching Joffrey and The Fresh Prince of Bel-Air reruns you know uh the butler doesn't cook the butler organizes the chefs the Butler doesn't do housekeeping the butler organizes the housekeepers the butler doesn't clean the pool the butler organizes the groundskeepers the butler does nothing right all it does is calls on other people to do things for them and Jenkins is the same way so if you want to pull from GitHub Jenkins can't do it it gets good to do it if you want to manage a dependency Jenkins can't do it but it gets Maven to do it if you want to build and test a spring boot project Jenkins can't do it but it can get Gradle to do it and so on and so on and so on so if you want to do anything interesting in Jenkins well you need to install the tools and maybe even some plugins as well because Jenkins can't do it on its own so that's the inside joke and that's why the mascot for Jenkins is a butler because Jenkins doesn't do anything but despite that it's still an awesome and amazing CI CD automation tool I'm not dissing it I'm just letting you all in on the joke now we do have Jenkins we do have Maven Gradle and get installed I'd like to follow the path that we did before with the parameterized Jenkins build job where we asked the user before it ran whether they wanted uh to print out the Java version or not I like to print out the maven Gradle and get version and I'd like to get input from the user but not before I want to get the input as the pipeline runs this is going to be some seriously Advanced Elon Musk stuff right here so pay attention and if you just want to watch as it starts to get Advanced don't feel bad because I'm going to have some really Advanced code on here um it's good enough to see it happening and think hey that's cool I'll also link to the code in the description and also on the article on the server side for it so you can always check that out but we're going to go back into the pipeline World we're going to create a new Pipeline and we're going to call it the um input check version info pipeline it's a bad name but I'm a bad person so there you go and we're gonna head on down here to the script section and I'm going to start off with a little hello world script just like to start off let Jenkins give me the the start there I'm actually going to copy the stage and just create a second stage just because it's it's more interesting when a pipeline has two stages I could do all this in the first stage but I want to I want a couple of stages here so I'm going to leave the hello world alone and add a second stage and say call it the uh you know software versions stage and I'm not going to get any input from the user yet I'm gonna I'm gonna you gotta walk before you can run so first off I'm just gonna print out so I'm going to use a batch command to say mvn dash dash version I'm going to use the batch command to say Gradle dash dash version and I'm going to do a batch command to say get dash dash version by the way I got a bunch of git tutorials online you should check them out if you're interested in git I'm just looking at that does that all look good two dashes with Maven griddle and get one dash with Java I'm going to click apply I'm going to click save I'm going to click save I'm going to click apply and then I'm going to do the build now and this should run it should print out the versions of all these software in the hello it just says hello that's to be expected the software versions well we can see the version of git we can see the version of Maven we can see the version of Gradle that all looks awesome now maybe we don't always want to print that stuff out maybe we want to ask the user as the pipeline runs hey do you want to print this stuff out now the analogy is it could be asking the user hey do you want to deploy into production hey do you want to commit this to the main branch I mean we could be asking the user anything as the continuous integration continuous deployment Jenkins pipeline runs here we'll just be asking them if they want to print out some version data now there is going to be some nasty code in here and it's not that nasty if you're you're used to coding but it's a lot of code to type out I'm going to paste it in just because um I'm not a good coder just seriously that's not my LinkedIn resume and so what I'm going to do here is inside these steps I think maybe I'll just tab that in a little bit and off right off the bat I'm going to add a script element so I've got to say I'm going to put a little bit of script in here you're not supposed to put a lot of script into your Jenkins pipelines it's a bad idea there used to be scripted pipelines and declarative pipelines when you've got a Jenkins pipeline that starts with the word pipeline that's known as a declarative pipeline you're not supposed to put a lot of code into it um if you've got a lot of code that you need to do to address a corner case then you should put that in a shared library and reference it in your code I actually got a video on YouTube about how to create shared libraries so feel free to check that out but occasionally you can put a little bit of script in here and the script that I want to put in here is to get some input from the user I want to find out if we should print out these parameters or not so I'm going to throw a little bit of script in here okay you can see it right there So Def response input message so we're declaring a variable called response we're going to be using the input plug-in the message that we're going to send to the user is should we print the Java version we're going to ask them yes or no we'll also give them a proceed or abort button and then the title of the field will be what to do so that is kind of the the input an input window is going to come up to the users the build runs and we're going to get input from the user now we're going to want to do something when we get that input so we'll do something like um well if the response equals yes then we'll print out the version now one thing to note here is that the syntax of the if statement is different from the if syntax of the if statement from the Jenkins build job that's because the if statement in the Jenkins build job is running as a Windows batch file if you were on Unix or Linux would have to run as obviously a shell script here we're actually working in the jab in The Groovy programming language potentially kotlin is another option you have so we're actually in a different programming environment when we're working with the pipeline but I'm going to click apply there and I'm going to click save and I'm going to just double check my code and I'm going to click build now and now when this Jenkins build job runs notice it's paused under software versions so I now have a pause if I click here it says should we print the Java version yes or no I'm going to click proceed it goes forward I look at the logs and notice we've got all of the versions here now there is a couple of Errors there and one of the errors is just in the syntax that I copied it says do you want to print the Java version so I'm going to go in and configure and it shouldn't be should we print the Java version it should we print the software versions right so we've got that corrected now it makes a little bit more sense but um you know that should work and yeah I think uh I think I'm going to give that a try now so I'm going to go over here I'm going to click apply I'm going to click save now I'm going to build this and now when it comes up it looks a lot more handsome than it says to us should we print the software versions I'm going to click no this time click proceed and now since I've selected no notice that none of the software versions have run so there you go that's how easy it is to configure Jenkins pipeline to take input from the user now one other thing we do have that pipeline that prints out the Java version Okay so we've got the uh input check version info and parameterized version build job so I'm going to go to the parameterized version build job and configure it and what was the name of that input check version info so input check version info I am going to go and configure this parameterized build job that prints out Java but watch this I'm going to add a post build action and the post build action is going to be to build another project and the other project to build is going to be the input check version info so now we'll get the Java version and then it'll actually call on the other build script so I'm actually going to daisy chain these scripts together I'm going to click save and now I'm going to say build with parameters should we proceed yes this prints it out this runs this will print out all the information about Java but now if we go back into the other Jenkins build job the input check version notice that it's just been called by the Java version check and if I click on here now it's asking me should we print out the versions of the other projects and now I click proceed and it's going to print out the version of Maven and Gradle and git for me and so now what we've done is we've used that post build action from the Java version build job we've had that run and then when it finishes it then kicks off a pipeline so we're now starting to create like a daisy chain almost like a pipeline of pipelines right but that's a pretty cool feature another interesting feature of Jenkins and that is the ability to have one Jenkins build job finish and have it call another build job so some super cool things that we can do with Jenkins but I promise you it gets even cooler when we start working with Git and Maven and Gradle and Docker and all of those other tools and that's what we're going to get into next you know I can't believe we've made it this far and we haven't actually built some code yet I've got my GitHub repository up over here and as you can see I've got a project called Spock lizard Docker and every GitHub repository has this GitHub URL that anybody can use to pull the code from that repository well anybody if it's public and if it's private if you've got credentials you can do it but you get the idea I'm going to copy that GitHub URL and I'm going to get Jenkins to pull that code down call on Maven get the code tested get the code compiled get the code packaged and make sure that everything in this project is working swimmingly so we're going to do some continuous integration so how do I do that well I click new item here and I'm going to say build Spock lizard this is going to be a freestyle project we'll do it as a pipeline in just a moment I'll click ok it's going to ask me all of the standard questions I'm going to go right down to source code management and click get it's going to say to me oh you want me to clone a repository what Repository boom I'm just going to type in my URL it's a public repository so I don't need git or GitHub credentials in git that's all I need to do there then it's going to say oh it's going to say the branch specifier we don't really use the master Branch anymore now we use the main branch so this is a bit of older project so I do have a master Branch I'm just looking at that commit from four years ago um but nowadays you would use main so if you do this on one of your own projects check that branch specifier make sure that you have a master Branch you shouldn't you should be using the main verbiage if you are using main you may have to to double check on that so please be careful there so this is going to pull that down I'm not going to do any build triggers I'm going to just build this manually and then what are the build steps well here I could just say at a top level Maven Target and optionally you could invoke a Gradle Target I can use a maven Target and I can type in anything I want here I can say clean compile test package and install you could choose whichever ones you want now I think if I say install that includes all of those so that might be a little excessive but let's say compile and test right so I'll click apply there that means the code is going to be pulled down and now we're going to compile the code and test the code in fact actually I could maybe even add like a second build step for for testing but we can do that later I'll click apply I'll click save I'll click build now and now if I actually take a look at this I can see the logs in real time here if I click over on the project you can see it actually starts going to get it's actually downloading the project from git it's moving it into the Jenkins home director I think under workspaces it's calling on Maven this is the first time maven's being used in my workspace so maven's going to go to Maven Central and pull down a whole bunch of projects and libraries that it's going to need and once maven's pulled all those things down it's going to take a look at the code that Jenkins has pulled down to its local environment and Jenkins is pulling that code down from GitHub it's not doing things in the cloud it's pulling all of that down locally then it's going to sick Maven on it and Maven is going to compile the code and run the test and all of the code compiles in true to form I don't have any tests in my project it says no test to run so I guess the build is successful but I've just now compiled code I've built code I've gone down into the code I've actually run my first build job and if I actually go over into git and maybe take a look at one of the files in SRC main I'll take a look at the Java code here something in mcnc Russian ball take a look at that score class I'll click on the pencil there I will remove a semicolon I will click commit I will commit the change after doing that all of the changes are saved I'm going to come back to Jenkins remember Jenkins is continuous integration we could configure this to run every 10 minutes or pull that repository every hour to check if there's any changes or we could configure it so that GitHub actually triggers the job every once in a while I'm going to click build now and now let's see what happens now we did miss a semicolon there you'll notice that this will run a lot faster this time because it doesn't have to go to Maven Maven will go to Maven Central to pull down what it needs the very first time after that everything's local and it shouldn't be a problem but all of a sudden skies are getting cloudy if I go back to the Jenkins dashboard you'll notice that I got a little Cloud there it's not Sunny in Philadelphia anymore I've got an X and what does this mean does this mean that I did something wrong well not from the Jenkins standpoint you go look at the console output and Jenkins is going to say to you Hey look there was a compilation error there's a semicolon missing at the end of score and I can't argue with that because I took that semicolon off um but there you go we throw that semicolon back on click commit changes go back and run this again go back to the dashboard build Spock lizard build now and now the the build will be successful of course I'm doing this manually but if you had this triggered that's why you configure triggers and your build jobs to go to the repo regularly you wouldn't have to do this manually and the idea is You Know Not only would you have this configured so that it would run at regular intervals but you would also configure Jenkins with your mail server and put the emails of all the people on your team and if the build is broken then email goes out to the team member saying hey all hands on deck the build is broken on the the development Branch or the master Branch or whatever key Branch you're pulling from uh the other thing to mention too is a compile error is as just unforgivable in terms of pushing into a main development Branch you should never have compiled errors more likely than not it's tests that fail right so a failing test will break the build but so will a compile error but there you go that's how you created Jenkins build job and actually run a build job pull from GitHub call on Maven to build and test your code and recognize when the code compiles and recognize when somebody broke the build and that's how how easy it is now it's also pretty easy to do if you're doing this all with a pipeline but before you run your pipeline you might want to configure a tool or two and we're going to take a look at that next okay I want to get into some Jenkins pipeline soon for building with Maven and building with Jenkins um but you know I've been trafficking for a little while on the Goodwill of the path and I shouldn't be and so first I'm going to configure some tools anytime you work in Jenkins you're going to work with a tool like Gradle or sonar Cube or check style or dock or anything like that you install the tool so I've installed Maven and Gradle then you typically install a plug-in okay the Gradle and Maven plugins were already installed by when I did the initial install but if you were to install sonar Cube you need a sonar Cube plug-in and then you do a tool configuration that tells Jenkins about that software that you want to use now as I said I put Maven and git and Gradle on the path and because I did that Jenkins can kind of pick it up automatically but we should be configuring tools so I'm going to click on manage Jenkins here and you'll see tools and for any tools where you've put added a plug-in there's a good chance that you'll have to go in here and configure the tools so you notice it says jdk installations do you have any well I do I should tell Jenkins about it and uh what's the name I'll call it jdk 17. and it's going to ask me where is it and it is C colon slash underscore tools slash jdk-17 so you want to configure that right also if you don't have it installed Jenkins can install it for you which is super cool um forget I've got a default git installation I think I'm going to leave that there path to get executable well no maybe I'll just actually configure it properly so is that the path there yes it is so there we go I will accept the default name having a default name is a smart idea so you can install a bunch of tools uh because you can install different versions of git you can install different versions of java um if you specify something as default you can create a bunch of build jobs that just use default and then when a new version of the tool comes out rather than having to go back to all of your build jobs and say okay we're updating this from jdk 17 to jdk21 if you just configure default here to jdk 21 then all of the the applications that use the default installation would be updated automatically as well so that's pretty cool the Gradle installation I'm going to add a reference to Gradle so I'll call this Gradle eight and do we want to install Gradle no we don't want to install it instead we'll just say where the Gradle home is see underscore tools slash Gradle not Bridle Gradle okay so now we've let the tool know about Gradle eight ant installations and then finally Maven installations now the tool actually assumes a name M3 for a lot of Maven installations uh uh they used to call it M2 for version two now M3 here I'm not going to install it automatically I'm just going to point to c slash underscore tools slash Maven and by the way I mean I'm not making this stuff up that's where all of these tools are Jenkins Gradle git right that's a maven right there and so tool slash Maven and now I've got Maven get Gradle even the jdk all configured here the tool knows about it and all of these tools have been given names um so I can reference jdk-17 at any point in time in the future so from there I'm just going to click apply I'm going to click save and I'm just going to be very happy with the fact that well all of this has been installed and configured properly so we just finished creating our Jenkins build job that pulls from GitHub brings the code down to the local workspace then calls on Maven to do a build which included compiling the code and testing the code I want to do exactly the same thing as that except do it completely differently with a pipeline instead of a build job so how do you do it well I'm going to come over here click new item the previous project was called build Spock lizard so I'll call this build Spock lizard pipeline I'll click the Super Mario Brothers icon for the pipeline right there and click ok I'm also going to go to my GitHub repository and copy that URL to that GitHub repository because I'll need that Jenkins needs to know where to to pull the code from I'll mosey on down to pipeline here and you'll notice that one of the options is GitHub plus Maven and that's going to come in very handy you'll notice that it says it's going to use the tool called M3 for Maven where does that come from well previously I just went in and clicked on manage Jenkins and configured all of my tools and if I came down here and looked at Maven installations you'd see that I named my Maven installation M3 in the tools configuration page of Jenkins so over here the script says Hey whenever we reference mavin it's that M3 tool that we're going to use now it comes down here and it says what are are the steps here well the first step is going to be to build the project using git so we just say git and the name of that git URL now if anybody here knows git you'd know that there's no git and then URL command if you tried to do that on a command line with get git would slap you right in the face that is not a proper git command by the way if you want to learn about git without getting slapped in the face I got some great tutorials on YouTube and over at the server side but what this is is this is part of the domain specific language that uh that Jenkins provides you to help you do things so when you say git and then you provide a git URL Jenkins is actually going to go out it's going to go to that repository I believe it doesn't in it and then a fetch it doesn't actually do a clone and I think it does a clone depth one so that it only gets I think the last commit and it only goes for like a specific Branch maybe the specific Branch doesn't happen here maybe it does but the point is that it's a whole domain specific language that helps you do things more efficiently you don't have to worry about a lot of the Nuance of the application you can see here it says goes and runs Maven as the Unix agent that's not going to help us out so I'm going to get rid of that instead I want to run it as the batch file so that will batch it out then once we're done it's got a couple of post actions we're just going to take the results from any unit junit tests and archive them I don't know if I'm going to have any junit test to Archive here but that's the idea now we've actually got a build that is going to pull from git and it's going to use Maven to compile and package the code um package includes compile and test and it will actually build a jar file for you so I'm going to click apply I'm going to click save I'm going to click build now that should trigger this Jenkins build job the build job runs it's going to take a while so it's going to have to pull down all that code from GitHub for this application we can always take a look at the progress that it's making over here take a look at the console output [Music] a lot of console I put foreign [Music] course here it's giving me that error in terms of the fact that it didn't have tests to record so this build was successful but due to the fact that I don't have any tests in my opification it was barking up just a little bit so I can get rid of that post action right there and send a note to the developers that they need more tests in their applications so I'm going to build that once again now the next time that I do the build it's actually going to run much faster it's going to run much faster because all of that all of those libraries from Maven have already been downloaded thank you and there we go we can now take a look at this build foreign data so it tells us that it went to the master Branch tells us that uh it was the master branch that was built we've got the ability to restart from stage so this is one of the cool things about Jenkins pipelines is that it will store the data that's generated per stage so they're they're actually stateful that we say that these pipelines are stateful so if they fail halfway through they can start up again that's not a big deal here but I've worked on projects where a build process can take a day you know they may have a million tests in their system and it'll take 24 hours for all of those tests to run so the ability to make these pipelines State full is very very important because if a pipeline stops and the data then it's nice to to for you to be able to pick up halfway through and in fact if you actually look at the configuration of a Jenkins pipeline you'll see that one of the options is to uh do a trade-off between speed and durability and that basically means the pipeline won't save as much data so that it'll run faster but if one of the stages fail you may have a problem restarting the pipeline Midway through but if performance is an issue and you think that your environment is pretty stable you can click that and then that'll improve the the speed of your application so kind of a an interesting configuration that you can have there pipeline speed durability override performance optimized less durability maximum durability so some cool configuration options with your Jenkins Pipeline and there you go that's how easy it is to create a Jenkins pipeline that connects to GitHub builds with Maven and generates artifacts for you and in fact if we were to go onto the file system take a look at Jenkins home we'd be able to go in there and actually find all of the different artifacts that are being created you can even then push them out potentially to our application server and do a continuous deployment which we'll take a look at when we actually start getting into working with tools like docker so I mentioned earlier that one of the things that people love about a Jenkins pipeline is the fact that it can be made part of source control right like it's it's configuration and code you can put it into git GitHub bitbucket gitlab when it's in there you can version it you can maintain a history of it something goes wrong you can roll it back to a previous point in time if somebody makes an error on it you can find out who it was that made the commit so having configuration of code is critically important in a modern devops environment however um if we're just creating our pipelines and putting the code right into Jenkins um we're not getting the true benefit of a Jenkins pipeline right because it's still inside Jenkins it's not with the code you should physically put it with the code now I've got another repository here called Rock Paper Scissors the other one was Spock lizard Docker I think and you'll notice that I've got a file called Jenkins file right in the root now I'm gonna I'm demonstrating two things here the first thing that I'm demonstrating is the fact that it's important and it's it's the the standard the best practice to not code your your pipeline inside Jenkins but instead to put it in a Jenkins file on the file system where you check your code in right so it's part of your repository so you can see my Jenkins file right here now the other thing that I'm pointing out here is the fact that well if you took a look at my original pipeline it would start with the word pipeline this one that I have in the repository it actually starts with the word node this is a scripted pipeline they look very very similar they're scripted pipelines and declarative pipelines soon to come there's going to be yaml pipelines as well the the way that we do things today is with a declarative pipeline we like to avoid scripted pipelines because people would put too much code into a script and the benefit of a simple lightweight build file is lost you end up having somebody put crazy kotlin or crazy groovy code into a Jenkins Jenkins pipeline they leave and then no one knows how to maintain the build so instead we'd like to have Jenkins declarative pipelines they start with the word pipeline a slightly different syntax and if you want to use code in them you're allowed to but you're supposed to put that code in a shared library and then reference that shared library in your declarative pipeline it's a best practice it's the way that that build pipelines and Jenkins should be built by the way I've got a tutorial on using um uh building Library Jenkins shared libraries if anybody's interested in taking a look at it um so here we actually have that scripted pipeline starts with node um looks fairly similar but it's not completely different now how do you use this well I'm going to get that GitHub URL of this new repository so I'm going to find that beautiful green button I'm going to click it I'm going to copy it I'm going to come over here I'm going to go back to the dashboard and I'm going to create a new item then I'm going to call it build rock paper scissors pipeline okay there we go it'll be a pipeline I'll click ok now watch this when I come down here it says do you want to write a pipeline script or do you want to find a pipeline script from SCM from a source code management system like GitHub gitlab bitbucket code commit I'm going to say yeah I want to have one from source code management what's the source code management well it's git what's the git URL well it is Cameron mcnz rock paper scissors do you need credentials no what's the branch Master well I think it's Master let me come over here yep it's Master on that one you got to check because sometimes it's main if it's main this isn't going to work and now I can click apply I can click save I can click save I can click apply I can say build now and now as this runs it's not going to be looking for a build pipeline inside Jenkins instead it's going to pull the repo pull all the code and as it pulls all the code for that repo it's also going to read that Jenkins file that's going to go through that Jenkins file and do all the steps evaluate the version take a look at environment variables maybe generate some javadoc compile the code do some acceptance testing and then maybe in the conclusion I don't know looks like there's a little question here what do you think wasn't this cool yes or no I think that was the input message that we took a look at just a little while ago in a previous tutorial I'll say yes it is and proceed and now all of a sudden I've got a full featured multi-stage Jenkins pipeline that is now stored as code in the source code repository of the project that I'm building this is a much much better way of managing your environment take a look at the logs file and there we see Isn't that cool I agree and there you go that is the key to working with a Jenkins file rather than typing out code you just specify that you want to pull the configuration of the build from source code management specify git or whatever other source code tools you've configured provide the git URL for me that was GitHub specify the branch that the Jenkins file is on and that's it you're not writing any code in here you just write it all in Jenkins it makes your life so much easier and there you go that's a real advancement on your knowledge and understanding of Jenkins and the Jenkins file and even a look at scripted pipelines versus declarative pipelines by the way don't be creating any scripted pipelines anymore make sure they're declarative there's what's next I'm going to take a look at some cool plugins there's a lot of reasons why Jenkins got popular but I would say the biggest reason is it's plug-in architecture you see Jenkins is great at doing the basics which is you know pulling code compiling it working with Maven working with Gradle but it also provides this plug-in architecture where other people can create components that can plug into Jenkins and if you want to include those products in your build all you have to do is download those products download those plugins from the marketplace so every vendor who thinks that they've got some great tool for testing for quality assurance for documentation for performance monitoring for load testing all they have to do is create a Jenkins plugin and then say do you look just run this plugin before your build runs or after your build runs are in the middle of your build and they can now provide that feature provide that functionality to you and it's very easy for you to just bring it right into your build so I'd like to actually add some plugins right now I'm going to go into Jenkins and click on the Jenkins head I'm going to click manage Jenkins and what I'm going to do is I'm going to add a plug-in that does static code analysis to your tools it will allow you to uh build your projects compile your code run your tests but after you've built your code and run your tests it's also going to do the static code analysis where it tries to find bugs tries to find programmer mistakes tries to takes a look at the style of your code now the plugin that helps me do this is called warnings Next Generation so I go over to plugins I type in warnings Next Generation and of course you don't see anything and it's because for some reason Jenkins puts you right onto the updates page right when you come in so you start looking for a plug-in you can't find it's like I can't find the plugin it's like no you gotta get head over to and to available plugins and it's called The Star Trek next Generation there it is Star Trek next Generation might be warnings Next Generation but by the way if you come over here like if you just type in git like you could see like the hundreds of different products that they have forget right you know get get lab get lab apis bit bucket stuff um you know you probably for performance I don't know what there's performance plug-ins like there's just there's thousands of plugins right like everybody that wants you to use their tool has a plug-in I can install this warnings Next Generation plug-in now normally when you install a plug-in um this is what you do the first thing you do is you install the software somewhere so if you want to connect to sonar Cube if you want to use Docker you've got to have Docker installed you've got to have sonar Cube installed somewhere so first if there's some software that you want Jenkins to connect to you got to install the software somewhere then you go into Jenkins and you install the plug-in all the vendors will have plugins that allow you to interact with their software from Jenkins so you install the software somewhere maybe sonar Cube you get that running in the cloud or on one of your servers then you install the sonar Cube Plug-In or the dock plugin whatever plug-in you want to use and then you go into Jenkins tools and when you go into Jenkins tools you configure the plugin just like we configured Gradle and we configured the jdk and we configured git and we configured Maven in Jenkins tools you do the same thing with the plugin you configure the plugin in Jenkins so Jenkins knows how to talk to it and from there you can start using plugins inside of Jenkins now I've got Jenkins up and running here the next Generation Star Trek plug-in is installed and what I'd like to do is I'd like to create a new freestyle project but in this freestyle project I'd like to run static code analysis on my rock paper scissors application my rock paper scissors repository you can see it right there I'm going to copy the get URL so that I can create a brand new build job and I'll call it static code analysis if you're wondering why static code analysis because this analysis sticks to your clothes when you pull it out of the dryer it's got a little bit of static cling to it so I'm going to do a static code analysis uh job that analyzes the code in my rock paper scissors application so I create a new freestyle project this is all normal stuff I go to source code management I select get I paste in my URL you're used to all of this stuff and then I go down to the build step and I invoke a top level Maven Target now I'll just do the uh well maybe I'll say I'll use M3 right we configured M3 as one of the maven tools and what's the goal gonna be well the maven goal will be well let's compile and test and then finally package this application package includes compile and test so I'm just going to say package but then I'm going to call in the mapen plugins and I'm going to say Maven call in your programmer mistake detection plugin and call in your find bugs plugin and then call in your check style plugin just to make sure that we're conforming to Czechoslovakian style standards in our code now these are Maven plugins um and Maven is actually going to download these plugins and make them available locally so the the pattern for installing software install the plug-in configure the plugin for the software we're actually able to skip a step because well because Maven is taking care of uh interacting with the plug-in and generating content from it remember I said earlier that Jenkins does nothing right it just delegates to other tools to do things here Maven is actually doing all the heavy lifting in terms of the static code analysis that's going to happen but in the end Jenkins will take credit for it now we haven't done anything with a plug-in yet but watch this I'm going to add a post build step and you'll notice one of the options is to record compiler warnings in static code analysis now if you rewound back into this tutorial and you looked at when we added that post build step to trigger another Jenkins build job when one build job finished that option wouldn't have been there that option only appears after you install the plug-in that's the magic of these plugins they add features all over the place in the tool sometimes it's new project types to build other times it's new menu items other times it's extra things in the drop down menu but here we've got this option and of course I want to Aggregate and use all of this static code analysis and so it says okay well which tools are you using and I see one of them is the tool that will validate czechoslovakian's coding standards so I use check style I say hey I want to use check stop make sure I hit that harder okay check style and it says is that the only one or do you want to add another tool and I said yeah I'm going to add find bugs and it says well are you done yet and I'm saying no I want you to aggregate their results for PMD programmer mistake detection as well and it says oh don't tell me you got any more and I'm like no that's it I'm done now the tool is going to read the plugin is going to read the files generated by PMD and you can do cool things like well if I click on this Advanced button and scroll down you'll notice that I've got the option to set quality Gates so I can set thresholds and say things like I'm going to run a static code analysis on my code I'm going to inspect the quality of the code and if I find one fatal error then it doesn't push to production or if I find 10 warnings then we won't do continuous deployment or if I find more bugs in this build than the previous build we won't push the deployment so I can fail the the bill the build that passes all the tests a build that compiles I can actually fail it if it fails to meet some of these thresholds you can see the healthy thresholds unhealthy thresholds all of those things there Health um add severities so this is kind of cool it can you could actually have a build fail based on these things so that's it I'm going to click apply I'm actually going to scroll up just to take one more look at this so again this was a Jenkins build job nothing configured in the first section the SEC second section I pointed to my GitHub repository that has the rock paper scissors code in it furthermore it's going to the master branch no build triggers no build environments the build step was simply package PMD find bugs check Style and then finally I just said aggregate the check style and the PMD and the find bug results and maybe set some thresholds as well okay I'm going to click apply I'm going to click save and I'm going to click build now but I'm going to watch this build run might take a minute because usually Maven has to download all these plugins the very first time that it runs so it's going to have to reach out and find PMD and find check style and find by by the way check style has nothing to do with Czechoslovakian standards that's just a bad joke but the program's running I'm going to go in take a look it was successful and of course I didn't put any quality thresholds on here so um if I did put quality thresholds on here I'm pretty sure there'd be errors because if I actually look at the log files right now you'll notice that well I got a couple of issues now first of all notice those links on the left hand side we've never seen those before those have never been available for any other build that we've done check style warnings fine bug warnings PMD warnings those are totally new again those are new because of the the PMD plug-in that we installed right you add new plugins and new features pop up all over this Jenkins tool it's pretty amazing a couple of things we've got here it says check style has told me that there's a hundred and four warnings I'm never going to be allowed into Czechoslovakia okay so here we go and what are some of the details so it says that it's seen avoid start Imports um it's got a hidden field problem it's got a left curly brackets in there I don't believe I did a star import there's no way so I'm going to click on that link there and it's going to say Yeah in game.jab on line number four no way no way and then boom there's the bug Splat right there import Java x dot swing dot star dot Asterix dot wildcard and it's even told me using the star form of import should be invoided so right after the build right after the compile this tool is telling me about issues with my code I can go in and find look at find bugs Now find bugs a bit more serious check style will be like oh your brackets are out of place and stuff like that PMD and and find bugs a little bit more serious here it says that um you know you've returned a java.util.date object that just shows you how old this Java code is and it says you should never return an entire object you should return properties of the object but returning an entire object is is not a good design pattern so it's flagged that and I can go into PMD warnings as well and how are we doing on PMD well this has found an unnecessary semicolon really I put an unnecessary semicolon in it says it's right in there in the desktop game I don't believe it I need to see it and boom there it is right after public enum rock paper scissors you don't need a semicolon after that you can just declare your enum so uh We've now seen the ability of this tool to go in and discover bugs discover problems discover issues we could even set up quality Gates quality thresholds with the static code analysis tools and Jenkins and have the build actually fail if there's more than one fatal error there's more than 10 warnings so you can really move your game up as far as uh enforcing quality in your code so really cool features these are just three free plugins there's a wealth of them now one of the interesting things is is this is a Jenkins build job and we did have a Jenkins pipeline earlier that ran the rock paper scissors build so there's build rock paper scissors pipeline now this pipeline it had a number of stages version environment document compile acceptance and conclusion but it did never did Static code analysis so one of the questions that I often get is can I call a Jenkins build job from a pipeline and the answer that question is of course you can so right now I could have a pipeline that runs you can call these static analysis codes from the pipeline just look at the documentation it's possible but you why if I've already got that that job that actually does static code analysis on this so here's what I'm going to do this particular pipeline pulls a Jenkins file from Rock Paper Scissors git so I'm going to go over to rock paper scissors get I'm going to find that Jenkins file I'm going to click edit and I'm going to go in here and I'm going to add a stage so right after the conclusion I'm going to add a stage and I'm going to change it from conclusion to almost done but I'm going to add a stage here and this stage is static code analysis and I'm going to use the build job syntax of the Jenkins DSL right the domain specific language that Jenkins provides for you to do cool things and I'm going to say call the build job called code analysis build job oh hold on a sec that's not what the build job was called let's make sure we call the right one um static code analysis was the name of the project boy that would be a big error will be a dumb error um so you gotta put in the name of the build job properly but there we go that's it so now it's in there I'm going to commit that change and once that change is committed I'm going to Mosey on back to Jenkins I'm going to see the static code analysis it's been about six minutes and 18 seconds since that's Run Okay so it ran a little while ago 108 issues reported that's not good um I was going to say I've never even dated a girl that had that many issues but that would be a sexist joke in it it's kind of a funny joke you get it um I've got issues too let me see okay going back to the pipeline here notice version environment document compile acceptance conclusion I'm going to run this again build now and notice well first of all almost done this actually asks for input from the user proceed and then after the almost done you notice we get a brand new one called Static code analysis now if I hurry up and go to the dashboard you'll notice static code analysis is running so you can see the the the spinning wheel there so that piece of code is actually running right now and that's actually going to to run and the other build job won't be finished until this build job actually completes itself and here's another thing that's interesting this build job has the potential to fail the pipeline so if the pipeline calls on the static analysis code build job and the static code analysis build job fails then the pipeline that calls it it will fail as well so you can call Jenkins build jobs from pipelines and if those Jenkins build jobs fail the kind of Daisy chains through bubbles up through the stack and that job will fail as well but we're not in that situation now we have all of these jobs they've run and you notice now the static code analysis is saying that it ran 19 seconds ago so that's all working properly so there you go that is how Jenkins plug-ins work um it's fairly straightforward find the plug-in you like install the corresponding software configure the plugin it's a step we didn't have to use with this particular plug-in but that's often in there and then when you build your Jenkins build jobs or your Jenkins pipelines just reference the plug-in and all of its features will be available to you it's that easy so now that we've mastered working with Jenkins plugin what is next well you know one thing I see a lot of Jenkins tutorials skip over is credentials and it's pivotally important what I'd like to do here is I'd like to go in create a credential and the reason I want to create that credential is because the payoff pitch for this entire course is US pulling down a spring boot application compiling it testing it packaging it up as an executable jar file creating a Docker image and then doing a deployment and pushing that Docker image right into Docker Hub so that anybody can access it and anybody can run it a full continuous integration continuous deployment pipeline now in order to do that in order to push to Docker Hub I need to provide a username and password so I need credentials here we are click on the Jenkins head to get to the landing page and click on manage Jenkins if you scroll down you'll see a security section and when you hit the security section look for credentials click on credentials and then just dig down through some of the defaults here you'll see system just click on system you'll see global credentials we'll just click on global credentials and then you'll see a lonely page with nothing configured and here we want to configure a credential so I'm going to click add credentials what type of credential is it going to be for us it's going to be a username and password credential but you can see there's a variety of different ones that you can configure the scope will be Global the username that will be Associated here is just my username for logging into Docker Hub my password is I'm not telling you my password you're not going to catch me like that I do need to provide an ID a unique identifier for this credential object so that later on in my pipelines when I reference it well I've got a unique identifier to reference it with and I'll call it Docker hub credentials Docker Hub credentials that looks good I'll click create and now I have a credential object created and what I'd like to do before I start taking a spring Boot microservice and deploying it to Docker Hub with Jenkins is I'd like to actually create a new Jenkins Pipeline and I'm just going to call this the credentials pipeline nothing crazy it's going to be a pipeline type of project and I'd like to just print out the credentials just see if I can access them that's it nothing crazy nothing too complicated in fact I might even be doing a little copy and paste here so I just want to make sure everything works I'm going to start off with a Hello World application I'm going to throw a little script stage in and by the way I'm sorry that the code here is so small I wish it was a little larger and I'm just going to make a reference to the Jenkins domain specific language and one of the functions that's available if you've got the appropriate security plug-ins in there and that's the with credentials function with credentials we'll go in and take a look at a credential that you've created you notice it says Docker Hub credentials there with credentials we'll pull that credential out of Docker and it will allow you to take the username and password variables and assign them variables of your own you know so I'm just assigning them the name username and password and then within this block of code you can use those variables to access the username and password and so I'm just going to do a batch file to Echo out password and username yes you are going to be able to see my password I'll click save here and I will click build now and in the log file I should be able to show you what my password is for Docker hub and so here is Cameron mcnz is the username and of course it masks out the password and tells you hey you're a bad person for trying to print out a password using a batch file um and I am a bad person so I can't argue with that um but that shows you how to access these variables there's definitely more secure ways to do programming than printing out uh passwords in a batch file um but this just gets us started right okay that's the foundation we need what we need to do next is we need to actually build a spring boot application um package that spring boot application an executable jar file and then finally push that up to Docker Hub now I've got a spring boot application here on dot on GitHub it's called Spock lizard Docker as you can see it comes with a Docker file so I can use that to actually build an image build an executable jar file and this looks for uh Spock lizard jar file if spring builds it and it puts it into an open jdk8 Alpine Docker image so that it can be run so this helps me create a Docker image you'll also notice there's a palm file in there as well and this pom file has all sorts of stuff about spring boot in there it is a spring boot micro service will deploy with Jenkins and you can see all the details in there now when we're done I'd like to take the docker image we create for our spring boot micro service and push it to Docker Hub once it's in Docker Hub anybody else can access it download it run it integrate it essentially that's a full deployment of our application as a Docker image into the cloud into Docker Hub where it's publicly available for everybody so we'll actually be simulating a full continuous integration continuous delivery continuous deployment Pipeline with Jenkins using spring Boot and Docker okay so the first thing I'm going to do here with my Spock lizard Docker project is just copy the get URL we're going to need that on the clipboard then I'm going to go over to Jenkins and I'm going to create a brand new item and I'm going to call this spring boot Jenkins deployment it's going to be a Jenkins pipeline so I'll click pipeline here and I'm going to scroll all the way down and I'm going to start off with a GitHub and Maven pipeline because I do have that git Hub URL on my clip portal paste that in there I'm going to get rid of the tools section I don't think I really need that um this is going to get busy so I'll just clarify this a little bit and I think I'm going to start off with a well it's got to be a batch file because we're on Windows here and I'm going to do a maven I'm not too gonna do clean package I'm just going to do a maven or not Maven Maven clean package here we go that looks good to me bat Maven Queen no no I'm not going to do Maven Queen package I'm going to do a maven clean test and compile right and so this isn't going to be the build stage this is going to be the compile and test phase okay so I think that looks good now I'm going to have multiple stages here I'm going to get rid of the post stage so I don't need that okay so that looks good I'm going to click apply at this point in time I don't see any errors and all my braces seem like they line up which makes me happy and I'm going to add another stage here a little copy and paste action to add this second stage and the steps are going to be a little bit different what I'm going to do here is um I don't know what uh what should I do here maybe I'll build the spring boot jar file so build the spring boot jar file with Jenkins and for that that's just going to be a simple Maven batch command so I'll tidy up some of the extra stuff here although it does actually have a little bit of spring booty stuff in it so the command to do this with spring boot is moving clean package spring boot colon re-package and so let me see do I have a brace out of line looks like I do okay there we go and that should make Jenkins happy you got to keep your braces straight okay and what do I do after that well after the spring boot executable jar is created the next sensible thing to do is I need another stage here I'm afraid to copy and paste just because I don't want to mess up my brackets that bracket there that bracket there that bracket there I'm pretty sure I've missed a bracket at some point in time oh no I think I'm good so I'm going to add another stage now and this stage will be creating the docker image now for this it's just another batch command that's going to call Docker build and there's actually a Docker plug-in we just talked about Jenkins plugins there's actually a Docker plug-in that will simplify this so if I install the docker plugin I could just do Docker build and then the name of the docker image I wanted to build alternatively you can just do the docker build command and give your image a name so my image is going to be named RPS for Jenkins tutorial colon latest okay now I have my final stage and I'm going to paste this in because it is plenty long on the final stage is deploy to Docker Hub and as you can see here as I deployed a Docker hub I need that credentials object right so with credentials right there and what I do is I pull those credentials out I assign them the variables username and password I'm not particularly creative in that naming but then I do a Docker login and notice in the docker login I've used that username and password properties so I do that login by doing that I am then able to do the docker push command which allows me to push all of my code up to Docker Hub and so if all of this works and all of this compiles well I should be able to pull compile tasked run and do everything that I need to do in order to push a spring boot microservice out to Docker hub using Jenkins I think that's pretty impressive so I'm going to click save I'm going to click build now and I'm going to cross my fingers hold my breath it looks like it's going compiling test seems to be running it's going to take a while to pull that code down from GitHub through Jenkins but it's working and I know what the error is the error is I don't have Docker running so I'm going to run Docker boy that was a disappointment I thought it was going to run so I'm going to run docker and there we go the docker engine is starting as you can see I'm using Docker desktop for Windows and now that it started I'm going to run this again okay build now let's see if our next attempt will succeed [Music] thank you and yes it says that it succeeded but the proof is in the plum pudding so let's go over here to my Docker Hub account and oh no Docker cam RPS service that's not what we named it over here in the configuration when we said to push that image up we actually gave it the name RPS for Jenkins tutorial latest and look at that it's not up there I'm just joking I didn't refresh there we go we do a little Refresh on Docker hub and boom Cameron mcnz RPS for Jenkins tutorial is right up there and we've now actually deployed our application pulled it from GitHub brought it locally build it packaged it tested it created a Docker image out of it and then taken that Docker image and pass some credentials logged into Docker Hub and pushed it up into the cloud and I would say that is pretty amazing and that is a pretty awesome Jenkins pipeline so anyways I think that's going to conclude this Jenkins course and if you stuck around this long God bless you I do appreciate it um uh I'm gonna I do have a couple of other tutorials on Jenkins that go into a little bit more detail on building pipelines and and interacting with sonar Cube and stuff like that but if you got this far you are very dangerous when it comes to CI CD Jenkins and automating your deployments uh it's really impressive how far we got now before we go a couple of things to mention um I don't know if you know this but I wrote a little book on The Simpsons a little while ago it's called Pickering a Springfield available on Amazon and it's all about how the fabled town of Springfield is actually based on Pickering Ontario a small town outside of Toronto where I grew up as a kid for example the highway that goes through Springfield in The Simpsons is the 401 the highway that goes through Pickering is the 401. Springfield is 30 miles from capital city where they play in a dome stadium uh Pickering is 50 kilometers 30 miles from Toronto where their major league baseball team plays in a dome stadium so check out that book Pickering is Springfield also I wrote a number of Java books so uh hibernate and jpa made easy is available as well so if you're into that type of thing check out my hibernate books my jpa books um also got some on Spring and git coming out too so take a look for those um and then also I've been working with a young freelancer over on the server side and her name starts to declote scrumptuous on Twitter and she wrote this scrum Master certification guide which I can't recommend highly enough if you're into agile into scrum definitely pick this book up it'll get you scrum Master Certified um and will help your career as for me if you want to follow me I'm cameronmcnz on Twitter you can get access to all my personal Antics over there uh I'm also the editor-in-chief of the server side as I've mentioned several times throughout this course um head over there check out some of the tutorials that we've got on Docker devops microservices agile scrum Java python you name it um and uh if you enjoyed this tutorial why don't you subscribe on YouTube
Info
Channel: Cameron McKenzie
Views: 8,460
Rating: undefined out of 5
Keywords: cameronmcnz, theserverside, java, jakarta, jakartaee, git, github, gitlab, dvcs, versioncontrol, tortoisegit, gitgui, atlassian, bitbucket, linux, azure, devops, devopstools, gitops, gitopstools, azuredevops, jenkins, teamcity, githubactions, Spinnaker, cloudbees, codepipeline, aws
Id: OXP8YBPBdgw
Channel Id: undefined
Length: 104min 34sec (6274 seconds)
Published: Mon Sep 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.