How to Read a Properties File Using Jenkins

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to talk about how to read a properties file using jenkins [Music] whether you're a java programmer or not you're probably familiar with the concept of a properties file and if you're not familiar a properties file is simply a file that contains key value pairs in this video i'm going to show you how to use the read property step from the pipeline utility steps plugin to read a properties file from within a jenkins pipeline here's our starting point for today i have a jenkins lts controller version 2.303.1 and you'll notice i don't have any agents attached and that's because i'm using kubernetes to provide ephemeral agents if you haven't watched the video about how to use kubernetes pods as jenkins agents you'll need to do that in order to understand how to set up kubernetes to provide agents to your controller also we are using a sample repository today the link to that repository is down in the description of this video as i said in the introduction we're going to be using the read properties step from the pipeline utility steps plugin at this point i have actually already installed the plugin but let's go take a look at it just so you're aware of what it looks like so installed pipeline utility steps and at the time of recording it's 2.10.0 and let's go take a look at the documentation for read properties read properties has up to four fields all four of these are actually optional but if you're going to get any kind of value out of it you need to provide at least something so there's four values there's a file so it will read a file from the file system text you can just pass in text values we also have the option for defaults which we'll see in our example in a few moments and we're not going to talk about interpolate in this video now if you think about it i could have done a really simple example for read properties but i want to show you something a little more interesting we're using a repository that was used in another video that was named using tfsec and jenkins to secure your terraform code if you haven't watched that video yet not a big deal but you can go watch it at your convenience so you can understand a little bit more of the basis of what we're building on in this video what we're going to be doing is we're going to be using read properties to set values for our build tools so what do i mean by build tools let me pull up my jeans file from that repository now in this repository the branch we're working in today is the properties dash file branch you can see that down here in the bottom left-hand corner so i have a jenkins file and you'll notice that it looks pretty normal from line 16 down standard declarative pipeline and we'll go through all this in just a moment but let's take a look at lines 1 through 14. i'm defining a map here i have terraform version colon one zero zero a tfsec version of v057.1 and a tf lint version of v032.0 these are the versions of the tools that i want to be able to use by default thus d nothing magical about the letter d but we're using it down here in read properties at line 12. you'll also notice that i've set up an empty map with props if we take a closer look at what's happening here because we're doing everything inside of kubernetes i need to grab the values from a file that we will be naming it doesn't exist yet version.properties and then we're going to be loading those into the props map variable but since we have a default here if we provide a version.properties anything that's defined in version.properties will override whatever the defaults are and our defaults are defined up here now because we're using everything with kubernetes we have no executor on the controller itself i basically need to spin up a quick ephemeral agent to be able to read the version.properties file from my repository that's why we're doing a checkout here first so we're checking out the files into our current ephemeral agent that we've got and then we load those values up into props now that i've defined props here then once we get down into the real pipeline where we're going to be doing the real work then you can see here that we're doing a token replacement right here and you can also see the ones just below here at line 32. line 37 so we're saying from props with the terraform dot version key i'm going to load in whatever the value of that is if i don't have a version.properties the value is going to be 1.0.0 if i'm doing the tfsec one if it's not defined in version.properties it's going to be v057.1 now why is the number v here and not a v here it's just the way that the releases were tagged and then once we get into our job we're doing a handful of different terraform steps i'm just doing a terraform version here just to verify that the version of terraform that i defined is what i expect it to be i do an init i'm doing a terraform validate then i'm doing tflint just to check out okay is my terraform file okay and then i'm doing a tf sec again you can watch the previous tf sec video to understand what that's about then we do a terraform plan and then i'm outputting the plan to tf plan and then i'm reusing that tf plan from the plan output to feed into my terraform apply now you might be thinking okay well what's this terraform file all about what are we doing with terraform here in this case it's nothing more than just sending an output to the screen this is a very simple and contrived terraform file but it's valid so let's see what we get here go back here to jenkinsfile let's go ahead and set up a job to run this jenkins file also i want you to notice one more thing we talked about reading in version properties at line 12. notice over here there is no version properties so we would expect that all of these values that populate down here should be the default values so let's go back over to our controller and let's create a new job let's call this tf pipeline okay let's go grab the repository so what i need to do here is chicken's example terraform copy that let's go to our job change this to get there's our url the branch that we're working in is properties dash file let's go ahead and match this up over here properties dash file so we can see what's going on and we're working with jenkinsfile okay let's go and click on save and then we're going to go ahead and click on build now this job run is going to take a little bit longer because my jenkins controller is located locally on my machine and my kubernetes cluster is halfway across the country in the united states so making these calls will take a little bit longer so i'm going to click on build now then we'll fast forward through it and then we'll take a look at the build output okay now the job is finished let's go ahead and scroll back up top we'll work our way back down through the job log so this is our ephemeral agent that got spun up to read in the repository just to try to find the version.properties file so there's the definition of the pod and it's just a single container pod in this case it does the checkout from get and then it gives us a warning saying version.properties does not exist omitting from properties gathering completely fine because if you remember there is no version properties within the repository at this point completely okay completely to be expected now we start to create our next pod but notice here we have terraform one zero zero we have tf sec ci v0571 and we have tflint v0320 these are the values of our defaults that were up here so these filled in down here when this pod was being created so then the job runs and we can see here just to make sure that everything is working as we expect here's our output for terraform one zero zero if we take a look at tf lint there's our threes our zero thirty two zero and then tfsec was zero fifty seven one again those are all the default values that we have here now what i want to do is go ahead and create that version.properties file so let's create a new file i'm going to call it version dot properties make sure i spell it right and i am going to load in current versions at the time of recording of each of these tools so the versions that we had here in our jenkins file are older versions they're still within versions that i want maybe they were the old versions i was using in my examples but now i'm moving on but i'm not ready to actually change my defaults yet but i'm willing to just override them and make sure everything still works once i know that everything still works i may go in and change the defaults that way i don't have to be concerned with setting up a versions.properties file so let's go ahead and add this file so add versions and let's push that up to get and if we take a look at our repository here i'm going to refresh it now we have our version.properties 16 seconds ago and there are the versions that we're looking for let's go back over to our job no changes to the job all we're doing is going to go in and click on build now again just like last time this is going to take a few minutes so i'll join you again once it's finished running through and now that this one finished let's go ahead and take a look back up top and work our way down again so we're at the very top here's that quick ephemeral agent that we're using to read version.properties so we go past the pod definition you'll notice this time read properties here but it doesn't give us a warning about no version properties found so it found version.properties how do i know that let's take a look at this pod i can see terraform106 i can see tfsec 5811 and i can see tflint321 these versions are the versions that we defined in our version.properties file and just to prove everything is still the same if we get down into the actual job run here what we will see is we will see terraform 107. we will see tflint 0321 and we'll see tfsect 5811. what we've seen in this video is that through the use of properties files we are able to configure things such as the versions of our build tools without needing to go into our jenkins file to make those changes that might not seem like a big deal to you but it does help us mitigate risk why do i say that think about this you have someone new that started on your team and they went in to make a change to the jenkins file and they added in some extra characters or maybe they did some formatting or did something just because they thought they should do it in that case that jenkins file may be broken and could cause downtime for your ci processes by exposing just the configurations that we want changed within this job the only real risk that we have is the person messes up puts in a wrong version yes it blows up but i'm not having to go and search down in my jenkins file of okay what broke it's just oh let's go take a look at the properties file oh we have an extra character here instead of being 107 it was 170d i was like oh no don't need d get rid of d everything's fine we're moving back again if you have any questions or comments you can reach out to us on twitter at cloudbeesdevs if this video was helpful to you give us a thumbs up and if you haven't subscribed to cloudbees tv yet why not take a moment click on that subscribe button and then ring that bell and you'll be notified anytime there's new content available on cloudbees tv thanks for watching and we will see you in the next video
Info
Channel: CloudBeesTV
Views: 766
Rating: undefined out of 5
Keywords: darin pope, jenkins, jenkins tutorial, readproperties, tfsec, terraform, tflint, jenkins pipeline tutorial, jenkins pipeline, continuous integration, how to read properties file, what is properties file, how to create jenkins pipeline, jenkins pipeline as code, pipeline as code, continuous deployment, jenkins declarative pipeline, how to create jenkinsfile
Id: qQS7Idaq_ME
Channel Id: undefined
Length: 14min 25sec (865 seconds)
Published: Tue Oct 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.