what is the key difference between Declarative pipeline and scripted pipeliine

+2 votes
Aug 12, 2019 in Jenkins by Rajendra
• 510 points
123,193 views
I have created 5 scripted pipeline jobs in jenkins, is there any way I can trigger all the pipeline jobs in sequence one after the other.  Eg:

Pipeline job 1->Pipeline job 2->Pipeline job 3->Pipeline job 4->Pipeline job 5

Hey Kavya,

You can look into this https://www.edureka.co/community/8876/how-to-trigger-jenkins-pipeline-in-another-jenkins-pipeline?show=8876#q8876. Your query is quite similar to this and I hope it will be helpful for you.

2 answers to this question.

+6 votes
Best answer
  • The key difference between Declarative pipeline and Scripted pipeline would be with respect to their syntaxes and their flexibility

  • Declarative pipeline is a relatively new feature that supports the pipeline as code concept. It makes the pipeline code easier to read and write. This code is written in a Jenkinsfile which can be checked into a source control management system such as Git.

  • Whereas, the scripted pipeline is a traditional way of writing the code. In this pipeline, the Jenkinsfile is written on the Jenkins UI instance. 

  • Though both these pipelines are based on the groovy DSL, the scripted pipeline uses stricter groovy based syntaxes because it was the first pipeline to be built on the groovy foundation. Since this Groovy script was not typically desirable to all the users, the declarative pipeline was introduced to offer a simpler and more optioned Groovy syntax.

  • The declarative pipeline is defined within a block labelled ‘pipeline’ whereas the scripted pipeline is defined within a ‘node’. 

Structure and syntax of the Declarative pipeline:

The Agent is where the whole pipeline runs. Example, Docker. The Agent has following parameters:

  • any – Which mean the whole pipeline will run on any available agent.

  • none – Which mean all the stages under the block will have to declared with agent separately.

  • label –  this is just a label for the Jenkins environment

  • docker –  this is to run the pipeline in Docker environment.

The Declarative pipeline code will looks like this:

pipeline {
  agent { label 'node-1' }
  stages {
    stage('Source') {
      steps {
        git 'https://github.com/digitalvarys/jenkins-tutorials.git''
      }
    }
    stage('Compile') {
      tools {
        gradle 'gradle4'
      }
      steps {
        sh 'gradle clean compileJava test'
      }
    }
  }
}

Scripted Pipeline:

  • The scripted pipeline is a traditional way of writing the Jenkins pipeline as code. Ideally, Scripted pipeline is written in Jenkins file on web UI of Jenkins.

  •  Unlike Declarative pipeline, the scripted pipeline strictly uses groovy based syntax. Since this, The scripted pipeline provides huge control over the script and can manipulate the flow of script extensively. 

  • This helps developers to develop advance and complex pipeline as code.

Structure and syntax of the scripted pipeline:

Node Block:

Node is the part of the Jenkins architecture where Node or agent node will run the part of the workload of the jobs and master node will handle the configuration of the job. So this will be defined in the first place as

node {
}

Stage Block:

Stage block can be a single stage or multiple as the task goes. And it may have common stages like

  • Cloning the code from SCM

  • Building the project

  • Running the Unit Test cases

  • Deploying the code

  • Other functional and performance tests.

So the stages can be written as mentioned below:

stage {
}

So, Together, the scripted pipeline can be written as mentioned below.

node ('node-1') {
  stage('Source') {
    git 'https://github.com/digitalvarys/jenkins-tutorials.git''
  }
  stage('Compile') {
    def gradle_home = tool 'gradle4'
    sh "'${gradle_home}/bin/gradle' clean compileJava test"
  }
}
  • Declarative Pipeline encourages a declarative programming model, whereas Scripted Pipelines follow a more imperative programming model

  • Declarative type imposes limitations to the user with a more strict and pre-defined structure, which would be ideal  for simpler continuous delivery pipelines. 

  • Scripted type has very few limitations that to with respect to structure and syntax that tend to be defined by Groovy,thus making it ideal for users with more complex requirements. 

For more info you could refer to this blog: https://www.edureka.co/blog/jenkins-pipeline-tutorial-continuous-delivery

answered Aug 13, 2019 by Sirajul
• 59,230 points

selected Dec 10, 2020 by MD
+1 vote

Hi,

The Declarative pipeline is a new feature that is added to create the pipeline. This is basically written in a Jenkinsfile which can be stored into a source code management system such as Git. Declarative pipelines is an ideal solution for the simple continuous delivery pipeline as it has very limited and pre-defined structure.

The scripted pipeline is a traditional way of writing the Jenkins pipeline as code. Ideally, a Scripted pipeline is written in the Jenkins file on the web UI of Jenkins. Unlike the Declarative pipeline, the scripted pipeline strictly uses groovy based syntax. Since this, The scripted pipeline provides huge control over the script and can manipulate the flow of the script extensively. This helps developers to develop advance and complex pipelines as code.

answered Dec 10, 2020 by MD
• 95,460 points
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP