Execute automation scripts using Pipeline in Jenkins

automation scripts using pipeline in Jenkins

What is Jenkins?

Jenkins is a Continuous Integration (CI) Server or open-source automation tool written in Java with plugins built for Continuous Integration purpose. Jenkins supports the complete development lifecycle of software from building, testing and deploying a software development lifecycle.

For example, if the team is developing a project, Jenkins will continuously test your project builds and show you the errors in the early stages of your development that’s the reason Jenkins is more popular.

What is Jenkins Pipeline?

Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. Jenkins Pipeline is written into a Pipeline script. In Jenkins Pipeline it allows us to fill in details for the different steps of our Jenkins job.

Below is the diagram of the Jenkins Pipeline workflow.

automation scripts using pipeline in Jenkins

Why do we use Jenkins pipeline?

Here are the reasons why you should use Jenkins pipeline:

  • Jenkins pipeline is allowing multiple users to edit and execute the pipeline process.
  • Pipelines are robust. So, if your server goes down then the pipeline will be automatically resumed.
  • Users can pause the pipeline process and make it wait to resume until there is a new input from the user.
  • Jenkins Pipelines allows you to run multiple jobs, and even use pipelines in a loop.

Jenkins Pipeline Concepts

Node: A node is a machine that executes an entire workflow. It is a main part of the scripted pipeline syntax. By default, master is a node for every single machine.

Stage: This block contains all the work that needs to be carried out. The work is specified in the form of stages. There can be more than one stage within this directive. Each stage performs a specific task. Stages show up as columns in the Pipeline Stage view with average stage times and colors for the stage result.

Step: Step is like a single command which performs a single action. The last required section is steps, which are defined in a stage. At least one step must be defined in the steps section. When a step succeeds it moves onto the next step. When a step fails to execute correctly the Pipeline will fail.

  • For Linux and MacOS, sh is supported
  • For Windows, bat or powershell can be used, as shown:

Configuration of Pipeline

A Pipeline is a way of defining some Jenkins steps using code, and automate the process of deploying software.

Pipeline defined in two different ways.

  • Declarative Pipeline
  • Scripted Pipeline

We are going to use the Scripted Pipeline which is following a more imperative programming model built with Groovy.

Install Pipeline Plugin in Jenkins

By using pipeline plugin, you can create your different stages in the pipeline view, and in Build History, you can check every built log with date and time and you can also view your logs on the console window. If your any stage is getting failed then stop the current stage as it would be marked as Failed too. Do not go to the next stage.

Here is how you can install the pipeline plugin in your Jenkins:

1. The settings for the plugin can be found under Manage Jenkins > Manage Plugins.

automation scripts using pipeline in Jenkins2. Click on Available Tab > Search Pipeline Plugin in the Filter box, if you have already installed the plugin, it will be displayed under the Installed tab and if you have not installed plugin previously, it shows up under the Available tab.

automation scripts using pipeline in Jenkins

Create Jenkins Pipeline Project

Once you have successfully installed the pipeline plugin in your Jenkins, you will be getting displayed with Pipeline Project option in Item list.

1. Go to the Jenkins dashboard and select New Item from the dashboard.

automation scripts using pipeline in Jenkins2. Next, enter the name of the pipeline project and select pipeline project. Click on ok to proceed.

automation scripts using pipeline in Jenkins3. Click on Pipeline tab for move to the pipeline section.

4. Click on Pipeline Syntax for creating pipeline script and check Use Groovy Sandbox.

automation scripts using pipeline in Jenkins5. Pipeline Syntax page will open, “select checkout: check out from version control” option from Sample step

6. Select “Subversion” option from SCM dropdown (for svn version control).

7. In the modules section,

a) In Repository URL give the URL of the repository.

b)On Credentials field click on Add button select Jenkins option,

c) In Jenkins Credentials adding a credentials of svn. Give the username and password.

automation scripts using pipeline in Jenkins8. Select “Use svn update as much as possible” option from Check-out Strategy.

9. Click on Generate Pipeline Script It will generate a mess of code (in groovy language) which you can copy and paste in the pipeline. Then all you need to do is just paste it in the pipeline script area as displayed in the below image.

automation scripts using pipeline in Jenkins

Note: Please refer to the below image which describes the overall steps that we have performed from step no. 5 to 9.

automation scripts using pipeline in Jenkins

10. I am using ANT build tool for run automation script So, I call the targets (which target you created in your Automation project). Below are the steps for calling the ANT task.

Note: First, make sure that build tool is should be configured in Jenkins. Configure Build tool or JDK by clicking Manage Jenkins on the left sidebar and the go to the Configure System page and on this page configure of build tool (which build tool you used) and JDK.

a) Click on Pipeline Syntax (in Step #4 of creating pipeline project)

b) Select “bat: windows Batch Script” option from Sample Step

automation scripts using pipeline in Jenkinsc) In Batch script give Build.xml file location of Automation Project (location of new checkout using pipeline in Jenkin’s workspace).

Note: Location of build.xml file as displayed in below screenshot (Jenkins workspace).

automation scripts using pipeline in Jenkins

syntax which will be added in the “Batch Script” section:

automation scripts using pipeline in Jenkins

d) Click on Generate Pipeline Script button and then copy generated code and paste in the Pipeline script as displayed in the below screenshot.

automation scripts using pipeline in Jenkins

11. After creating the jobs, Click on Save. we’ll save our script

Now let’s quickly summarize the steps which we have performed, so please refer the below example:

1. First, I generated Groovy code from pipeline syntax for checkout Framework from SVN Repository. Copy generated groovy code and paste in stage(“Framework-checkout”).

NOTE: follow above #4 to #9 steps.

2. Second, I generated Groovy code from pipeline syntax for checkout Automation Project from SVN Repository. Copy generated groovy code and paste in stage(“Automation-checkout”).

  NOTE: follow above #4 to #9 steps.

3. Third, I generated a Groovy code by executing the Automation script using ANT Target. Copy generated groovy code and paste in stage(“Execution”).

 NOTE: follow above #10 step.

automation scripts using pipeline in Jenkins

Run Jenkins Pipeline Project

1. Select Created pipeline from dashboard.

2. Click on the Build Now and the execution will start and getting displayed under Build History. In build History you can see all execution with date and time,

Here’s an overview of the builds and we’ll find the stage view of the pipeline, with the result of each stage:

automation scripts using pipeline in Jenkins

Each output is accessible when hovering over a stage cell and clicking the Logs button to see the log messages printed in that step.

Each row also records the build number, date it was started, and any changelog entries from a version control system. Progress bars also indicate how long each stage is taking and how long it might still be expected to take, based on historical averages.

NOTE: If you want to edit existing pipeline then go to the dashboard > click on pipeline project > click on Configure option from left side and you can do changes in the configuration of Pipeline Project.

The following two tabs change content below.

Shakuntala Choudhary

QC Automation Engineer at Dev Information Technology Ltd.
I'm a QC Automation Engineer of Testing Department, at Dev Information Technology Limited. I look upon the projects related to Automation & Performance Testing. I having knowledge of the Automation Testing.

Latest posts by Shakuntala Choudhary (see all)

Leave a Reply

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