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
-
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.
-
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.
- The
-
Fluent Interface:
- The
body.delegate = config
andbody.resolveStrategy = Closure.DELEGATE_FIRST
lines allow the closure passed toSampleDSL
to interact with theconfig
map directly, making the configuration more fluent and readable.
- The
-
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
-
DSL Definition:
- The
call
method is defined in thesampleDSL.groovy
script. It takes a closure (body
) as an argument. - Inside the
call
method, an empty mapconfig
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.
- The
-
Pipeline Stages:
- The
node
block defines the Jenkins pipeline stages. - Each stage (
input demo
,hardware info
,config info
, and3rd stage
) performs specific tasks, with some of them using values from theconfig
map.
- The
-
Jenkinsfile:
- The Jenkinsfile calls the
SampleDSL
method, passing a closure that sets up configuration parameters (HW
,P1
, andCustom
). - These parameters are added to the
config
map inside thecall
method and are used in various stages of the pipeline.
- The Jenkinsfile calls the
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.