Ryan Kennedy
Ryan Kennedy

Reputation: 3615

Two nested Groovy closures -- looking for cleaner syntax

I'm unfamiliar with Groovy syntax, but spent a bit of time researching this. I'm working with a Jenkinsfile, and I have a section that looks like this:

configFileProvider([/* ... */]) {
  withCredentials([/* ... */]) {
    sh 'my command'
  }
}

Does Groovy syntax enable a shorter expression of this same logic? I'm not a fan of the indentation here.

Upvotes: 0

Views: 366

Answers (1)

emilles
emilles

Reputation: 1179

You can assign any closure to a variable and pass it. So you could refactor to:

def shCommand = { withCredentials([...]) {
  sh 'my command'
}}
configFileProvider([...], shCommand)

or

def shCommand = {
  sh 'my command'
}
configFileProvider([...]) {
  withCredentials([...], shCommand)
}

Upvotes: 1

Related Questions