Groovy DSL in Jenkins - unix1998/technical_notes GitHub Wiki

the scenario can be considered a Groovy DSL (Domain-Specific Language) within the context of Jenkins. Let's break down how it fits the characteristics of a DSL:

Breakdown of the Script

The DSL Definition

DSL Implementation:

// vars/sampleDSL.groovy
def call(body) {
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    node {
        stage ('input demo') {
            properties([
                parameters([
                    choice(
                        choices: ["DEV", "UAT", "QA", "PPROD", "PROD"].join("\n"),
                        description: 'choice parameter for build',
                        name: 'my_CHOICE'
                    )
                ])
            ])
        }
        stage ('hardware info') {
            echo 'step1'
            echo '######my_CHOICE##########'
            echo my_CHOICE
            echo config.HW
            sh '''
            echo "GIT_PREVIOUS_SUCCESSFUL_COMMIT" : $IT_PREVIOUS_SUCCESSFUL_COMMIT
            echo "GIT_PREVIOUS_SUCCESSFUL_COMMIT" : $IT_PREVIOUS_SUCCESSFUL_COMMIT
            '''
        }
        stage ('config info') {
            echo 'stage 2'
            echo config.P1
            echo 'stage 3 , Feb '
        }
        stage ('3rd stage') {
            echo 'stage 3'
            echo config.Custom
        }
    }
}

Calling the DSL in Jenkinsfile:

@Library('jenkins-pipeline-shared-lib-sample') _
SampleDSL {
    HW = "Hello World! first line. "
    P1 = "This is Para1 value , test of 2020 "
    Custom = "this line can be anything "
}

Why It's a DSL

  1. Domain-Specific Language:

    • This script defines a set of domain-specific instructions that are particularly useful in the context of a Jenkins pipeline. It abstracts the details of the pipeline setup and provides a higher-level interface for configuring stages and parameters.
  2. Custom Syntax:

    • The SampleDSL method in the Jenkinsfile provides a way to define pipeline configuration using a custom, readable syntax.
    • The closure passed to SampleDSL uses a simplified and expressive syntax to set up configuration parameters.
  3. Fluent Interface:

    • The body.delegate = config and body.resolveStrategy = Closure.DELEGATE_FIRST lines allow the closure passed to SampleDSL to interact with the config map directly, making the configuration more fluent and readable.
  4. Encapsulation:

    • The DSL encapsulates the complexity of setting up Jenkins stages and properties within a simple method call, providing a cleaner interface for defining the pipeline.

How It Works

  1. DSL Definition:

    • The call method is defined in the sampleDSL.groovy script. It takes a closure (body) as an argument.
    • Inside the call method, an empty map config is created.
    • The closure's delegate is set to this map, allowing properties defined in the closure to be added to the map.
    • The closure is then executed, populating the config map with the parameters defined in the Jenkinsfile.
  2. Pipeline Stages:

    • The node block defines the Jenkins pipeline stages.
    • Each stage (input demo, hardware info, config info, and 3rd stage) performs specific tasks, with some of them using values from the config map.
  3. Jenkinsfile:

    • The Jenkinsfile calls the SampleDSL method, passing a closure that sets up configuration parameters (HW, P1, and Custom).
    • These parameters are added to the config map inside the call method and are used in various stages of the pipeline.

Conclusion

Yes, this is an example of a Groovy DSL within Jenkins. The DSL provides a higher-level, domain-specific abstraction for configuring Jenkins pipelines, making it easier to read, write, and maintain pipeline scripts. It leverages Groovy's dynamic capabilities to create a fluent and expressive syntax for pipeline configuration.