DEV Community

Logesh Sakthivel
Logesh Sakthivel

Posted on

Jenkins Pipeline script

In this article, we are going to write a simple Jenkins Pipeline script using some variables and functions that runs on the Agent we specify using the label.

The main goal of this article is to use variables,functions and agent labels in the Pipeline script. 

There are two ways of defining the Pipeline script in the Pipeline project.

  • Directly writing the Script in the Pipeline section while creating the Pipeline Project.
  • Pipeline Script from SCM (Source Code Management) using Jenkinsfile.

In this article we will be following the First way(Pipeline script).


Pipeline Script

Create Pipeline

In the Jenkins Dashboard, click New Item and create a Pipeline Project.

Create Pipeline

Scroll down, in the Pipeline section we are going to write our Pipeline Script(Groovy).

Pipeline

Pipeline Script

Simple pipeline script that executes bash commands in the agent that I specified.

pipeline {
    agent {
        label "ubuntu"
    }
environment {
        def myString = "Hello World"
        def myNumber = 10
        def myBool = true
    }
stages{
        stage("Node Info") {
            steps {
                echo "Node name $NODE_NAME and the labels: $NODE_LABELS"
            }
        }
        stage("Basic shell cmds") {
            steps {
                executeBasicShellCmds()
            }
        }
        stage("Variables") {
            steps {
                echo "myString: ${myString}"
                echo "myNumber: ${myNumber}"
                echo "myBool: ${myBool}"
            }
        }
    }
}
def executeBasicShellCmds(){
    sh """
    pwd
    ls -a
    uname
    whoami

    """
}
Enter fullscreen mode Exit fullscreen mode

Pipeline Script breakdown

All the pipeline related stuffs comes within the pipeline block. Fucntions will be written outside the pipeline block.

To define a Pipeline,

pipeline{
....
}
Enter fullscreen mode Exit fullscreen mode

To define variables,

environment {
        def myString = "Hello World"
        def myNumber = 10
        def myBool = true
    }
Enter fullscreen mode Exit fullscreen mode

To define the stages(Pipeline stages),

stages{
        stage("Stage 1") {
            steps {
                ...
            }
        }
        stage("Stage 2") {
            steps {
                ...
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

The functions should be defined outside the pipeline block.
To define a function with and without parameters,

def func1(){
    echo "function 1"
}
def func2(String a){
    echo "${a}"
}
Enter fullscreen mode Exit fullscreen mode

Pipeline script

Click save.

Build Now
In the Project's page click on Build Now to build the Project.
You can see the Build status on Stage view or in the Build history

Build Output

Click on the each stage to view the logs.

Stage 1

Stage 2

Stage 3


In this article, we have learned how to use the Pipeline script in the Pipeline project with the simple pipeline script.

Happie Learning :)
Keep Learning!!!

Top comments (0)