0

I want to customize the powerShell() method in jobDSL

a .groovy file with some job dsl defined jobs

def betterPowerShell(def cmd) {
    powerShell("$cmd; exit \$LastExitCode")
}

Since I am out of the

job {
  step {
  }
}

context this is not working...

Maybe I need something like...

def betterPowerShell(def cmd) {
    javaposse.jobdsl.dsl.helpers.step.StepContext.powerShell("$cmd; exit \$LastExitCode")
}
David West
  • 1,463
  • 3
  • 16
  • 25

1 Answers1

1

What you want is a Jenkins shared library which will allow you to (among other things) define custom Pipeline DSL steps in a repository you own. See the "Defining custom steps" section in that document.

Here's the example custom step from that document:

// vars/sayHello.groovy
def call(String name = 'human') {
    // Any valid steps can be called from this code, just like in other
    // Scripted Pipeline
    echo "Hello, ${name}."
}
jayhendren
  • 2,952
  • 7
  • 15
  • I wanted to call powerShell() from the powershell jobDSL plugin. Will that work in this context? – David West May 15 '20 at 16:03
  • Yes, you can call other Pipeline steps from your custom Pipeline step. In fact most of the custom steps that I have written using this method call other Pipeline steps. – jayhendren May 15 '20 at 20:41
  • how do you load the shared library in a Job DSL seed then? https://devops.stackexchange.com/questions/11833/how-do-i-load-a-jenkins-shared-library-in-a-jenkins-job-dsl-seed – David West Jun 16 '20 at 18:17