How to access pipeline DSL in groovy classes(and not in Jenkinsfile)?
Clash Royale CLAN TAG#URR8PPP
How to access pipeline DSL in groovy classes(and not in Jenkinsfile)?
I want to access pipeline steps like 'bat' or 'echo' in my groovy classes but I am not really aware of how the 'script' variable usage works. This is very small repository with only this class and I am calling this class in groovy test(spock test). Can somebody please enlighten me on correct usage of 'script' variable and how the call should look like ?
SoftwareInstallation.groovy
class SoftwareInstallation implements Serializable
Script script
def runInstallationJar(repoDir)
def exitValues=
def configFiles=['software1.xml', 'software2.xml']
configFiles.each
def status = script.bat returnStatus: true, script: " java -jar $repoDir\resources\software.jar --inputxml=$repoDir\resources\$it"
script.echo "Return status : $status"
if (status)
log 'success'
else
log 'failure'
exitValues.add(status)
return exitValues
ps- I do have a Jenkinsfile but that just contains a gradle call to run tests.
1 Answer
1
There is an example in the article about Extending with Shared Libraries.
There you'll find following code:
package org.foo
class Utilities implements Serializable
def steps
Utilities(steps) this.steps = steps
def mvn(args)
steps.sh "$steps.tool 'Maven'/bin/mvn -o $args"
@Library('utils') import org.foo.Utilities
def utils = new Utilities(this)
node
utils.mvn 'clean package'
It doesn't really matter whether you defined the class inside of a library or in your script. The approach would be the same:
class Utilities implements Serializable
def steps
Utilities(steps) this.steps = steps
def mvn(args)
steps.sh "$steps.tool 'Maven'/bin/mvn -o $args"
def utils = new Utilities(this)
node
utils.mvn 'clean package'
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.