In this article we will learn about Jenkinsfile in Jenkins pipeline. if you want to see our last two lessons.
- https://devclouds.io/blog/run-jenkins-in-docker-container-jenkins-pipeline-1-4/
- https://devclouds.io/blog/create-multibranch-pipeline-with-git-jenkins-pipeline-2-4/

let’s go ahead and create a Jenkinsfile in dev branch. Let’s go to dev.

And I’m gonna create a new file.

This is the most basic Jenkins file that basically does nothing. But this is the basic syntax we’re going to go through a Jenkins file syntax in more detail in a later section. But here I’m gonna explain you the required attributes for Jenkinsfile in jenkins pipeline.
Pipeline Syntax: Scripted vs. Declarative

So the first thing is that Jenkinsfile can be written as a scripted pipeline or a declarative pipeline. So the scripted pipeline was the first syntax of Jenkins file. What it is it allows to write the whole configuration of the Jenkins file using groovy script.

So the only structure that you have there inside of this is groovy scripts. And you can write the whole configuration here in a flexible manner.
So there is no predefined structure there however for people who didn’t know or doesn’t know groovy syntax. Or who hasn’t worked with groovy is little bit difficult to start with.

That’s why as a recent addition they actually edit declarative pipeline syntax from Jenkins file. Which is easier to get started with it. Of course it’s not as powerful as the scripted pipeline. Because here you’re not limited in. Anyway but it’s easier to get started with because you have a predefined structure. So this what you see here is basically pre given structure that you have to follow.

In a declarative pipeline you declare that you are writing a pipeline. And “agent any” basically means that this build is gonna run on any available Jenkins agent. And agent can be a note it could be executors on that note whatever. This is more relevant when you have a Jenkins cluster so to say with master and slaves where you have Windows nodes and Linux nodes etc.
Basic Syntax

But here for the beginning for base we just say “agent any”. So we’ll just run with the next available agent and as I said these two are equivalent to this one here. And they are required attributes you always have to use them. The next required attribute is “stages”.
And this is where the whole work actually happens so you have different stages of that pipeline. Inside the stages you define stage name and you can define as many stages as you want. Usually you have something like this so you have a build stage. Maybe you have a test stage and then you have a deploy stage like picture above.
As you see inside the stage you have the steps and here goes the script that actually executes some comment on the Jenkins server or Jenkins agent.
For example if you’re building a JavaScript application all the NPM install NPM build etc scripts will go here like this.
If you are you know running tests or for Java back-end all the text execution scripts will be here and the deployment script will be here. So right now let’s just write simple echo comments to kind of test this so here we’re gonna say building the application. And here we’re gonna say testing the application and in here we are deploying the application. And this is a Jenkinsfile and this should work.

Now we committed it.
Jenkinsfile in Jenkins Pipeline

Let’s go back to our pipeline and let’s scan it again.

See the logs and you see here it found Jenkinsfile in dev and now it’s actually executing the Jenkins file.

If I go back to my pipeline view. I can see the first branch the dev branch.

let’s go inside and see the first execution so what happened here the first step that you see is “check out SCM”. The second one was built, then test and then deploy. Here you can see the logs and this is what we echoed in each stage “testing the application” etc.

Now the question is where did this come from. This stage is something we didn’t define before. And it basically checks out the code. So how does this happen this actually comes from my-pipeline that we want git repository URL to be checked out. We basically in UI declaring that we want that checkout stage.
Here inside of that branch configuration you also have some options. Like where you can build the branch manually and see the configuration.
Now what if you want to test some stuff or some changes in a Jenkins file. Let’s say add one of the steps you want to test some groovy expression. And you are new to groovy so you’re just learning and you just want to try this out. It will be more effort to you know adjust the Jenkins file and commit it.
You know it has to check out again the changes and rebuild it again etc so there is actually a handy way to do that in Jenkins. So we can test and execute the Jenkins file changes without having to commit each change.
Replay Jenkinsfile in Jenkins pipeline

The way to do that is go to one of the branches so let’s say in the dev and here you have the history of the build and if you click inside one of those builds.

Here you have a bunch of options and one of them is replay and what replay does is basically allows you to adjust the last run jenkinsfile and rerun it again.

So here let’s say i want to try some groovy expression. The thing is in this inside the steps you can only write the command, line and comments. So if you want to try it or if you want to execute groovy script then you have to enclose it inside script.
Script will be one of the steps so here you can have “echo ‘building the application…’ and inside script we can write some groovy expression. So let’s say let’s just write something whatever so if this is true then we’re gonna say “cool” if not “not cool” and then I’m gonna echo this and let’s run.

It will build now and here if I open build’s logs I see “building the application…” and the new message “cool”. So when now we tested the changes in the Jenkinsfile without having to actually commit it to the repository.
Restart from stage

Another thing that you can do with the specific build is also so if you click on this triangle you actually see all this commands. You can also do restart from stage.

If you click here you see a list a drop down list of all the stages that you have defined. So if one of the stages fails or I don’t know you need to repeat this stage again. You can actually choose one of the stages without having to repeat all the other.

Let’s actually choose the deploy and if I run in again you see you will skip the previous stages and just do the last one which also could be pretty handy sometimes.
We learn about Jenkinsfile – jenkins pipeline.