Complete Jenkins Pipeline Tutorial | Jenkinsfile explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm gonna show you what jenkins file is how to create it and what the syntax looks like so what is jenkins file if you're familiar with Jenkins usually you would create new jobs or new builds and you would configure them so basically what Jenkins file is that instead of you creating and configuring jobs on the Jenkins user interface you would write a file or you would script that build and it's configuration in a file so Jenkins file is pipeline as a code it's a scripted pipeline which is part of this whole infrastructure as a code concept and so respectively you would create Jenkins file in your repository with your code so I have this demo project here that I'm gonna use to demonstrate janki's file and configure a build with it the first step is to create jenkins file in your repository so i'm gonna create a new file in my dev branch and call it jenkins file so code is where your jenkins file lives now how do you create a jenkins file how the syntax look like and this is the most basic jenkins file that basically does nothing but this is the basic syntax but here i'm gonna explain you the required attributes for jenkins file so the first thing is that jenkins file can be written as a scripted pipeline or a declarative pipeline so the scripted pipeline was the first syntax of jenkins file so what it is is basically it allows to write the whole configuration of the Jenkins file using groovy script so the only structure that you have there is this here and inside of this is basically groovy scripts and you can write the whole configuration here in a flexible manner as you wish so there is no predefined structure there however for people who didn't know or doesn't know groovy syntax or who hasn't worked with groovy is a little bit difficult to start with that's why as a recent addition they actually edit declarative pipeline syntax from jenkins file which is easier to get started with it has of course it's not as powerful as the scripted pipeline because here you're not limited in any way but it's easier to get started with because you have a predefined structure so this what you see here is basically pre given structure that you have to follow but again you don't have to think about how to script all this so you just fill in the gaps so what we saw here the note definition is basically same as these two lines here so in a declarative pipeline you declare that you are writing a pipeline and agent any basically means that this build is gonna on any available Jenkins agent an agent can be a note it could be executors on that note whatever and this is more relevant when you have a Jenkins cluster so to say with master and slaves where you have Windows nodes and Linux nodes etc but here for the beginning for base we just say agent any so we'll just run with the next available agent and as I said these two are equivalent to this one here and they are required attributes you always have to use them the next required attribute is stages and this is where the whole work actually happens so you have different stages of that pipeline and inside the stages you define stage name and you can define as many stages as you want usually you have something like this so you have a build stage let's get rid of this one actually have a build stage maybe you have a test stage and then you have a deploy stage like this you can have additional ones like for example you might have a check out stage stage here and I don't know cleanup stage afterwards whatever and as you see inside the stage you have the steps and here goes the script that actually executes some comment on the Jenkins server or Jenkins agent so for example if you're building a JavaScript application all the NPM install NPM build etc scripts will go here like this if you are you know running tests or for Java back-end all the text execution scripts will be here and the deployment script will be here so right now let's just write simple echo comments to kind of test this so here we're gonna say a building the application and here we're gonna say testing the application in here we are deploying the application and this is a very Jenkins file and this should work already so let's actually okay so I have the Jenkins file and that just basically echoes some stuff and now let's see how to actually use that in a jenkees pipeline so the second step will be to go to Jenkins to create a new build again and I'm gonna call it my app pipeline and I'm gonna create a multi branch pipeline and here's a configuration where I select the git repository that I want to build so I'm gonna configure that repository credentials if you don't know how credentials work in Jenkins I have a separate video about that what scopes of credentials there are in Jenkins how to create them and use them etc and that would be it so basically this is my configuration and here I can specify which branches I want to build so I'm gonna leave it at all the branches and let's see what happens so here you see that it just basically went through all the branches that I have master Dave I have some test branches there and it scanned all the branches for Jenkins file and only found ranks while in dev branch because that's were created and it built dev branch and let's see that actually see that dev branch was built by jenkees file and the stages that we defined in the jenkees file are this one here so we said build test and deploy and to each one of them you can see this echo comment that we defined so building the application testing the application etc so the first step here which is declarative checking out a see basically comes from this configuration here so this implicitly defines the first stage of the pipeline which is checking out the code and the second question is why or how is it configured that my pipeline looks for Jenkins file in those projects and that configuration is right here built configuration which is default I didn't set it or I didn't configure it by default it looks for file called Jenkins file and this is the path in the project and I have junkies file in the root directory so I didn't change any of this so this is all you need to do in order to configure a pipeline to build from Jenkins file the rest of the configuration of individual steps of built pipeline will be done in a Jenkins file itself so let's go back to the jenkees file and how can we see what attributes we can use to configure different parts of the build so this is the basic syntax where we have the base construct inside stages where we defined three different stages so another thing you can do with jenkins file is that at the end of it you can define post attribute so what it does is basically executes some kind of logic or some kind of scripts after all the stages are done and inside the post there are different conditions that you can execute your scripts on so for example if I say always in the post condition it means that this script or this logic whatever is in there will be executed always no matter if the field failed or if it succeeded or whatever happened this will be always executed and this could be something like sending an email out to the team about the build condition because you want to know whether it failed or succeeded or whatever happened another condition is success so you will execute a script here that all is only relevant if the build succeeded and you have the same thing with failure so you can define all the different conditions in the post block and there are a couple of more but to generalize in the post block you basically define expressions of either build status or build status change so for example if the last build change and now it's green again you can use that condition as well here so now it's actually switch to the editor so that we can see the highlighting as well and let's continue from here another thing you can do in Jenga swell which is very useful is define expressions or conditionals for each stage so for example you only want to run the tests on development branch build you don't want to run tests for feature branch build or any other builds what you can do here is inside the stage block you can define so-called when expressions so when should this stage execute and the syntax for that is like this and inside you have expression block and here you find basically your boolean expressions and the branch name or the current the active branch name in the build is always available in jenkins file through an environmental variable that jenkins provides it environment variable is called branch name and that is just available or you can also write like this and that is basically just available for you out of the box and you can use it to check what the current branch that's building is so if i want to check that it's a dead branch i can do like this and this stage will only execute or this part of the stage will only execute if the current branch is deaf if not it's just gonna skip and the same way like you would do in if expressions you can also add or or end expressions here so you can say I want to run this stage if it's a deaf build or if it's a master build I'd say you can do it like this or in other applicable use case for when expressions is for example you only want to build your application if their code changes made in the project so for example in the build step I can do something like if it's a dev and code changes equals true and this could be a variable that you define yourself at the beginning let's say outside the pipeline and that basically has logic where gets heat changes or something like this so basically this will be a groovy script that checks whether there has been any changes made in the code and sets the value of this boolean so let's clear all this up again and talk about environmental variables in jenkees file which is very important concept so as I just showed jenkins provides some environments of variables out-of-the-box for you to use them in jenkees file like current branch name but there's some other stuff that you might need for the logic for example you want to know which build number the current built here so you can use it in your versioning or some other stuff so how do you know what variables you have available from Jenkins the location where you can find that which I find pretty practical is in your Jenkins there's a URL in bars dot HTML that basically provides you with the list of all the environments of variables and you can use them in your Jenkins file this is the one that we used and also you have descriptions for each one of those that you want to use but in addition to the ones that Jenkins provides you can define your own environmental variables and this is also a common practice so let's see how to do that in Jenkins file there is an attribute called environment and whatever environmental variables you define here will be available for all the stages that you have in the pipeline so for example if you need a version in many different stages in your pipeline you would define that version here let's actually go and do that so version and let's just say whatever usually you would calculate the version like you would extract it from your code but I'm just gonna write it like this and the way that you can use that environmental variable in your script is like this so let's see building version you and you also see the syntax highlighting that this is interpreted as a variable an important thing to note here is that if I copy that and I put this in single quotes this is what happens and this is a syntax of groovy if you want the variable to be interpreted as a variable in a string you have to enclose it in double quotes something to be aware but otherwise if you have a simple string you can use either another practical example of using environmental variable can be credentials so for example you have a stage that deploys your newly built application to a development server let's say and for that stage you need credentials of the development server to connect to it and to copy the newly built artifact so you would define credentials in Jenkins as I said I have a separate video where I explain hard credentials work in Jenkins different scopes etc so once you have defined credentials in Jenkins you can use that in Jenkins file and one way to do that is in your environment block you would say server credentials and you would extract the credentials from Jenkins and let's see how that works so this is method or function that binds the credentials that are defined in Jenkins to your environmental variable and that is actually separate plugin that you have to install in Jenkins it's called credentials binding plugin which makes it possible to use Jenkins credentials inside the jenkees file and as a parameter it takes the ID or reference of the credentials in Jenkins so let's go back to Jenkins and see our credentials so this is a global scope and if I adhere a credential let's say username and password I don't really just say server user whatever and this is the ID of the reference let's call it server credentials and if I create this I will be able to use that credential inside the Jenkins file using that reference or the ID that I provided and now I can use that environmental variable in stage the same way as you version again I have to enclose it in double quotes and like this and obviously you're not gonna be using that credentials in a string it would be a shell script so the same way you can provide it inside there now if you need that credentials only in one stage it actually makes sense to read it from Jenkins in that stage as well so another way to do that is instead of defining that in environment block and this is the syntax of rappers so to say you can directly define it here so let me delete that instead what you can do is use with credentials wrapper and that takes as a parameter and object this is object syntax in Group E and that object you will be username password and that is basically a function that lets me get password and user name of that credentials individually so I pass it inside the object and as parameters here I can provide credentials ID which is the same as here and the reason why I can use username password is because the type of Prudential that I created so the server user here is of type username and password it's why I can use it here and after the ID of credentials I can get my username and I can store it into a variable name that I want so I'm gonna call it user and variable and I can store it into password so what this will do is take the username and store it into variable I want to call user and same with password okay so now inside this block with credentials I can use user and password variables so if I have a shell script here some scripts I can like I don't know copy the artifact to some place I can use my username and password like this so a user and password so this will be the script some commands some actual shell commands and username and password variables inside the script and as I said you have to have these two plugins in Jenkins let's actually go and see those two plugins so I have credentials plug-in that allows me to create credentials inside Jenkins UI and I have credentials binding plugin that allows me to use those credentials in my Jenkins file through environmental variables let's clean all that up another useful thing that you can use in your file is tools attribute so what tools does is basically provides you with build tools for your projects so if you have a front-end banking application you will have some build tools configured for that it could be maven Gradle or some other tool that you use and obviously when you're building an application you want to have those tools available so for example in build you would run something like maven install or Gradle build whatever and you need to have those tools available in one way to have those tools available is through these tools attribute and there are three tools right now that's jenkins file support that's Gradle maven and JDK if you need yarn or npm for example you have to do it in another way if you want to have a complete overview of how tools work and how you can use any build tool in your jenkins file from your jenkins configuration i have made a separate video on that where i explain the whole thing in detail so you can check it out but here i'm just gonna show you how to let reboot works and the way you can define the tools here is using name that is supported so as I said Gradle and JDK and next to the tool you have to provide the name of the tool and that has to be pre-configured or pre-installed in Jenkins and the way that you can check that is in global tool configuration and maven is I believe in most of the recent versions pre-installed and pre-configured so you have that and that is the name so name of the installation that you have to provide in your Jenkins file using quotes so this definition he will make maven comments available in all the stages in a game if you want to know exactly how to add new tools here and how to use them in Jenkins file then you can go and check out the other video that I made another thing that could also be helpful in pipelines is parameters so maybe you have some external configuration that you want to provide to your build to change some behavior an example could be that you have a bill that deploys your application to a staging server let's say and you want to be able to select which version of the application you want to deploy so here in parameters you will define the selection and the way you can write that is like this so you have the type first of the parameter and inside you provide the name of the parameter that's how it's going to be displayed let's say call it version you can provide default value I'm just going to leave it blank and you can add the description so that other people also know or maybe you later know what that parameter is meant for so it's a version to deploy on prod there are different types of parameters support for example instead of string you can have a choice which let's call it versions so instead of having to enter input a version name you can have predefined choices of different versions that you can actually choose from and here you know to say choices and let's say like this and this will also have its description nicely playing for now and you also have some other types like boolean harem and this could be for example you want to skip a certain stage on some builds and you can do that here so let's say you execute test parameter default value will be true and description let's leave it here so we can delete one of those two so now we've defined the parameters and we can actually see in practice how they're displayed in Jenkins built and now you can use these parameters in any of your stages which is pretty straightforward you remember the when expression that we use so parameters are suitable usage for expressions so for example here we want to define that whenever expression so in the when expression we can use the parameter so we can say that if execute tests parameter is set to true only then we're gonna run tests if not we're gonna skip that so this step will only execute if execute test that's and this is how you can use the parameters if execute test is true so you can write it either like this or you can just leave that equals true out and this will work as well so if harems execute test is set to true this is gonna execute if you set it to false this will be skipped and the version we can use let's say double-quotes deploying version like this and let's actually execute this jenkees file to see how it looks like in Jenkins so I'm gonna copy that and and it's my Jenkins file and the check so poems this is missing here so I have to say poems thought version and again you can write it with care all caps or camelcase it's up to you and I'm gonna commit that and let's execute that on Jenkins so now let's see where I see my parameters so in pipeline view I don't see any configuration but if I go into the French DIF now here instead of built I see button called build with parameters so before if you don't have any parameters set then it will just say built so I'm gonna click that and I'm gonna see both of the parameters listed here so I can choose a version here from the list so let me just say in 1 to 0 and I'm gonna uncheck execute tests and if I build that let's see so the tests should be skipped because we unchecked it and let's see what the deploy stage says deploying version 1 to 0 so that's how parameters work so these parameters that we defined in jenkees file apply to all the branches so you can use that individual in every branch when building it now another very useful and interesting thing is using external scripts in Jenkins so imagine a scenario where you have all these pipeline steps where you build front end and you run tests and you build back-end and you build docker image out of it and you push the repository etc so you have many different stages and then inside of this stages you have a lot of logic and the logic is mainly groovy scripts right and very soon your Jenkins file can get loaded with external scripts or you could have shell scripts or you may have cradle scripts that execute different stuff so often it's a good idea to clean up Jenkins file and put all these scripts or say in its own file and the way that you can use it is create a separate Gradle script and extract all the Gradle scripts into that external file and let's actually do that till now on this examples that I showed you I have used just very simple echo comments but jenkees file basically supports groovy script so you can write much more powerful logic using groovy and the way you can do that is using script blocks so instead of just putting these echo simple commands in the step section you would define script here and here you can write normal cradle so you can define variables and you can define functions and do all other stuff and call cradle functions etc so what we're going to do now is we're going to extract those simple echo comments to a Gradle script just to see how to do that so I have this groovy script here that just defines one function just echo something very simple and return this is important because otherwise you can't import it in thinking's file so how do I import and use that groovy function in jenkees file so I'm gonna go ahead and create an image section or in its stage here in which I'm going to load that groovy script and in order to load that I need a script block because this is gonna be groovy command and I'm gonna say groovy or chibi whatever and load script dot groovy and I want to make this a variable that holds the imported groovy script source a globally available so that I can use it in all my stages I'm gonna define that actually outside here and assign the whole groovy script there so the one that I exported was so this should be a function name so I'm gonna call it build yep and the way I can use that function now in my Jenkins file is using that variable that holds the script and instead of this year I can replace this echo statement but again I need to do that in script because otherwise it's not execute because it's groovy syntax so the variable name and function name and the function name very simple and let me actually export all of those so I'm gonna create a new function I'm gonna call it test app and copy that here and I'm gonna do a third one which is deploy app and again I'm gonna copy here and I need to put them in script this is gonna be test AB and this is gonna be deploy it and one thing you may already noticed here is that the echo command with version parameter it should be parms version actually I put that in deploy app in the groovy script so basically all the environmental variables that you have accessible here through parameters attribute or environment attribute or out-of-the-box available from Jenkins you can use all of them in groovy so you have access to them inside your script so you don't have to configure anything for that so now we have exported each stage functionality to groovy script so whenever for example your logic of building the application changes and you have some complex logic here for example in build application you may want to extract the version from your pom file or from your Gradle file or maybe some other place and you have to build a new version construct depending on how you version your application so all that logic will leave in groovy script and jenkees file will have more slim and simple structure what you can also extract groovy is your expression so basically everything that is Ruby scripts you can extract to the external script so let's actually execute that now and see how it works so I'm gonna edit the Jenkins file and I'm also gonna add that groovy script to my project I called it script out groovy ok so now if I scan my branch see the logs and let's see dev is building and it was successful so one thing that is very practical in pipeline is in the branch specific build where you have the history if I go for example in one of those I have this replay button that is a very practical way of testing out changes in Jenkins file without having to commit that so I can actually make the changes right here so I can add another echo and I can run the build again so now that I've committed that extra script that I have let's go to the last build here and if I do replay I see not only my Jenkins file but also that script so I can actually change or I can adjust both and test the changes right here so if I wanted to say changes and run that I can see that you echo and that's how you configure and use external groovy script in Jenkins file thanks for watching the video I hope it was helpful and if it was don't forget to like it if you want to be notified whenever a new video comes out then subscribe to my channel if you have any questions if something wasn't clear in the video please post them in the comment section below and I will try to answer them so thank you and see you in the next video
Info
Channel: TechWorld with Nana
Views: 735,642
Rating: undefined out of 5
Keywords: jenkins pipeline tutorial, jenkinsfile, jenkinsfile tutorial, jenkinsfile tutorial for beginners, jenkins pipeline, jenkins pipeline as code, jenkinsfile syntax, how to create jenkinsfile, how to create jenkins pipeline, jenkinsfile example, jenkinsfile parameterized build, jenkinsfile post build action, post build actions in jenkins, jenkinsfile parameters, jenkins groovy pipeline tutorial, jenkins, jekins pipeline tutorial for beginners, jenkins tutorial, techworld with nana
Id: 7KCS70sCoK0
Channel Id: undefined
Length: 35min 5sec (2105 seconds)
Published: Sat Mar 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.