Jenkins

Jenkins Scripted Pipeline

Jenkins is an open source continuous integration server that provides the ability to continuously perform automated builds and tests. Several tasks can be controlled and monitored by Jenkins, including pulling code from a repository, performing static code analysis, building your project, executing unit tests, automated tests and/or performance tests, and finally, deploying your application. These tasks typically conform a continuous delivery pipeline.

In Jenkins, Pipelines are specified by using a specific DSL following almost the same structure as Groovyto specify its statements and expressions. This makes pipelines easy to use for Groovy savants.

In this post we will discuss the Scripted Pipeline (Pipeline as Code) in detail, while explaining its structure and providing examples of how to use it

How to Create Your Jenkins Pipeline

With the introduction of the Pipeline, Jenkins added an embedded Groovy engine, making Groovy the scripting language in the Pipeline´s DSL.

Here are the steps you need to take to setup a Jenkins Pipeline.

1. First, log on to your Jenkins server and select “New Item” from the left panel:

Increase image
create your jenkins pipeline

2. Next, enter a name for your pipeline and select “Pipeline” from the options. Click “Ok” to proceed to the next step:

Increase image
use your jenkins scripted pipeline

3. You can now start working your Pipeline script:

Increase image
pipeline script in jenkins

The red box in the middle is where you can start writing your script, which will be explained now.

Creating Your Jenkins Pipeline Script

 
Pipelines has specific sentences or elements to define script sections, which follow the Groovy syntax.

Node Blocks

The first block to be defined is the “node”:

node {
}

A “node” is part of the Jenkins distributed mode architecture, where the workload can be delegated to multiple “agent” nodes. A “master” node handles all the tasks in your environment. Jenkins agent nodes offloads builds from the master node, thus performing all the pipeline work specified in the node block. Detailed information on this can be found at Jenkins Distributed builds.

This block is not mandatory but it is desired and can be considered as good practice, since with the code included in this block, Jenkins will schedule and run all the steps once any node is available and creates a specific workspace directory.

Stage Blocks

The next required section is the “stage”:

stage {
}

Your pipeline will consist of several steps that can be grouped in stages.  Among these stages you might have:

  • Pull code from repository
  • Build your project
  • Deploy your application
  • Perform functional tests
  • Perform performance tests

Each of the these can include more than one action. For example, a stage to deploy your application can consist of copying the files to a specific environment for functional tests and to a dedicated server for performance tests; and once files are copied successfully, proceed to deploy it.

Each stage block specifies the tasks to be performed. For example, a full scripted pipeline might be:

node {
		stage (‘Build’) {
			bat "msbuild ${C:\\Jenkins\\my_project\\workspace\\test\\my_project.sln}"
		}

stage('Selenium tests') {
dir(automation_path) {  //changes the path to “automation_path”
bat "mvn clean test -Dsuite=SMOKE_TEST -Denvironment=QA"
}  
}
	}

This script has the following stages:

  • Build stage:
    • bat “msbuild…”: Builds the project by specifying a Visual studio solution file.
  • Selenium tests stage:
    • dir(automation_path): Changes the current directory to the value set on the automation_path variable.
    • bat “mvn clean test … “: Invokes maven to perform tests specified in the suite “SMOKE_TEST” and using the values defined on “QA”. Also, the “clean” flag  will clean the build.

Stage blocks are also optional, but they are recommended because they provide an organized way of specifying tasks to be executed in the script.

Jenkins provides an interface that generates pipeline sentences for predefined actions that can be added to any of the script stages. On your pipeline script page, click on “Pipeline Syntax” to access the following page:

Increase image
how to script in jenkins pipelines

For example, to create a script command to execute a windows batch file, select the following:

Increase image
guide to jenkins scripted pipelines


Clicking on “Generate Pipeline Script” will create the desired sentence that can be added to your script right away.

The Jenkinsfile

Pipeline as code is based on the idea of being able to add the pipeline script to a code repository for source control and versioning. The text file containing the code of your pipeline is also known as a Jenkinsfile.

Writing your pipeline into a Jenkins file and make it part of your application repository for source control has several advantages: it can be reviewed/edited by other team members and the file can be versioned and included with your application builds.

Your Jenkinsfile can be edited through the Jenkins web interface or with a text editor, and you can also edit it with your prefered IDE, thus making it part of your project. Then, you can configure Jenkins to automatically poll your repo, while triggering new builds when updates to it are detected. This can be done through the following screen on your Project configuration under “Build triggers” section:

Increase image
jenkins scripted pipeline tutorial

Enabling “Poll SCM”, allows you to enter a cron like expression in the Schedule text box. Configuring Jenkins to poll your repo is not a clean and efficient way to retrieve updates. Instead, Git Hooks is neat way of doing it. The document at Customizing Git – Git Hooks provides information on how to configure it.

References:

https://jenkins.io/doc/book/pipeline/

https://jenkins.io/doc/pipeline/tour/hello-world/

https://www.blazemeter.com/blog/how-to-use-the-jenkins-scripted-pipeline

Categories: Jenkins

2 replies »

Leave a Reply