How to Write a Pipeline Script in Jenkins

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to talk about how to write a pipeline script in jenkins [Music] are you new here if you are welcome and if you are new here you don't know who i am my name is darren pope and i'm a developer advocate for cloudbees around december of 2014 the jenkins project introduced scripted syntax pipelines and around february of 2017 declarative pipelines made their entrance now why do you want to use pipeline you have two different syntaxes you can choose to use declarative or you can choose to use scripted back in 2015 before declarative came out all you had was scripted but once declarative was ga in roughly february of 2017 that gave you a better way to construct a vast majority of your pipelines now what we're going to look at today is a very basic way if you've never created a pipeline script before i'm going to show you a quick and easy way to do that within jenkins itself now you may be asking what is a jenkins pipeline well jenkins pipeline is a suite of plugins that makes it possible to create the definition of a job within a file if you've used jenkins in the past you may be familiar with freestyle jobs or maven jobs or there's numerous job types but pipeline is the only one that makes it possible to be able to save the definition of your job to an scm such as get so you'll be able to manage the definition of your jobs as code let's talk about what we're using today to do our demonstration i have a jenkins controller running 2.27.2 when it was installed it was installed using install suggested plugins and also to this controller we have attached an agent so let's go ahead and jump right into it what we're going to do today is we're going to create two sample jobs i'm going to say create a job and i'm going to say scripted pipeline just so you can see how to do it i'm going to click on pipeline and click ok now remember i was just talking about saving the definition of your jobs to an scm we're not going to do that today that's a little more advanced if you're just now getting into it i want you to understand how you can quickly create a pipeline job and you can look at other videos to understand how to save that and pull it back into a job for later usage all right so let's go ahead and scroll down to the section that is called pipeline script and in fact there's a little helper over here on the right-hand side that says try a sample pipeline let's go ahead and click on scripted pipeline and what you'll see here is this is a very pretty advanced type pipeline let's go ahead and simplify this a lot so let's go ahead and just leave first the node block at the top and we're going to leave a stage stages where material type work happens so think of material as you'll see in a minute and here what we want to do instead of results i'm just going to say hello and i'm also going to say echo hello world and i'll change these to singles doubles doesn't really matter for this use case let's go with singles for today okay so now we have node stage echo hello world let's click on save and click on build now and you'll see here that it ran and you can see that it says running on agent one that is our agent that we have connected to our controller and then it echoes out hello world so let's take a look at that one more time we have roughly five lines that gives us a hello world node says make sure this runs on an agent my stage is where the what's happening in this job and then here is the actual thing that's happening in this case it's an echo now let's create a declarative job so we'll click on dashboard new item and we'll call this declarative pipeline we'll click on pipeline and click ok again we'll scroll right back down to our pipeline script section we're going to try a hello world and this hello world is exactly the same as what we just did with our scripted syntax pipeline now scripted syntax it was five lines this one is roughly with extra spaces here 11 lines so let's go ahead and save it just to prove that when this one runs it does the exact same thing as our scripted syntax let's go ahead and click on build now when we take a look at the output we can see that it ran on agent 1 and it also echoed out hello world this is the exact same output as we saw with our scripted syntax pipeline this has been a short video just to give you a little bit of information if you've never seen what a jenkins pipeline is before you saw that we had two different syntaxes that we can use we had scripted syntax and we have declarative syntax in that time between scripted syntax and declarative syntax obviously all we had to work with was scripted syntax but once declarative syntax went ga that gave us other options in how to construct our pipelines since the release of declarative pipeline here's the basic recommendations start with declarative syntax when you start to run into some issues with declarative syntax whatever that might be we've covered that in other videos you would then add on a shared library go check out the shared library video that's listed down in the description or it may be up here just to my right also if the combination of declarative and shared library starts to fall apart for you as well you've taken it to the edge then and only then should you completely construct a pipeline using scripted syntax the reason why is scripted syntax can be more complex yes it is a little bit shorter as compared to what we saw in declarative a word of warning sometimes people will treat scripted syntax as if it's a general purpose programming language it is not scripted syntax as well as declarative syntax are meant for ci purposes only if you need to integrate with other systems use a real programming language go java your pick doesn't matter write whatever that functionality is within a real programming language that you can put real tests around and then once you have that written whether that's a simple script maybe you're writing a bash script or maybe a powershell script or something maybe even more complex as a full client and go now you have something that you can call from the command line once you are able to test all of that from the command line then it's very simple whether you're using scripted or declarative to simply make an sh call if you're on linux or a bat call or powershell call if you're on windows to make the call to that tooling that you have written 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 is new content available on cloudbees tv thanks for watching and we will see you in the next video you
Info
Channel: CloudBeesTV
Views: 3,618
Rating: undefined out of 5
Keywords: darin pope, jenkins, jenkins tutorial, jenkins pipeline, jenkins pipeline tutorial, jenkins pipeline as code, jenkins declarative pipeline, how to create jenkins pipeline, continuous integration, jenkins tutorial for beginners, jenkins scripted pipeline, jenkins pipeline groovy tutorial
Id: TiTrcFEsj7A
Channel Id: undefined
Length: 8min 57sec (537 seconds)
Published: Thu Apr 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.