Audvik Labs

Knowing and Creating Jenkins File

Introduction

Jenkins pipelines can be defined using a text file called Jenkins File. You can implement pipeline as code using JenkinsFile, and this can be defined by using a domain specific language (DSL). With JenkinsFile, you can write the steps needed for running a Jenkins pipeline. 

The benefits of using JenkinsFile are: 

•You can create pipelines automatically for all branches and execute pull requests with just one JenkinsFile. 

•You can review your Jenkins code on the pipeline 

•You can audit your Jenkins pipeline 

•This is the singular source for your pipeline and can be modified by multiple users. 

•JenkinsFile can be defined by either Web UI or with a Jenkins File. 

Declarative versus Scripted pipeline syntax: 

There are two types of Jenkins pipeline syntax used for defining your JenkinsFile. 

Declarative 

Scripted 

Declarative

Declarative pipeline syntax offers an easy way to create pipelines. It contains a predefined hierarchy to create Jenkins pipelines. It gives you the ability to control all aspects of a pipeline execution in a simple, straight-forward manner. 

Scripted

Scripted Jenkins pipeline runs on the Jenkins master with the help of a lightweight executor. It uses very few resources to translate the pipeline into atomic commands. Both declarative and scripted syntax are different from each other and are defined totally differently. 

In Declarative pipeline, the Jenkinsfile start with pipeline block (Mandatory) 

pipeline { 

Inside pipeline block we have agent block (Mandatory) and stages block (Mandatory) 

Inside satges block we should have atleast one stage block 

Inside stage block we should have steps block 

Inside steps block we should have atleast one step(inbuild function name) eg: 

sh step to execute any shell commands 

echo step to print some data 

pipeline { 

    agent any 

    stages { 

        stage (‘Print’) { 

            steps { 

                echo “Hello Devops Engineers” 

            } 

        } 

    } 

agent block is used to tell Jenkins where to execute this Job. By default it executes in the same Jenkins instance. If we have configures slaves for Jenkins, then we can use this agent block to tell the Jenkins to execute the Job in particular slave. 

stage block is used to group the set of tasks 

steps block is used to group the step 

step is the basic unit which executes the command. 

The post block is inside a pipeline block. 

pipeline { 

    agent any 

    stages { 

        stage (‘Print’) { 

            steps { 

                echo “Hello Devops Engineers” 

            } 

        } 

    } 

    post { 

        always {  

            echo ‘I will always say Hello again!’ 

        } 

        success { 

            echo ‘I will say Hello only if job is success’ 

        } 

        failure { 

            echo ‘I will say Hello only if job is failure’ 

        } 

    } 

Even if some stages failed, post block will be executed always. In post block we have three important blocks always, success, failure 

always – If we trigger a job, whether the stage is success or failure, this block will be always executed. 

success – This block will be executed only if all the stages are passed. 

failure – This block will be executed if any one of the stage is failed. 

The triggers block is inside a pipeline block. 

triggers block is used to re-trigger the job based on the defined triggers, it can be cron, pollSCM and upstream. 

cron – Accepts a cron-style string to define a regular interval at which the Pipeline should be re-triggered, for example: triggers { cron(‘H/15 * * * *’) } This will trigger the job every fifteen minutes 

pollSCM – Accepts a cron-style string to define a regular interval at which Jenkins should check for new source changes. If new changes exist, the Pipeline will be re-triggered. For example: triggers { pollSCM(‘* * * * *’) } This will check for new source changes in git repository every one minute. 

upstream – Accepts a comma-separated string of jobs and a threshold. When any job in the string finishes with the minimum threshold, the Pipeline will be re-triggered. 

The parameters block is inside a pipeline block. 

The parameters block is used to pass dynamic paramerts/variables to the job. It has the following types : 

string – A parameter of a string type, for example: parameters { string(name: ‘DEPLOY_ENV’, defaultValue: ‘staging’, description: ‘’) } 

text – A text parameter, which can contain multiple lines, for example: parameters { text(name: ‘DEPLOY_TEXT’, defaultValue: ‘One\nTwo\nThree\n’, description: ‘’) } 

booleanParam – A boolean parameter, for example: parameters { booleanParam(name: ‘DEBUG_BUILD’, defaultValue: true, description: ‘’) } 

choice – A choice parameter, for example: parameters { choice(name: ‘CHOICES’, choices: [‘one’, ‘two’, ‘three’], description: ‘’) } 

password – A password parameter, for example: parameters { password(name: ‘PASSWORD’, defaultValue: ‘SECRET’, description: ‘A secret password’) } 

Creating a Jenkinsfile 

The current Jenkinsfile has two ways of writing, and pipeline if it is the root, it is called Declarative Pipeline. In this case, you cannot write the Groovy script directly, and if you want to write Groovy you script need to use the directive. This article mainly deals here. 

Pipeline If that does not start from, say Scripted Pipeline, to this case also write directly Groovy script, node() arrow stage(), such as, can also be written Pipeline Steps method. Although it seems convenient, degrees of freedom are too high and tend to be craftsmen code. 

Pipeline Editor 

This will open the Blue Ocean Pipeline Editor, which allows users to quickly design and save a new Jenkinsfile in the selected repository. If a Jenkinsfile is already present in this repository, then Blue Ocean will not open editor and create a new Pipeline based on the existing Jenkinsfile instead. You can always edit this Jenkinsfile in the editor by selecting a branch and clicking on the pencil icon. 

Branches and Pull Requests 

When a new Pipeline in Blue Ocean is created, it also creates a new Multibranch Pipeline project for the repository specified. Jenkins will monitor this repository and automatically create a new Pipeline for each Branch and Pull Request in the repository that contains a Jenkinsfile. Removing the Branch, Pull Request, or the Jenkinsfile from the repository will remove the Pipeline from Jenkins as well. 

Users can modify the Jenkinsfile in each Branch or Pull Request to perform different steps, but it is recommended that all of the different Pipeline flows are defined in the primary Jenkinsfile with choices made based on the BRANCH_NAME environment variable or other conditions defined in the stages 

Advantages of Jenkins 

There are abundant advantages of this open-source tool, most impressive benefits of Jenkins: 

•Jenkins is user-friendly and easy to install. You do not require extra installations or tools with it. 

•It is platform-independent. You can find Jenkins on several platforms and operating systems such as Windows, Linux, OS X, etc. 

•Configuration of Jenkins is extremely easy. You can modify or extend it. Moreover, the deployment of code and the generation of test reports are done quickly. For continuous delivery and continuous integration, you can configure Jenkins according to your requirements. 

•It consists of a rich plugin ecosystem. The massive collection of plug-ins makes Jenkins more flexible and permits building, deploying, and automating across several platforms. 

•It is free of cost. 

•With Jenkins, you receive incredible support. As you have seen, it is the most popular open-source server available; therefore, you receive agile teams to resolve your queries all across the globe. Along the journey, there are multiple issues faced by the users. Such obstacles are solved immediately, which helps to keep the software in a position where you can release it any time with utmost safety. 

•The integration work is mostly automated. Therefore, there are fewer integration problems. This further helps to save money and time in the journey of every project. 

•The developers can detect errors in the code as soon as possible. This saves them from large-scale error-coated integrations. 

•Moreover, in collaboration with Docker, Jenkins escalates the progress of the development team working on different projects. It also provides the software infrastructure with Agile development throughout the journey 

Conclusion 

With Jenkins, software companies have the opportunity to escalate the software development process. It is possible to automate, build, and test processes at a rapid rate. It gives you the liberty to track repeated tasks that occur in the development of a project.  

In a nutshell, if you wish to escalate your organization’s progress in the field of software development, then Jenkins is the best pick for you. 

Leave a comment

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