0

I need to validate if a file exist in jenkins workspace after HTML publish. So this would be a post-build groovy script execution:

Tried the following:

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."

def exists = fileExists 'file'

if (exists) {
    echo 'Yes'
} else {
    echo 'No'
}
println new File('var/jenkins/workspace/kae.html').exists()

Nothing works, gives below error:

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
user312307
  • 153
  • 6
  • 21
  • The error does not correspond to your code. I don't see any `WORKSPACE` in your code. – daggett Oct 10 '19 at 20:13
  • Thanks for pointing that out, I tried multiple things, so the output was from another trial. Pasted the correct one now. – user312307 Oct 10 '19 at 20:52
  • `testFile.exists()` should work – daggett Oct 11 '19 at 09:47
  • the last case also should work. if you have error - provide it for those cases. error could not be the same. – daggett Oct 11 '19 at 09:49
  • With last case, I do not get any output about file existence – user312307 Oct 11 '19 at 13:38
  • but the code is fine. question where `println` outputs the result in your environment. you could change third case to `assert new File('var/jenkins/workspace/kae.html').exists()` so in case of file absence it will throw exception. – daggett Oct 11 '19 at 13:44

2 Answers2

1

I was able to make this work but it prints "File not exist" even if it exists.

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.")

user312307
  • 153
  • 6
  • 21
0

The short answer is you cannot do it easily.

Functions such as readFile, writeFile, fileExists and so on are available only inside a pipeline because they are not parts of standard Groovy modules.

The Groovy Postbuild plugin allows you to write Groovy scripts in other Jenkins job types but you are forced to use only standard Groovy tooling plus some extra objects mentioned in the plugin's documentation at https://plugins.jenkins.io/groovy-postbuild/. See sections whitelisted and non-whitelisted methods.

Unfortunately, the usage of File class is not working either. The first thing is, that the current working directory is not set to the Jenkins workspace. But code like this

File file = new File(manager.getEnvVariable("WORKSPACE") + "/test")
file.exists()

is not working too because it seems like there is some permission issue. This you can check when you try to write something to the file. Then java.io.FileNotFoundException: test (Permission denied) is returned.

The best option is to rewrite your job into a pipeline where the pre-prepared functions are present and all Groovy Postbuild objects are available too.