It's a common pattern to write Jenkins pipeline code like this:
def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
}
I'm not sure how to word this simply, but this closure get implicitly imported and can be called by the file name of the .groovy
file it lives in.
I call it like:
MyClosure { myarg = 'sdfsdf' }
I not entirely sure what this is doing. It's doing call(body)
and then assigning body
as the delegate. so that means the closure I pass it is the delegate, but isn't that just the owner? Wah? me confused.
The Groovy documentation explains that, “A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable. A closure may reference variables declared in its surrounding scope.”
Creating a Jenkinsfile , which is checked into source control, provides a number of immediate benefits: Code review/iteration on the Pipeline. Audit trail for the Pipeline. Single source of truth for the Pipeline, which can be viewed and edited by multiple members of the project.
A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. Consider the following Pipeline which implements a basic three-stage continuous delivery pipeline.
Default place where Jenkins is looking for a Jenkinsfile is the root of the project with Jenkinsfile as the filename. Jenkinsfile can be put in any directory and the filename can be whatever you want.
When this runs, it is creating an empty map (config
). Then it is telling the closure (body
) to look at the delegate first to find properties by setting its resolveStrategy to be the constant Closure.DELEGATE_FIRST
. Then it assigns the config
map as the delegate for the body object.
Now when you execute the body()
closure, the variables are scoped to the config
map, so now config.myarg = 'sdfsdf'
.
Now later in the code you can have easy access to the map of values in config
.
body
is the owner, and by default is the delegate. But when you switch the delegate to be config
, and tell it to use the delegate first, you get the variables config
's scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With