0
0
Jenkinsdevops~5 mins

Scripted vs declarative comparison in Jenkins - CLI Comparison

Choose your learning style9 modes available
Introduction
Jenkins pipelines automate software build and deployment. There are two ways to write these pipelines: scripted and declarative. Each style solves the problem of defining automation steps but in different ways.
When you want a simple, easy-to-read pipeline with clear stages and steps.
When you need more flexibility and custom logic in your pipeline code.
When you want to quickly create a pipeline using a structured format.
When you want to write complex scripts with loops and conditionals.
When you want to maintain pipelines that are easy for teams to understand.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the project'
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests'
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying application'
      }
    }
  }
}

// Scripted pipeline example
node {
  stage('Build') {
    echo 'Building the project'
  }
  stage('Test') {
    echo 'Running tests'
  }
  stage('Deploy') {
    echo 'Deploying application'
  }
}

The first part is a declarative pipeline that uses a clear, structured syntax with pipeline, agent, and stages. It defines three stages: Build, Test, and Deploy, each with simple steps.

The second part is a scripted pipeline using node and stage blocks with Groovy code. It performs the same steps but allows more flexible scripting.

Commands
Check Jenkins Job Builder version to ensure Jenkins CLI tools are installed before running pipelines.
Terminal
jenkins-jobs --version
Expected OutputExpected
1.0.0
Trigger the Jenkins pipeline job named 'example-pipeline' to run the pipeline defined in the Jenkinsfile.
Terminal
jenkins-cli build example-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the project [Pipeline] echo Running tests [Pipeline] echo Deploying application [Pipeline] End of Pipeline
Retrieve the current pipeline job configuration to verify the pipeline script is loaded correctly.
Terminal
jenkins-cli get-job example-pipeline
Expected OutputExpected
<flow-definition plugin="workflow-job@2.40"> <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.90"> <script>pipeline { agent any stages { stage('Build') { steps { echo 'Building the project' } } stage('Test') { steps { echo 'Running tests' } } stage('Deploy') { steps { echo 'Deploying application' } } } }</script> <sandbox>true</sandbox> </definition> </flow-definition>
Key Concept

If you remember nothing else from this pattern, remember: declarative pipelines are easier to read and maintain, while scripted pipelines offer more coding flexibility.

Common Mistakes
Mixing declarative syntax inside scripted pipeline blocks without proper structure.
This causes syntax errors because the two styles have different rules and cannot be mixed directly.
Use either fully declarative syntax or fully scripted syntax in a Jenkinsfile, not both mixed.
Omitting the 'pipeline' block in declarative pipelines.
The 'pipeline' block is required to define a declarative pipeline; without it, Jenkins cannot parse the script.
Always start declarative pipelines with the 'pipeline' block.
Using scripted pipeline without 'node' block.
The 'node' block defines where the pipeline runs; without it, the scripted pipeline will fail to execute.
Wrap scripted pipeline steps inside a 'node' block.
Summary
Declarative pipelines use a simple, structured syntax with 'pipeline' and 'stages' blocks.
Scripted pipelines use Groovy code inside 'node' blocks for more control and flexibility.
Use declarative pipelines for readability and ease of maintenance; use scripted pipelines for complex logic.