Main differences between Freestyle - Scripted Pipeline Job - Declarative Pipeline Job

Article ID:115003908372
2 minute readKnowledge base

Issue

  • We show how to migrate a freestyle job to a scripted pipeline and a scripted pipeline to a declarative pipeline.

Environment

Resolution

Let’s see the same job in three different scenarios:

  • Freestyle Job.

  • Scripted Pipeline Job.

  • Declarative Pipeline Job.

From that point, we’ll explain the changes needed to move from one scenario to another and how this works in Jenkins in order to understand what changes you need.

FreestyleJob

You need two jobs.

1. You need to set up the label for the agent with "SlaveSCMConnected" and configure your repository and add an "Execute shell" build with:

* mvn clean install

2. You need to set up the label for the agent with "SlaveWithoutConnectionToSCM" and configure an email post-build step with the corresponding configuration.

Scripted Pipeline Job

node('SlaveSCMConnected') {
    stage('SCM operation'){
        checkout scm
        sh 'mvn clean install'
   }
}

node('SlaveWithoutConnectionToSCM')
    stage('Operation without SCM'){
        mail (to: 'devops@acme.com',
             subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) is waiting for input",
             body: "Please go to ${env.BUILD_URL}.");
   }
}

Declarative Pipeline JOB

pipeline {
  agent none
  options {
      skipDefaultCheckout()
  }
  stages{
    stage('SCM operation'){
      agent {label 'SlaveSCMConnected'}
      steps{
        checkout scm
        sh 'mvn clean install'
      }
   }
    stage('Operation without SCM'){
      agent {label 'SlaveWithoutConnectionToSCM'}
      steps{
        mail (to: 'devops@acme.com',
             subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) is waiting for input",
            body: "Please go to ${env.BUILD_URL}.");
      }
   }
  }
}

General Job vs Scripted Pipeline Job

Main difference

The main difference between any job and a Pipeline Job is that the Pipeline Scripted job runs on the Jenkins controller, using a lightweight executor expected to use very few resources in order to translate the pipeline to atomic commands that execute or send to the agents.

In our example, in the Freestyle job everything is executed in the agent, but for the Scripted Pipeline Job, the pipeline code is translated in the controller to atomic commands that are sent to the agents.

References

Scripted Pipeline Job vs Declarative Pipeline

Main difference

There are several differences:

  • You need to use a Jenkinsfile to define it that have to be located in an SCM.

  • By default each step is going to do a "checkout scm" on each "node".

In our example, if the controller doesn’t have access to the SCM it couldn’t work because the first step is that the controller get the Jenkinsfile from the repository and translate the steps in atomic commands.

One of our nodes doesn’t have access to the SCM so by default it’ll fail because each "node" do a "checkout scm" perhaps it doesn’t need anything from there.

To avoid that you need to add this piece of code:

  options {
      skipDefaultCheckout()
  }

Common Problems

  • Like the code is in an SCM and the controller is the one that translates it, the controller needs to have access to the SCM.

  • If you don’t set the "skipDefaultCheckout()" option, all your agents need to have access to the SCM.

References