The Official DevClouds Blogs

logo-devclouds

Jenkinsfile – Jenkins Pipeline 3/4

January 15, 2023

In this article we will learn about Jenkinsfile in Jenkins pipeline. if you want to see our last two lessons.

dev  branch

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

dev branch new file

And I’m gonna create a new file.

basic jenkins 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

Pipeline Syntax

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.

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.

pipeline syntax

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.

Required fields of jenkonsfile

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

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.

sample jenkinsfile

Now we committed it.

Jenkinsfile in Jenkins Pipeline

back to our pipeline

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

jenkinsfile found

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

first dev branch

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

inside 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.

git url

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

build history

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.

build replay

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.

groovy expression

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.

build result

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

build's commands

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.

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.

build stage

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.

Share This Article

Join our newsletter & get update, It’s That Simple!

Join, Blogs, Discount & Promo code !!!

THERE’S MORE TO READ.

What tools you need to learn to become a devops engineer and what are the tasks and responsibilities of a devops engineer.

| January 17, 2023

kubernetes is an open source container orchestration framework. Which was originally developed by google.

| January 17, 2023

Terraform allows you to automate and manage, your infrastructure, your platform and services that run on that infrastructure.

| January 16, 2023

Whenever there is a change in git repository. You want Jenkins to be triggered and changes builds.

| January 16, 2023

what is git exactly it's a version control system but really what that is a system for managing your files.

| January 16, 2023

Git with visual studio code. Source control, version control, backing up whatever you choose to call it.

| January 16, 2023

Leave a Reply

Your email address will not be published. Required fields are marked *

Cyber​​Power Cloud, fast, protected virtual servers, instant setup.

Spin up your choice of virtual machine in just 55 seconds.

OVH

Cloud VPS & Dedicated Servers, advanced hosting solutions

Amazon Lightsail provides easy-to-use cloud resources for web applications

Managed Google Cloud Virtual Machines

Easily deploy cloud servers and storage worldwide

Cloud VPS & Dedicated Servers, advanced hosting solutions

Cloud optimized for performance equipped with fast NVMe drives.

AWS

Amazon EC2, the broadest and deepest compute platform

Managing complex cloud infrastructure made easy

High-Performance Cloud Storage Solutions

10 Clouds 1 Panel

Fully managed by our team

All your clouds and dedi's

All your control panels (CP,DA)

All your wordpress sites&plugins

All of that in one CyberPower

Mega control panel

Reliable Co-Location in ISRAEL Service

Buy and add server hardware components to your co-located servers.

Attacked? We Can Defend You!

Advanced DevOps Services

All about us and what we do in cyberpower

Contact us to activate your custom affiliate plan

Terms & Conditions, Privecy Policy