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
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.
Jenkins Scripted Pipeline syntax
The JenkinsScripted Pipeline generally starts with one or more node block
node { agent { //agent name } options { //<option1> //<option2> } stage('Build'){ // build code } stage('deploy'){ // deploy code } post { <condition> } }
Jenkins scripted pipeline agent
In a Jenkins scripted pipeline, the “agent” section is used to specify the agent on which the pipeline will run. The agent section is optional, but if specified, it determines the machine executing the stages defined in the pipeline.
The agent section can be defined at the top level of the pipeline script, or it can be specified within individual stages.
agent { <label> <node> <customWorkspace> }
Jenkins scripted pipeline options
The options section in a Jenkins scripted pipeline allows you to specify options for the entire pipeline, such as timeouts, notifications, and environment variables. The options section is optional, but if specified, it can provide additional control and customization for the pipeline execution.
Some of the options that can be specified in the options section include:
- timeout
- timestamps
- buildDiscarder(logRotator)
- environment
Below is an example of how the options section can be used in a scripted pipeline:
node { agent any options { timeout(2h) timestamps buildDiscarder(logRotator(numToKeepStr: '3')) } stage('Build'){ // build code } }
Jenkins scripted pipeline stage
A stage in Jenkins scripted pipeline represents a phase in the software development lifecycle. Stages help to break down the pipeline into smaller, manageable steps that can be executed in parallel or sequentially.
Below is an example of a stage in a Jenkins scripted pipeline:
node { agent any stage('Build'){ // build code } stage('test'){ // test code } }
Jenkins scripted pipeline post
The “post” section is used to specify steps that should be executed after all stages have been completed. The “post” section is optional.
Below is an example of how the “post” section can be used in a scripted pipeline.
node { agent any stage('Build'){ // build code } post{ success { echo 'Project build successfully!' } failure { echo 'Project build failed!' } } }
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
Now go to the 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 a test
- Print ‘hello’ in the shell
Click on save to save the Pipeline
Now click on the build now button
Once the job is successful, you will get the below window
Click on #1 to see the job logs > click on the console output
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
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
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 the Scripted Pipeline
- In the next step, we are stage to pull the 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 prints out “HELLO WORLD”
#!/bin/sh echo "HELLO WORLD"
Click on build now and wait for the Jenkins job to complete
As you can see from the above output the above job 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 into the pipeline script
node{ withEnv(['FName=Naive','LName=skill']) { stage('Build') { sh 'echo $FName $LName' } } }
Click on the Save button, run the Pipeline, and observe the output.
Conclusion
In this blog, we have understood what 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
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!