What Is currentBuild in Jenkins?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is the current build variable in jenkins [Music] when working with pipeline and jenkins you might be familiar with the two variables env and params however you might not be familiar with current build not surprisingly current build allows you to access properties from the currently running build in this video we're going to take a look at how to use current build in your pipelines here's today's starting point i have a jenkins lts controller version 2.303.2 when it was installed it was installed using install suggested plugins we're going to be using a sample repository in this video the link to that sample repository is down in the description now before we get into actually creating jobs let's go take a look at the documentation for global variable reference and underneath this we see env in params again nothing real surprising if you've been working with pipeline for any amount of time but you may not be aware of what current built is and current build can give you the ability to discover information about the currently executing pipeline with properties such as current build result display name etc as we go through our examples today we'll also see the ability to make changes to the currently running job so let's go over and take a look at our sample repository and again the link should be down in the description let's take a look at jenkins file 1. now jenkinsfile1 nothing really surprising here we're going to do an sh echo hello world just to have a step there and then we're going to say echo build number is current build dot number now you might be asking how do i know that number is a value that's available to me on current build well let's go back over to our controller and let's create a job first and then we'll take a look at that so i'm going to say i'm going to call this current build test pipeline click on ok let's go and get this set up and here so let's go to this code copy back over to here get [Applause] there's my url main branch and chickens file dash one now i'm going to go ahead and save this but let's go and take a look at pipeline syntax this will open up here notice that we have here our global variables reference if we click on that that's going to take us into the variables reference and this is built out based on plugins that are installed configurations that are already on the controller so this is specific to my controller as it's currently configured if we scroll down what we're going to see is current build and we can see all the different values that are available for us to use or attributes not necessarily values and in my case i know that i'm using build number well i could have used the environment.build underscore number but i'm going to use currentbuild.number just as an example but we can see that we can get the display name normally just 1 2 3 but sometimes it can be set to a commit identifier the full display name if it's in a folder project names all this other information duration so how long did the duration of the build take or maybe make it human readable lots of different attributes that we can access on our current build variable so let's go back over to our job and let's click on build now let's see what happens here so with build one what we see here is we have echo hello world and build number is one and again not really surprising it is build number one now the build number one was a very contrived example let's go take a look at jenkins file two again not going to be real surprising unless you're living in a upside down or bizarro world what we're saying here is we're going to run our shell step and within that stage if it was successful we are going to change the result of our current build to failure so if it was successful we're going to market failure and then on the global level post we're always going to echo out whatever the current result is so you'll see here that i'm doing currentbuild.result to change the value of result or to set it and then to access the value we are going to call the dot current result on current build let's go back over to our job and change our configuration to pull in jenkinsfile dash 2. do that do a build now and i'm going to go ahead and open up pipeline syntax again here so we can take a look at what we just did there so if we go back down to current build we can see here that result is typically success unstable or failure it could be null during the time it's actually running but current result is going to be will never be null so current result is what is it right now but to be able to mutate that value we need to set it to result okay so let's go back over here and take a look at two okay we see that two failed but it was successful again we're living in an upside down world to where success is failure and failure is success so we can see here in our post stage that we've set failure and we echo out failure now that example of setting success to failure and echoing out failure because it was successful probably not something that you would actually use in real life but it does show you the ability to potentially change the value of your current build based on some other value we made the decision a simple decision if it was successful market failed you could have said i got to this point maybe i do some extra checks and i might want to force a failure at that point because of some other item so we can always force the value of the build result now let's go on to our next example this next example is jenkins file three and what we're going to see here now we can see we're pulling in a shared library so i already have a shared library configured on my controller and the link to that shared library is in the readme or by the time you see this it will be in the readme of the repository again just doing our sh just to have a standard shell step but then i have a step called add sidebar link and what this is going to do is it's going to add a sidebar link to this specific job run so up to this point we had build run one and we had build run two now we're getting to do build run three and what we're going to see on the side is that we will have a url some text and an icon now this is an icon that i defined and this user content folder exists inside of my jenkins home directory which happens to be var lib jenkins on my centos controller so what i did is i created a cloudbees.png file i put it in that folder size it to 24 by 24 because as we'll see in a moment that is the size of the icons on the left nav now you may be asking okay well what is this add sidebar link add sidebar link is taking advantage of a plugin called sidebar and i have installed the plugin sidebar on my controller however this plugin although it does have support for pipeline it doesn't have this specific ability of adding a sidebar link during the run time it's normally defined as an option so it's predefined as the job starts but let's say i don't know exactly when this is going to happen i want to force it at my timing so what i did is i created a custom step again add sidebar link and i'm taking advantage of link action which is part of the class that's inside of the plugin now this is pretty boring we're just passing in the values for url text and icon but here's where it gets specific notice we're saying current build raw build add action and then we're passing in the action object what is raw build if we take a look at our documentation and we'll scroll down here a little bit more what raw build is is it gives us access to the run object that has even more apis available to it however this raw build is only accessible by trusted libraries which is my shared library is a global shared library so by default that means it's trusted or an administrator approved script outside of the sandbox now since i have it inside of a trusted library this is something that i don't have to do any kind of extra approvals for this step has now been exposed so if people want to use that step they can so now let's go back over to our job make the modification and then run it so we'll change two to three and let's click on build now and we'll see that it's running now we got rid of our successes faults and all that's gone we can see three is successful now if i click into three watch the left nav you'll see here that we have a cloudbees website if i go to build 2 there is no cloudbee's website because we added in the add sidebar link custom step from the shared library in build three which happened well we added in jenkins file three which happens to map to build three so let's go back here take a look at three and then we have klobby's website and if i right click on it open in a new tab then that takes me straight to klobbies.com now you might be saying to yourself okay that's interesting that's a static url i could do that with the standard way of adding sidebar links and that's true you could because that's all static but what if you needed to create a sidebar link that deep links you to a very specific place based on runtime values that are created that's what we're going to do here let's go take a look at jenkinsfile 4. so what we have here are a couple of variables with current build so i have current build time in millis so what time in millis means is right up here time since the epoch when the build was scheduled so this is going to be some very long number and then i'm creating an image tag we're going to be creating a container image that we're going to be pushing to docker hub so the image tag i have is darren pope slash alpine plus time and millies i needed to capture time and millies into a variable because i need to reuse this in a couple of other places we pull in our shared library we've set up credentials on our controller in fact let's go ahead and validate that those are still here some managed jenkins managed credentials i have a dh dash creds so this is my id and token for docker hub so all of that set up ready to go let's get back over to here what we're going to do is we're going to basically just build out a docker file live so all we're doing is saying from alpinelatest redirecting that to dockerfile then we're saying dockerfile dasht image tag dot an image tag is up here so it's going to be darin pope slash alpine colon in some long number then we're going to log into docker hub now we're going to do a docker push with that tag we're going to pipe out and grep for sha-256 then we're going to pull and value off of that output and then finally we are going to pull a little bit more data off of that file as well so we're going to be pulling from dollar sign four and then dollar sign one and then finally we do a trim here and that value gets stuffed back into this def sha-256 which is going to be the actual value of the sha-256 you're going to notice here is we're building out a url that will deep link us right into that specific image so we're saying layers i've got my username alpine time and millis which was the value up top images shot 256 and the 256 value that we pulled from here and then finally we have content equals explore so all of that set up and then finally we say add sidebar link url equals url that's the defined line here at 22. image on docker hub and then the icon is star dot gif now you might be saying okay where does star.gif come from that comes from within the images folder within the exploded war file so if you were to go in my case on centos or any other fedora based type operating system that's going to be in var cash jenkins and then images and then you're going to find a 24 by 24 folder and then the image is in there and then finally we do a log out so let's do this let's go back over to our job current build test configure dash four click save and click on build now let's watch what happens here so we're building out four we do our docker build here's our time in millis we do our login we do our parsing of the output from docker push so therefore we get our sha-256 then we do our log out well now let's click on four and look at the left nav we see a star here image on docker hub let's click on this to open in a new tab and what you're going to see here is it deep links us directly into darren pope alpine and then our time in millies all the way in here how did this work well let's click on darren pope alpine we can see here that this was pushed a minute ago and if i click on this that takes us back to this deep link but this url is not something that can be previously constructed because all of this is built at runtime so by being able to take the values at runtime reconstruct the url and then add that sidebar link it will make it simpler for anyone that is actually using the jenkins ui to find out information we can deep link them in this case directly to the layers page on docker hub as we've seen just like with env and params we can use current build to access properties during the job run but as well we can make changes to the job output such as adding a sidebar link 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 you
Info
Channel: CloudBeesTV
Views: 389
Rating: undefined out of 5
Keywords: darin pope, jenkins, jenkins tutorial, jenkins currentbuild, jenkins pipeline tutorial, jenkins pipeline
Id: gcUORgHuna4
Channel Id: undefined
Length: 17min 23sec (1043 seconds)
Published: Thu Oct 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.