Search code examples
powershelljenkins-job-dsl

Multiple line powershell command in JobDSL


I'm tinkering with JobDSL and can't seem to find a way to run several powershell commands in one go. Example:

job('whatever'){
 steps{
  powershell("""$var = $env:mybuildvar
   cmdlet2 $var""")
 }
}

How do I achieve this? Thanks!

it seems that """ """ works for batch, but not for powershell. Also, if I try to use $var with escaping or without JobDSL fails with

ERROR: (sandbox_CI_Dev, line 15) No signature of method: javaposse.jobdsl.dsl.helpers.step.StepContext.powershell() is applicable for argument types: (java.lang.String) values: [$var = $ENV:mybuildvar]

The error is reproducable on jobdsl playground (http://job-dsl.herokuapp.com/), use following code (or anything similar to code above):

job('whatever') {
    steps{
        powershell("write-output $")
    }
}

also powershell('write-output test; write-output test') doesn't work


Solution

  • The method name is powerShell, not powershell. See https://jenkinsci.github.io/job-dsl-plugin/#path/job-steps-powerShell.

    And Groovy interpolates double quoted strings, see String interpolation. You need to use single quotes to avoid the interpolation if you want to use the dollar sign ($), e.g. '$var'. Use triple single quotes for multiline strings.

    job('whatever'){
      steps{
        powerShell('''$var = $env:mybuildvar
    cmdlet2 $var''')
      }
    }