Jenkins Scripted Vs Declarative Pipeline

Last Updated : 30 May, 2026

Jenkins provides two types of pipelines: Scripted Pipeline and Declarative Pipeline. Both are used to automate build, test, and deployment processes in CI/CD.

  • Scripted Pipeline is flexible and powerful.
  • Declarative Pipeline is simple and easy to manage.
  • Both help automate Jenkins workflows.

Scripted Pipeline

The Scripted Pipeline is the original pipeline syntax in Jenkins, and it is based on the Groovy scripting language. It provides flexibility and control over the pipeline process. It requires a better understanding of Groovy scripting for the implementation of the complex code.

groovy
node {
    stage('Build Step') {
        // Build the application
        sh 'mvn clean install'
    }
    stage('Test Step') {
        // Run tests
        sh 'mvn test'
    }
    stage('Deploy Step') {
        // Deploy the application
        sh 'deploy.sh'
    }
}

Working:

  • Build Step: Runs the build process using Maven.
  • Test Step: Executes unit tests to verify the code.
  • Deploy Step: Deploys the application

Use Case:

  • If your pipeline requires custom logic, parallel execution, or complex error handling
  • When your team is comfortable writing Groovy scripts
  • If you need greater flexibility and control over execution

Declarative Pipeline

The Declarative Pipeline is a more recent addition to Jenkins and provides a more structured and simpler syntax compared to the Scripted Pipeline. The syntax uses a Domain-Specific Language (DSL) based on Groovy, designed to be intuitive and more readable. Declarative Pipelines are defined inside a pipeline block and are less verbose than Scripted Pipelines.

groovy
pipeline {
    agent any
    stages {
        stage('Build Step') {
            steps {
                // Build the application
                sh 'mvn clean install'
            }
        }
        stage('Test Step') {
            steps {
                // Run tests
                sh 'mvn test'
            }
        }
        stage('Deploy Step') {
            steps {
                // Deploy the application
                sh 'deploy.sh'
            }
        }
    }
}

Use Cases:

  • If you want a simpler, more readable pipeline.
  • For standard CI/CD workflows.
  • When you need quick setup with minimal coding.

Jenkins Scripted vs Declarative Pipeline

FeatureScripted PipelineDeclarative Pipeline
DefinitionWritten using Groovy scripting with full programming controlWritten using a predefined structured syntax
Syntax StyleFlexible and code-basedSimple and structured
ComplexityMore complexEasier to learn and maintain
FlexibilityHighly flexible and customizableLimited flexibility compared to Scripted
ReadabilityLess readable for beginnersMore readable and user-friendly
Error HandlingManual handling requiredBuilt-in post conditions and validation
Pipeline StructureNo fixed structureFollows a fixed pipeline structure
Recommended ForAdvanced users and complex workflowsBeginners and standard CI/CD pipelines
MaintenanceHarder to maintainEasier to maintain
Example Startnode { }pipeline { }

Best Practices for Jenkins Pipelines

A well-structured Jenkins pipeline improves CI/CD efficiency, scalability, and security. Below are key best practices to follow:

  • Divide pipelines into clear stages such as Build, Test, and Deploy to improve readability and maintenance.
  • Use parallel execution to run multiple tests or deployments simultaneously, reducing overall pipeline execution time.
  • Automate testing and builds by triggering pipelines on code commits, pull requests, or scheduled jobs.
  • Integrate reporting and quality tools like JUnit, TestNG, Allure Reports, and SonarQube for better visibility and code quality checks.
  • Ensure security and optimize performance by storing secrets securely, implementing RBAC, using Jenkins agents, and regularly cleaning old builds and artifacts.

Real-World Example: Hybrid Pipeline for Advanced Workflows

You can combine the strengths of both Declarative and Scripted pipelines by using a Declarative pipeline as the base and adding Scripted sections for complex logic.

Example: Hybrid Pipeline

groovy
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
            }
        }
        stage('Test') {
            steps {
                script {
                    try {
                        echo 'Running tests...'
                        // Custom test logic
                    } catch (Exception e) {
                        echo "Test failed: ${e.getMessage()}"
                    }
                }
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying the application...'
            }
        }
    }
}

Create a pipeline job in the Jenkins

Example: 1

1. Create a project in the Jenkins which is follows:

Create a project in the Jenkins

2. And in Advance project section select and add the sample project script which is :

add the sample project script

3. Click on the Build now button. open the console which is show the progress:

Screenshot-2024-12-20-113835

It will give the Stage view for the following sections:

Stage view

Add the another stages into the same code and check the stage view:

ScreenshotAdd the another stages into the same code-2024-12-20-115503

Example: 2

Lets create a another job names pipelineJob2 and use the sample code which is named as Git+ Maven:

Screenshot-2024-12-20-115808

Save the Structure and click on the Build Now option for the same as we do last time and View the stage View

View the stage View
Comment

Explore