Jenkins Pipeline for Java using Maven and Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello will welcome in this video I will show you how you can build a maven Java project using Jenkins pipeline as a code approach we will start slowly with a very simple pipeline but in the end we will move to docker iced pipeline bills so I have a very simple maven Java project I generated it from maven archetype so there is nothing special in my application I just want my pipeline to execute something similar to me even clean installed so it can compile my code run some tests here I have set up my Jenkins pipeline job I can show you its configuration there is nothing special in it as well so I use a regular pipeline job with pipeline from get I use a local git repository and that's actually it so we will use a single branch this is not a multi branch pipeline job this is just a regular pipeline job and we need to create Jenkins file because this pipeline will execute pipeline from the Jenkins file ok let's create the Jenkins file then and we will use the declarative pipeline syntax so let's just start with any agent for stages we need just a single build stage with two steps we can call maven - version and maybe maven clean install this is our build common we want to execute also as a good practice and post always section we will execute clean workspace so our pipeline always starts with a fresh workspace okay we come in to change now we can go to Jenkins pipeline job and we can execute it it builds but there is one thing worth mentioning I run my Jenkins as my local user so Jenkins has an access to all executables that I have in terminal that's why it had no problems with running maven or Java but your Jenkins installation most probably runs from different user and in some cases it may not have an access to maven or or Java comment what to do in this case Jenkins has global tools configurations where you can go and you can define specific installations or versions of specific tools the list of tools is limited but there is maven and here I define maven three six zero version to be automatically installed from Apache and I want to use this specific free free six zero version in my pipeline how to do it you can go back to our Jenkins file and here in tools section we can define maven three six zero now let's come at this change [Music] and see what happens [Music] now the tool-free 6/0 maven is installed so it uses this maven v60 and maven clean insole everything works fine so this is a good starting point but it has some limitations so if you are administrator of your Jenkins you can probably define as many mavens as as you wish but if if you need to ask someone to install specific maven version this might be a little bit problematic for you so let's try solve it differently and for that let's try to use docker agent instead so if you want to use docker agent we need to apply a few changes to our jenkins file so we can remove tools and here we can specify docker agent docker agent means that jenkins will execute docker container and the whole pipeline will be executed inside this docker container ok we can define what image to use and let's say we want to use official maven image from docker hub version 3 6 0 jdk 13 let's say we can also specify a label of a node that this pipeline will be executed on and there is good convention that notes with docker installation are labeled with docker so my master node is labeled with docker as well so we can update this and see if it works [Music] now you can see there is one small difference of course it runs the same even free six zero but now you can see that a lot of dependencies are being downloaded and this is because we are using container and container by default has no access to my local m2 repository so it had to download all these dependencies and if we go to full console of this build you can see how this container is executed so here you can see the current call you can see that this container runs on the same user that the Jenkins is running it creates workspace it mounds a few volumes here and as of course uses this maven a free 6-0 JDK 13 official maven docker image so using this docker container gives us a huge advantage of choosing whatever docker image we want to start our pipeline from but of course downloading those dependencies every time we run our pipeline is not something that we would like to see but this can be solved of course let's go back to our Jenkins file and let's try to mount a folder where we will keep those dependencies for the next pipeline runs docker agent allows us to add additional dr. run arguments so we can mount the folder like temp maven let's say from host to var maven m2 in this folder we will store our dependencies for the the next runs we need also specify an environment variable maven config [Music] and to this is information for me even where to store a local repository and to instruct maven to use var maven assets home folder we need to expose Java tool options environment variable where we will define the user home variable - var maven there is one more thing we need to take care of and we need to create this temp maven folder otherwise if this folder does not exist docker demon would create it and it would be under root permissions so our container would have no permissions to write files inside this folder and let's comment this update and let's see how it works now we can see that dependencies are downloaded in the mean time we can see that the repository is created so all these dependencies that are downloaded at the moment are stored in our temple even a repository folder this is very nice so if I go and if I rerun this job again previously maven cleans all took twenty five seconds and now we can see that these dependencies are not downloaded anymore and it finishes in eight seconds this is really nice improvement switching to maven container is a nice improvement for our pipeline but there are still some limitations and let's say that I want to use SSH client in my pipeline so let's add I don't know just for a second we can add SSH - we just display the version of SSH client and with we if we make this change let's see what happens we will run the pipeline and it fails because there is no SSH client and the official maven fries three six zero JDK 13 so we could try to install this SSH client but it's not so easy as you will see so we can use yum this is Oracle Linux distribution we can say yum install open SSH clients this is the name of that in a specific package just commit change here and we can run it of course installation of open SSH clients fail because our user has no permissions to var Cassio M here okay well let's add sudo let's see we can start the pipeline again and it fails because there is no such common like pseudo in official Navin docker image so as you can see we can't install anything in inside this container so how we can solve it we could switch from docker agent to docker file agent and we could build our own maven docker image based on the official one in our Jenkins environment so let's give it a try and see how it goes we will need to create docker file first and our docker file will be very simple we will build it from the official JDK tin here and what we need to do is we will do yum install open SSH clients but we can also add a user Jenkins this way we will be able to make some small changes in in our jenkins file as well so here is our docker file and for our Jenkins file we will do the following we will change user home into home Jenkins and same here same here we don't need to install anything we should be able to check the version of the SSH and now we need to change docker to docker file we remove image we still use a node with label docker here are our arguments that we will use to start the container so Jenkins will handle creating the docker file and we will start a container from this docker file so let's make another comet and let's run the pipeline when we want to take a look at the full console at the moment [Music] and it failed because we didn't add okay and the dockerfile add this and we need to try it again [Music] now we can see that packages are being installed and now our pipeline starts from the docker image that we created which contains OpenSSH as we expect it uses maven three six zero on Java 14 of course building this docker image took some time but the cool thing is that this docker image is built only one spare docker node right so if you have a single node to run your Locker builds you will build this image only once and now if I run it again you will see that this step docker built finishes in less than a second because docker image exists so it just jumps into running our steps like SSH - - V and my even version and maven clean install so what we have right now is we have Jenkins pipeline but builds maven project from the maven version that is under our control it uses docker container it caches all maven dependencies and we can add actually any tool that we need in our build environment directly in the docker file so if you're missing something else you can just add here and if the docker image changes of course Jenkins will build the docker image again but the next time you run your pipeline it will use the docker image from from cache let's quickly recap what we have learned today you can start your Jenkins pipeline by using maven installed on your Jenkins node and you can control specific versions with the global tools configuration it has its limitations but the might be a good starting point as a next step you can consider using agent docker and existing docker image you can use official maven docker image or maybe you have your own docker image stored in a docker hub or any other repository if you use official maven docker image you may be limited to only those tools that are pre-installed in the docker image alternatively you can use agent docker file where you provide your own docker file and you can build your maven image from any existing docker image this way you can install whatever tool you want of course you just need to remember that this docker image is stored in the local cache so anytime you run your job from the new node this docker image will be created that's all for today I hope you have learned something interesting from this video and a final question goes to you how do you use maven in your Jenkins pipelines please leave me a comment in the comment section below see you in the next video [Music]
Info
Channel: Szymon Stepniak
Views: 29,353
Rating: undefined out of 5
Keywords: jenkins pipeline maven, jenkine pipeline maven build, jenkins pipeline docker, jenkins maven docker, maven docker image, maven docker, maven docker pipeline, jenkins docker, jenkins pipeline as a code, jenkins maven project, jenkins maven java, maven jenkins java, maven jenkins, maven jenkins docker, maven build docker, jenkins maven in docker, jenkins maven java docker, jenkins pipeline tutorial, jenkins pipeline tutorial for beginners, jenkins pipeline java
Id: HaGeSq-SB9E
Channel Id: undefined
Length: 17min 43sec (1063 seconds)
Published: Wed Apr 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.