简体   繁体   中英

Running groovy script at the end of Jenkins Job


I need to run a groovy Post-Build script to do some clean up so I have arranged the following script:

def sout = new StringBuilder(), serr = new StringBuilder()
def proc = '/usr/bin/docker stop mysql'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)

Although the command '/usr/bin/docker stop mysql' works if I log on the machine, when the groovy script is executed, the following error is raised:

java.io.IOException: Cannot run program "/usr/bin/docker": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at java.lang.Runtime.exec(Runtime.java:620)
    at java.lang.Runtime.exec(Runtime.java:450)
    at java.lang.Runtime.exec(Runtime.java:347)
    at org.codehaus.groovy.runtime.ProcessGroovyMethods.execute(ProcessGroovyMethods.java:533)
    at org.codehaus.groovy.runtime.dgm$894.invoke(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:104)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
    at org.kohsuke.groovy.sandbox.impl.Checker$checkedCall$0.callStatic(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
    at Script1.run(Script1.groovy:2)

Do you know why the groovy engine fails to find the 'docker' command? Thanks!

The Groovy Postbuild plugin actually runs on the master , not on the build node. The documentation states:

This plugin executes a groovy script in the Jenkins JVM

which is presumably meant to convey "and not in an agent JVM". (Fun side note: If you use println in the Groovy script, it prints out to the Jenkins master log, not to the job's console log)

The Post build task plugin does run on the build node (and you don't need to wrap your shell command in a groovy script). The task requires a condition: to "always run" your script just leave the Log text blank.

You might also consider taking a look at the Jenkins Pipeline , which lets you bake in try/catch type logic and maintain your pipeline "as code" using a Jenkinsfile .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM