Jenkins Scripted Pipeline Example

Jenkins, a popular open-source automation server, offers a powerful feature called Scripted Pipelines, enabling you to define your software delivery process in a simple, human-readable Groovy script. This approach provides flexibility and control, making it an excellent choice for continuous integration and delivery (CI/CD) pipelines. Let's explore an example of a Jenkins scripted pipeline to illustrate its capabilities.

Jenkins CI/CD Pipeline - SonarQube, Docker, Github Webhooks on AWS | Resume Project | English
Jenkins CI/CD Pipeline - SonarQube, Docker, Github Webhooks on AWS | Resume Project | English

Before diving into the example, ensure you have Jenkins installed and a basic understanding of Groovy, the programming language used in scripted pipelines. Also, familiarize yourself with Jenkinsfile, the file that contains the pipeline script.

Building a Continuous Delivery Pipeline Using Jenkins
Building a Continuous Delivery Pipeline Using Jenkins

Defining the Pipeline Structure

The first step in creating a scripted pipeline is defining its structure. In this example, we'll create a simple pipeline with the following stages: checkout, build, test, and deploy. Each stage will be defined using Groovy syntax within the Jenkinsfile.

How to Use the Jenkins Scripted Pipeline | Perforce BlazeMeter
How to Use the Jenkins Scripted Pipeline | Perforce BlazeMeter

Here's a basic structure of the Jenkinsfile for our example:

```groovy pipeline { agent any stages { stage('Checkout') { steps { // Checkout code from SCM } } stage('Build') { steps { // Build the application } } stage('Test') { steps { // Run tests on the application } } stage('Deploy') { steps { // Deploy the application to the target environment } } } } ```

Checkout Stage

Jenkins Development & CI/CD Automation Services in USA
Jenkins Development & CI/CD Automation Services in USA

The checkout stage is responsible for obtaining the latest code from the source code management (SCM) system. In this example, we'll use Git to clone the repository.

Update the Jenkinsfile with the following code to implement the checkout stage:

```groovy pipeline { agent any stages { stage('Checkout') { steps { git 'https://github.com/your-username/your-repo.git' } } // ... other stages } } ```

Build Stage

Devops: Jenkins Pipeline As Code: All you need to know A - Z
Devops: Jenkins Pipeline As Code: All you need to know A - Z

The build stage compiles the source code and prepares the application for testing and deployment. For Java applications, you can use the `sh` step to execute shell commands and run Maven or Gradle to build the project.

Update the Jenkinsfile with the following code to implement the build stage:

```groovy pipeline { agent any stages { // ... checkout stage stage('Build') { steps { sh 'mvn clean package' // For Maven projects // or sh './gradlew clean build' // For Gradle projects } } // ... other stages } } ```

Implementing Parallel Stages

the event - driven devops pipeline is shown in this diagram, it shows how to use
the event - driven devops pipeline is shown in this diagram, it shows how to use

Scripted pipelines also support parallel execution of stages, allowing you to optimize your CI/CD process. In this example, we'll parallelize the test stage to run multiple tests simultaneously.

Update the Jenkinsfile with the following code to implement parallel tests:

Complete LLM Pipeline
Complete LLM Pipeline
a poster with some information about the properties of complex sine sizing key concepts and formulas
a poster with some information about the properties of complex sine sizing key concepts and formulas
Pipelines
Pipelines
an old typewriter that has been written in black and white
an old typewriter that has been written in black and white
a table that has some words on it and numbers in the top right hand corner
a table that has some words on it and numbers in the top right hand corner
50 GPT-5.5 Prompts for DevOps Engineers: CI/CD Pipelines, Infrastructure as Code, Monitoring, and...
50 GPT-5.5 Prompts for DevOps Engineers: CI/CD Pipelines, Infrastructure as Code, Monitoring, and...
PIPELINE RULES
PIPELINE RULES
Sales Pipeline CRM Notion Template Lead Deal Tracker Revenue Forecast Manager Gift Digital Download
Sales Pipeline CRM Notion Template Lead Deal Tracker Revenue Forecast Manager Gift Digital Download
an image of a website page with different pipes and piping lines on the bottom
an image of a website page with different pipes and piping lines on the bottom
the logo for wine'em pipeline'em dine'em, which is featured on
the logo for wine'em pipeline'em dine'em, which is featured on
someone is holding a pen and writing on a piece of paper that has been placed in front of them
someone is holding a pen and writing on a piece of paper that has been placed in front of them
an image of a woman in white dress on the screen with words above her head
an image of a woman in white dress on the screen with words above her head
Behind The Glass, Touch Up, Acting
Behind The Glass, Touch Up, Acting
the back side of a cell phone with text on it
the back side of a cell phone with text on it
Scripting
Scripting
a block diagram showing the stages of testing
a block diagram showing the stages of testing
an image of someone doing tricks on their cell phone
an image of someone doing tricks on their cell phone
the language of piping engineers is shown in this handwritten diagram, with instructions on how to use piping machines
the language of piping engineers is shown in this handwritten diagram, with instructions on how to use piping machines
Sales Pipeline Excel Template
Sales Pipeline Excel Template
a close up of a piece of paper with writing on it
a close up of a piece of paper with writing on it

```groovy pipeline { agent any stages { // ... previous stages stage('Test') { parallel { stage('Unit Tests') { steps { sh 'mvn test -Dtest=UnitTests' // Run unit tests } } stage('Integration Tests') { steps { sh 'mvn test -Dtest=IntegrationTests' // Run integration tests } } } } // ... deploy stage } } ```

Test Stage

The test stage runs various tests on the application to ensure it meets the required quality standards. In the updated Jenkinsfile, we've parallelized this stage to run unit and integration tests simultaneously.

No additional changes are needed for the test stage, as it's already defined in the previous section.

Deploy Stage

The deploy stage is responsible for pushing the built application to the target environment. For this example, we'll use the `sh` step to execute shell commands and deploy the application using a simple script.

Update the Jenkinsfile with the following code to implement the deploy stage:

```groovy pipeline { agent any stages { // ... previous stages stage('Deploy') { steps { sh './deploy.sh' // Deploy script } } } } ```

With this example, you now have a solid foundation for creating scripted pipelines in Jenkins. You can further customize and extend this pipeline by adding more stages, implementing post-build actions, or integrating with external tools and services. Happy automating!