In the code below, all files and folders from the temp directory are recursively deleted upon exit.
mydir = Files.createTempDirectory() addShutdownHook { mydir.deleteDir() }
This code works for normal Groovy execution, but it does not work based on Jenkins Grooby, because:
an exception which occurred: in field delegate in field closures in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@782f5796 Caused: java.io.NotSerializableException: sun.nio.fs.UnixPath at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
So, I am still working on the “CPS” aspect, the first attempts failed with the same errors:
@NonCPS def mkdtemp(String s) { mydir = Files.createTempDirectory("cp-") addShutdownHook { mydir.deleteDir() println "cleaned" } mydir.toString() } node { mkdtemp('xxx') }
sorin source share