我在postbuild-step中使用的脚本如下,我试图从文件中提取MAVEN_VERSION并且它没有在远程slave上找到该文件,但是当我在本地slave上执行相同的工作时同样正常,
所以我认为插件需要更新才能将VirtualChannel打开到远程工作区来解析文件,我还没看过代码,但是如果有人有快速解决方法,可以帮助解决这个问题。
环境详情
运行Jenkins 1.625.3.1
groovy-postbuild-plugin - 2.2.2
JDK1.8.0_72
Tomcat_8.0.30
import jenkins.util.*;
import jenkins.model.*;
def thr = Thread.currentThread();
def currentBuild = thr?.executable;
def workspace = currentBuild.getModuleRoot().absolutize().toString();
def project = new XmlSlurper().parse(new File("$workspace/pom.xml"))
def param = new hudson.model.StringParameterValue("MAVEN_VERSION", project.version.toString())
currentBuild.addAction(new hudson.model.ParametersAction(param));
日志错误 -
ERROR: Failed to evaluate groovy script.
java.io.FileNotFoundException: /prod/jenkins/test-slave/workspace/MYProjectBuild/pom.xml (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at groovy.util.XmlSlurper.parse(XmlSlurper.java:160)
at groovy.util.XmlSlurper$parse.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at Script1.run(Script1.groovy:6)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:166)
at org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.perform(GroovyPostbuildRecorder.java:362)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:785)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:726)
at hudson.model.Build$BuildExecution.post2(Build.java:185)
at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:671)
at hudson.model.Run.execute(Run.java:1766)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:408)
答案 0 :(得分:2)
我能够通过调整代码来解决这个问题,感谢@izzekil建议hudson.FilePath能够弄明白。希望这有助于任何有类似需求的人。
import jenkins.util.*;
import jenkins.model.*;
def thr = Thread.currentThread();
def currentBuild = thr?.executable;
if(manager.build.workspace.isRemote())
{
channel = manager.build.workspace.channel;
def fp = new hudson.FilePath(channel, manager.build.workspace.toString() + "/pom.xml");
def project = new XmlSlurper().parseText(fp.readToString());
def param = new hudson.model.StringParameterValue("MAVEN_VERSION", project.version.toString());
currentBuild.addAction(new hudson.model.ParametersAction(param));
}
else
{
def workspace = currentBuild.getModuleRoot().absolutize().toString();
def project = new XmlSlurper().parse(new File("$workspace/pom.xml"))
def param = new hudson.model.StringParameterValue("MAVEN_VERSION", project.version.toString())
currentBuild.addAction(new hudson.model.ParametersAction(param));
}