What is the basic difference between declarative and scripted pipelines?

329    Asked by DanielBAKER in Devops , Asked on Jan 18, 2024

 I was setting up a Jenkins-based pipeline, however, I was struck by a particular scenario where I was confused between using either a declarative or scripted pipeline. How can I decide which one should I use? 

Answered by Daniel Cameron

 In the context of DevOps, here are the differences given between declarative and scripted pipeline:-

Declarative pipelines

In the context of Jenkins-based pipelines the declarative pipelines are known as providing a more structured and concise way to define pipelines. Therefore, it is suitable for building straightforward blocks and deployment where there is a simplified syntax is required:

Pipeline {
    Agent any
    Stages {
        Stage(‘Build’) {
            Steps {
                // Build steps
            }
        }
        Stage(‘Test’) {
            Steps {
                // Test steps
            }
        }
        Stage(‘Deploy’) {
            Steps {
                // Deployment steps
            }
        }
    }
    Post {
        // Post-build actions
    }
}

Scripted pipeline

If your particular building process has more complex logical and conditional statements then you can use the scripted pipeline as it allows the use of groovy scripting which helps in providing greater and seamless flexibility and even control:

Node {
    // Define environment and variables
    Stage(‘Build’) {
        // Scripted build steps
    }
    Stage(‘Test’) {
        // Scripted test steps
    }
    Stage(‘Deploy’) {
        // Scripted deployment steps
    }
    // Post-build scripted actions
}


Your Answer

Interviews

Parent Categories