Jenkins Pipeline Tutorial: Declarative Pipeline vs Scripted Pipeline

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
oh hello stranger welcome to the new video if you clicked on this video because you are looking for information about differences between the Clara lift pipeline and the script pipeline you can find a better place in this video I will show you four significant differences between the Clara the pipeline and scripted pipeline some blog posts try to answer this question of a day focused mostly on those I would call it like a philosophical differences between both pipelines but here I will focus only on the most practical information ever found okay stay with me for a few minutes let's start with a following question why there are two types of pipelines in the first place so the scripted pipeline is the first implementation of pipeline as a code in Jenkins and even though it was built upon this pipeline subsystem it was designed more or less as a general purpose DSL build in groovy the declarative pipeline on the other hand is more opinionated its syntax is well-structured so it may sound that the declarative pipeline is more limiting but actually you can build the same things using scripted pipeline or the declarative pipeline so the question is which one to choose if you ask me this question and expect an answer different then it depends I would say use the declarative pipeline and here's why reason number one pipeline code validation at startup let's take a look at the following example here I have some regular Jenkins pipeline job and let's define the following scripted pipeline so skirt the pipeline starts with node and here is your pipeline code you can put whatever you want you can define stages but you are not forced to do so but we will build two stages let's say like a build stage and I will just echo something like a building there will be the second stage that you the cold test and here I will make a mistake so I will call the echo step with 123 it's an integer echo step expects string so this pipeline will fail let's take a look at the error message that we will see here so let's start the pipeline and here you can see that could not instantiate message 1 to free echo step class cast exception it failed as expected but the problem is that the build stage was executed now imagine build stage running for I don't know let's say a few minutes and it needs those few minutes just to fail because you made this goofy mistake let's take a look at the declarative pipeline so in the declarative pipeline which starts with the pipeline here the structure is much more opinionated so we have we need to specify agent I will use just any agent here we have stages block and we will have stage called build inside stage we need define steps the step is echo building there is also stage called test with the following steps there is echo 1 2 3 the same exactly the same pipeline logic a little bit different syntax as you can see but there is also a completely different result I executed this pipeline and it failed immediately you can see that there was expecting class java.lang string but got 1 2 3 of type class integer so the pipeline validation immediately failed my pipeline and told me what is the exact problem I didn't have to spend time on running the build stage only - you see that the pipeline is configured incorrectly let's get back to the previous scripted pipeline example let's fix it so we will echo testing here let's save it and let's run our pipeline again now it didn't fail it succeeded as expected but there is one thing that declarative pipeline has and scripted pipeline is missing if there was some issue let's say on a very environmental issue which is has nothing to do with our source code if I want to restart my pipeline and I only want to restart test stage I need to restore the full pipeline now imagine that your pipeline releases a new version it builds deployable artifacts it deploys it if you rerun the pipeline without any code changes you will just spend some CPU cycles and resources for nothing because the source code is exactly the same so with the scripted pipeline there is no such feature as restart from stage this is what declarative pipeline has and now let's get back to our pipeline example and let's build again the same pipeline but using declarative pipeline okay so quickly stages we have stage built steps echo building stage test steps echo testing okay we have it now we can rerun the pipeline it will produce the same stages the same output but the difference is that now I have those options build and test and this is because the declarative pipeline syntax is opinionated the structure is opinionated so the pipeline subsystem knows that there are stages and it knows how to restart those stages of the scripted pipeline there are no structures there is no guarantee that stages are serializable so that's why this feature is missing so now if I click on restart test the next pipeline starts with the build that is skipped because the workspace is restored so any items that the build stage produced are already here and I already only run the test stage so now imagine you have a stage like a post deployment checks you deploy the the item and then you want to run some tests maybe regression tests whatever and those tests are highly dependent on some environment configuration let's say the environment configuration that is not stored in your source code so it may happen that it fails but you need to change something elsewhere and then just restart post-deployment checks with this solution with the declarative pipeline you don't have to release again create the playable artifact again from the same source code you can just restart post-deployment checks anywhere okay reason number three declarative pipeline options this feature is not unique to declarative pipeline but it makes it much easier to use take a look at the descriptive pipeline I prepared for you here we have three features edit or pipeline the timestamps it means that every line in console.log will have a timestamp we also want to use NZ color and console logs so any kind of text that uses NZ colors will be printed in in this color and we also use a time-out option for this build stage so as you can see this pipeline should fail because the timeout is one second but here we have this sleep for five seconds ok so if we run this pipeline here we will see it failed it produced output with timestamps and the hello world is also printed with with the color ok what's the problem with that well as you can see here we had to nest like a free blocks timestamps and they call our timeout now imagine you add some new options to your pipeline and then you need to nest those blocks even even further how the declarative pipeline solve that in declarative pipeline we have a different syntax for that so let me remove this and let's define the de Klerk pipeline again we will use any agent here we will define a global options so options at the pipeline level means that these options will be applied to any stages so we want to add timestamps and we want to add NZ call or X term then we have stages stage build and in stage build we can add options that are at the stage level so here I want to specify time out for this one second and then we have those steps here okay what's the advantage compared to the scripted pipeline here those options they are not you know mixed with our pipeline logic so here I know that okay the time old for this stage is one second here we have some global options and those options they don't you know like a move nested blocks further because here I could add them actually without affecting how many nested blocks I have in my pipeline configuration so let's give it a try let's see how this pipeline works seconds of course my mistake here let's apply it let's run it and here we can see that we have time stamps we have an z color output and the timeout also worked as expected the last feature I want to show you is skipping stage of functionality built in the declarative pipeline here we have the pipeline we created earlier with one small difference this time the test stage uses a 1 block which defines conditions on which this stage gets executed in our case we use an expression that evaluates the environment variable cold something is equals to Lauren Epsom we also use this before agent just to control when this when block is executed so in this case it means that before Jenkins actually forwards that the job execution to agent it will check this wind condition it might be crucial especially if you run stages on different nodes and running on a different node means that your source code has to be checked out it has to be the workspace has to be initialized so in this case we will do this check on a master and there is no need to check out your source code repository just to evaluate that this stage should be skipped at this moment so let's see what does it look like when we run the pipeline and we can see that the test stage was skipped we still see it in a blue ocean UI so this is the information for us okay there were conditions in which tests a test stage should be skipped what are the options in scripted pipeline well you could simulate something similar like this using some workarounds but there is nothing that is very like a strict having this kind of option like the stage that is marked as skipped requires you enabling some utility functions from the Jenkins API alternatively you could just create very simple pipeline like this so we have our stage build with this echo building and then you have this F here so we check if something equals lorem ipsum and if this is true we will run the test stage okay but unfortunately it does not produce the same output here the test stage is completely missing this might be a problem because you don't know if test stage was skipped in tension or maybe there is some kind of problem that it was removed from the pipeline so these are my four reasons why you should consider the carotids pipeline over the script one what is your opinion which pipeline type do you prefer and why a please leave me a comment in the comment section down below and in the meantime see you in the next video take care [Music]
Info
Channel: Szymon Stepniak
Views: 30,714
Rating: 4.9540668 out of 5
Keywords: jenkins pipeline, jenkins scripted pipeline, jenkins declarative pipeline, jenkins pipeline tutorial, jenkine pipeline groovy, jenkins pipeline for beginners, jenkins pipeline groovy, jenkins declarative pipeline tutorial, jenkins scripted pipeline tutorial, jenkins scripted pipeline vs declarative, jenkins declarative pipeline vs scripted, jenkins tutorial, jenkins tutorial for beginners, jenkins pipeline groovy tutorial, jenkins pipeline as a code, jenkins groovy pipeline
Id: Ei_Nk14vruE
Channel Id: undefined
Length: 13min 45sec (825 seconds)
Published: Sun Jan 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.