What Is an Input in Jenkins?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is an input in jenkins [Music] you may or may not have used the input step in your jenkins pipelines in the past we're going to look at a few different ways that it can be used and we're going to look at a way that it should never be used here's our starting point for today we have a jenkins lts controller it's version 2.303.1 we have an agent attached to it and this agent is a linux based agent we're also going to be using a sample repository in this video so down in the description you'll find a link to that repository let's go over and take a look at that repository very simple repository we have five different jeans files that we're going to be walking through today let's take a look at the first one jenkinsfile jenkinsfile-1 a very standard very normal looking jenkins file i've got a pipeline agent any i have a stage with a step that has actually two steps in it i have an input and then i'm using the sh step to just echo out hostname and cat etsy red hat release so this is a centos based agent your agent may be different so if you're wanting to go through these examples you might need to change the commands that are being called so let's go over and create our pipeline so new item i'm going to call this input job pipeline click ok let's go now take a look at pipeline script to sem change sem to get there's our url our branch is main and jenkins file dash one let's click on save all right let's go ahead and click on build now and let's see what happens click on one here does the clone we get a hello world so we have a choice we have either submit or abort if we take a look at our pipeline our ok we change to submit so let's go ahead and click on submit and as the job finishes we see our hostname which is agent one and that's the hostname of the agent and you can also see that it's rendering out centos linux seven nine so everything looks pretty good here or is it there's actually a problem with this pipeline let's go back up to our job and let's run it one more time we click on build now and we can see that 2 has started up and if we take a look at two we can see that it's waiting on hello world right it's waiting for us to click on the submit button it's really not a button it's a link but think of it as a button let's go back to the top of our controller if we take a look down here we can see that input job has taken over the executor that is on our agent so if we were to start another job that did not need an input and it could just use this executor but right now we have completely stolen this executor from the agent and we can do no more work on this controller because right now this controller only has the single executor on this one agent so how do we get around this let's go ahead and stop this job and let's go take a look at jenkinsfile2 so if we take a look at jenkins 2 we've actually broken this up a little bit and now we have two stages we have a stage that's just the input and we have a stage that is going to echo out our host name and red hat release but notice how the agent has changed our global agent is now none and we've moved our agent any down into this all done stage where the echoing of the host name and red hat release is happening let's go update our job and take a look at this so if we modify this job click on configure change our jenkins file dash 1 to jenkins file dash 2. click on save click on build now now if we go take a look at build 3 we can see that it's like oh hey here we are let's not click it yet let's go back over to our controller right now we can see that the agent executor is setting idle the reason why that is is the input is just sitting there listening for the answer i don't need an agent executor to wait for this input so i'm on agent none but the input is able to run and it's just waiting for that answer let's go give it an answer in fact let's give it an answer in the affirmative otherwise it will just stop right there click on submit it was approved by admin i'm logged in as admin now we can see the output for hostname and red hat release so by having agent none here and having the input outside of an agent context we are not blocking any processes on our agents from running because we haven't stolen all of the agent executors it's just waiting there nicely for an answer to occur on the controller so this is the big deal do not put input steps inside of an agent right now we have agent none so our input is waiting on agent none and then once we've said yes go ahead then it goes down to the next stage it grabs an agent and then does the work now another way that you can use the input step is you can add parameters to be passed on down through the rest of the pipeline let's take a look at our jiggins file 3. now these parameters are different than the parameters that you would use on the pipeline this is a parameter within the input step now you'll see here i have defined a first name variable it's outside of the pipeline block itself we can see here within steps i've opened up a script block i'm doing input and now my message is just saying hello world i'm now saying what is your first name and then i'm defining the parameters in this case i have a single parameter now by default if there's only one parameter that parameter value is returned back from input into a variable if you had multiple values here then what would happen is this would now be a map but since we only have a single i only need a single variable here again going back to what we talked about in the jenkins file 2 example we have agent none so that means this input is not running on an agent so we're not stealing any agent executors and then we get down into our output stage and we say agent any then our steps we're saying echo good morning first name and then we're echoing out our host name and red hat release just like we did before so let's go back and run this job configure change this to three oops where is it there it is click save and let's click on build now we take a look at this run we have input requested let's click on that it takes us to what is your first name the default's dave i'm going to leave dave in there and click on submit and as it continues on we can see that it says good morning dave and then it echoes out agent one and centos just as we expected from this value now although this worked it's pretty ugly so we have a way that we can clean this up make it a little more readable it may be a little different but it's going to be more readable and more maintainable so let's go take a look at jenkinsfile 4. so let's go up here click on jenkinsfile 4. we've gotten rid of our first name variable that was outside of the pipeline block we still have agent none now we have a single stage we're back to a single stage now you might be saying no wait a minute darren i thought i needed an agent but a single stage wouldn't give me that look at how this is constructed instead of now having our input inside of the steps block we now have an input directive just like there is a steps directive and just like there is an agent any directive so this looks very similar to our jenkins file 3 it's just that it's reconstructed to be a directive so i'm saying input what's your first name okay is submit here's our parameter for first name and then once this is entered what we're going to see is basically the exact same thing that we just saw on the previous run let's go set this up so let's click on that let's go to four so we'll scroll down change three to four and now let's click on build now so we're on five it's saying input requested now before we go on remember we have an agent none and it's waiting for the input just to verify that we can see down here at our executor it's setting idle so everything's still looking pretty good so let's go ahead and click on input job let's go back over to five click on input requested sure dave scroll down and again we get the same answer good morning dave our host name and red hat release so this is a cleaned up version of what we just saw in dash 3. we didn't need to define any variables outside of the pipeline block we didn't need to add in any script blocks within our steps so this is a much cleaner implementation of the previous pipeline now what if you don't even want to run this input step unless you're on a specific branch that's possible so let's go back over and take a look at five now right now you can see that we're on the main branch if we take a look at 5 this is exactly the same as dash 4 except we've added in this win directive and what this is saying is before we even evaluate the input i'm going to check and see if i'm on the branch of production now remember we're actually on the main branch not a production branch production branch doesn't even exist in this repository but in this example that i'm laying out in this case the only time i even want to do the input and therefore these steps is if i'm on the branch named production so let's go modify the job one last time and change it to dash five click save let's do a build now and this will be the most boring output that you've seen so far in this video we can see that the stage input was skipped due to the win conditional what is our win conditional we're not on the branch called production we're on a branch called main so therefore the input is never rendered therefore the steps are skipped 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 cloudbeast tv thanks for watching and we will see you in the next video [Music]
Info
Channel: CloudBeesTV
Views: 990
Rating: undefined out of 5
Keywords: darin pope, jenkins, jenkins tutorial, jenkins pipeline input, jenkins pipeline, jenkins pipeline tutorial
Id: QD6vhbadRJs
Channel Id: undefined
Length: 12min 35sec (755 seconds)
Published: Thu Sep 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.