Jenkins Scripted Pipeline

Jenkins Scripted Pipeline | How to write from scratch [2022]

Jenkins is one of the popular CICD tools which is used for Automation. In this tutorial, we will understand what Jenkins Scripted Pipeline is and understand Jenkins pipeline by small examples.

What is the Jenkins Scripted Pipeline

 the Jenkins Scripted Pipeline

In Jenkins, a pipeline is a collection of events or jobs that might be interlinked together in a sequence. In Jenkins, users can define the Pipeline using below two methods.

  • Scripted pipeline
  • Declarative pipeline

In the initial days of the Jenkins Pipeline, there was only one way to define the Pipeline, which we now call a Scripted pipeline.

Jenkins scripted pipeline uses domain-specific language(like groovy) that simplified numerous tasks while creating a pipeline. In a scripted Pipeline, users can inject a groovy code during declaring. One of the major advantages of scripted pipelines is that they are very flexible and expressive.

Syntex of Jenkins Scripted Pipeline

The JenkinsScripted Pipeline generally start with one or more node block

node { 
  stage('Build') 
}

Although defining aScripted Pipeline with the node is not a mandatory requirement but confining your Pipeline inside of a node the block provides various advantages like

  • Queue the stage and run the stages as soon as the executor is free.
  • Creates a workspace where work can be done

I will explain how to create a scripted pipeline in Jenkins in the next session.

Jenkins Scripted Pipeline examples

Jenkins Scripted Pipeline examples

To create a scripted pipeline in Jenkins, go to Jenkins UI and click on New item.

provide the pipeline name and select Pipeline, and then click on ok

Jenkins Scripted Pipeline

Now goto pipeline session and paste the below code

node {
    stage('test') {
        echo 'hello'
    }

Code Explanation

  • Node signifies the starting point of the pipeline
  • Provide the stage name as test
  • Print ‘hello’ in shell
Jenkins Scripted Pipeline

Click on save to save the Pipeline

Now click on the build now button

Jenkins Scripted Pipeline

Once the job is successful, you will get the below window

Jenkins Scripted Pipeline run

Click on #1 to see the job logs > click on the console output

Jenkins Scripted Pipeline logs

From the above o/p, it is clear that the job ran successfully

Awesome, you have successfully understood and run your first JenkinsScripted Pipeline; let’s proceed further and understand how to use scripted Pipeline in Github

Jenkins Scripted Pipeline GitHub

jenkins Scripted Pipeline GitHub

In this session, we will be pulling a git repo using JenkinsScripted Pipeline and running a bash script file to print hello world.

Go to Jenkins UI and click on New item. provide the pipeline name as scripted_Pipeline_ex_2 and select Pipeline, and then click on ok

Jenkins Scripted Pipeline GitHub

Now go to the pipeline session, paste the code below, and click on the Save button.

node {
    stage('cloning git repo') {
        git branch: 'main', url:'https://github.com/naiveskill/devops.git'
        sh "chmod +x -R ./jenkins"
        sh "./jenkins/script/scripted_pipeline_ex_2.sh"
    }
}

Explanation

  • Node signifies the starting of Scripted Pipeline
  • In the next step, we are stage to pull git repo “https://github.com/naiveskill/devops.git”
  • Once the code is successfully pulled from git, we need to change the file permission before running the script
  • Finally, in the last step, we are running a shell script file scripted_pipeline_ex_2.sh which basically print out “HELLO WORLD”
#!/bin/sh
echo "HELLO WORLD"

Click on build now and wait for Jenkins job to complete

As you can see from the above output the above job is successfully ran

Jenkins Scripted Pipeline parameters

In a scripted pipeline, we can pass parameters by using properties. Below is the working example of a simpleScripted Pipeline with parameters

node(){
    properties([
        parameters([
            string(defaultValue: 'world', name: 'Name')
            ]
    sh "hello ${Name}"
}

Jenkins pipeline supports many parameters like string, boolean, file, etc.

Jenkins Scripted Pipeline environment variable

In this session, I show you how to use environment variables inJenkins Scripted Pipeline. Theenvironment variable can be set using the withEnv keyword in a scripted Pipeline.

Now let’s create a scripted pipeline, define two environment variables FName and LName, and finally print out the value of theseenvironment variables.

Go to Jenkins UI and click on New item. provide the pipeline name as scripted_Pipeline_ex_4 and select Pipeline, and then click on ok

Copy-paste the below code in the pipeline script

node{
    withEnv(['FName=Naive','LName=skill']) {
        stage('Build') {
            sh 'echo $FName $LName'
        }
    }
}
Jenkins Scripted Pipeline environment variable

Click on the Save button, run the Pipeline, and observe the output.

Jenkins Scripted Pipeline environment variable

Conclusion

In this blog, we have understood what is Jenkins scripted pipeline is and understood the Jenkins scripted pipeline via different examples. I hope you liked this tutorial on Jenkins Scripted Pipeline.

Please do let me know in the comment box if you face any issues while following along.

More to read

Deploy Jenkins on Kubernetes using Jenkins operator

Jenkins create a python virtual environment

Jenkins workflow

3 thoughts on “Jenkins Scripted Pipeline | How to write from scratch [2022]”

  1. Tһanks for your personal marvelous posting! I truly enjoyed reading it, you will bе a great author.

    I will make certain to bookmɑrk your blog and will eventually come back in the foreseeable
    futսre. I want tо encourage one to continue your great writing, haѵe
    a nice afternoon!

Leave a Comment

Your email address will not be published.

Scroll to Top