如何检查jenkins工作区中是否存在自由式作业的文件

如何检查jenkins工作区中是否存在自由式作业的文件,jenkins,groovy,post-build,Jenkins,Groovy,Post Build,我需要验证HTML发布后jenkins工作区中是否存在文件。这将是一个构建后groovy脚本执行: 尝试了以下操作: def fileName = "/temp/new_invoices.txt" def testFile = new File(fileName) if (!testFile.exists()) testRunner.fail("File $fileName does not exist.") else log.info "File $fileName exists."

我需要验证HTML发布后jenkins工作区中是否存在文件。这将是一个构建后groovy脚本执行:

尝试了以下操作:

def fileName = "/temp/new_invoices.txt"
def testFile = new File(fileName)
if (!testFile.exists()) testRunner.fail("File $fileName does not exist.")
  else log.info "File $fileName exists."

无效,给出以下错误:

ERROR: Failed to evaluate groovy script.
groovy.lang.MissingMethodException: No signature of method: Script1.fileExists() is applicable for argument types: (java.lang.String) values: [/target/package-summary.html]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at Script1.run(Script1.groovy:1)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:375)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:312)
    at org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.perform(GroovyPostbuildRecorder.java:380)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
    at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1073)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
    at hudson.model.Run.execute(Run.java:1835)
    at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Build step 'Groovy Postbuild' changed build result to UNSTABLE
Notifying upstream projects of job completion

我能够使这个工作,但它打印“文件不存在”,即使它存在

def fileName = "/var/abc.html"
def testFile = new File(fileName)
if (!testFile.exists()) manager.listener.logger.println("File $fileName does not exist.")
  else manager.listener.logger.println("File $fileName exists.")


该错误与您的代码不对应。我在您的代码中没有看到任何
工作区
。感谢您指出这一点,我尝试了多种方法,因此输出来自另一次尝试。现在粘贴了正确的。
testFile.exists()
应该有效最后一个案例也应该有效。如果您有错误,请为这些情况提供它。错误不可能是相同的。对于最后一种情况,我没有得到任何关于文件存在的输出
ERROR: Failed to evaluate groovy script.
groovy.lang.MissingMethodException: No signature of method: Script1.fileExists() is applicable for argument types: (java.lang.String) values: [/target/package-summary.html]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at Script1.run(Script1.groovy:1)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:375)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:312)
    at org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.perform(GroovyPostbuildRecorder.java:380)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
    at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1073)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
    at hudson.model.Run.execute(Run.java:1835)
    at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Build step 'Groovy Postbuild' changed build result to UNSTABLE
Notifying upstream projects of job completion
def fileName = "/var/abc.html"
def testFile = new File(fileName)
if (!testFile.exists()) manager.listener.logger.println("File $fileName does not exist.")
  else manager.listener.logger.println("File $fileName exists.")