20

Whenever a build of my Jenkins job finishes, I'm sending an email with the results using the Editable Email Notification plugin (a.k.a. Email-ext plugin). Also, if a certain file is present in the system, I'm modifying the default content of the message to notify of the existence of that file.

For that end, I'm using the Pre-send Script field of the plugin.

Test job configuration:

Prepare an environment for the run

Properties Content

LOG="log.txt"

Build stage

Execute shell

#!/bin/bash

touch ${WORKSPACE}/${LOG}
echo "this is just a log" >> ${WORKSPACE}/${LOG}

Post-build Actions

Editable Email Notification

Pre-send Script

File log = new File("${WORKSPACE}/${LOG}");
logger.println(log.text);

When executing the build, Jenkins will create the file in the ${WORKSPACE} and fill it (I'm able to print the contents from the terminal in the Jenkins slave).

But when trying to access it from the Email plugin, the exception java.io.FileNotFoundException is raised:

java.io.FileNotFoundException: /home/jenkins/workspace/testJob/log.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at groovy.util.CharsetToolkit.<init>(CharsetToolkit.java:69)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.newReader(DefaultGroovyMethods.java:16958)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.getText(DefaultGroovyMethods.java:16006)
    at org.codehaus.groovy.runtime.dgm$381.doMethodInvoke(Unknown Source)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.doMethodInvoke(GeneratedMetaMethod.java:70)
    at groovy.lang.MetaClassImpl$GetBeanMethodMetaProperty.getProperty(MetaClassImpl.java:3500)
    at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:61)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227)
    at Script1.run(Script1.groovy:59)
    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 hudson.plugins.emailext.ExtendedEmailPublisher.executePresendScript(ExtendedEmailPublisher.java:450)
    at hudson.plugins.emailext.ExtendedEmailPublisher.sendMail(ExtendedEmailPublisher.java:311)
    at hudson.plugins.emailext.ExtendedEmailPublisher._perform(ExtendedEmailPublisher.java:297)
    at hudson.plugins.emailext.ExtendedEmailPublisher.perform(ExtendedEmailPublisher.java:244)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:723)
    at hudson.model.Build$BuildExecution.cleanUp(Build.java:195)
    at hudson.model.Run.execute(Run.java:1785)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:410)

I'm at a loss as why Groovy is complaining about the missing file, whereas if I instead print the path with

logger.println(log.getPath());

it will print it successfully.

NOTE: The logger variable in the Pre-send Script is provided by the notification plugin to access directly the build log.

Adri C.S.
  • 2,909
  • 5
  • 36
  • 63
  • Is it possible that the post-build action gets executed on the jenkins master rather than on the slave? The logger object might be shared across the nodes, which is why the `log.getPath()` returns a path, but the file itself might not be present on the master node. – fishi0x01 Jan 19 '17 at 10:47
  • @fishi Indeed, it seems that the post-build action is executed in the Slave. I printed the `hostname` of the machine instead of creating files and it reported the Master's. The question remains as how could I be able to access that file from the Master... – Adri C.S. Feb 15 '17 at 13:12
  • One option would be to copy the file to the master. If you use Jenkins pipeline scripts you could use [stash/unstash](https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#stash-stash-some-files-to-be-used-later-in-the-build). But judging from the question it seems you are not using Jenkins pipeline. In that case you could have a look at [Copy to slave plugin](https://wiki.jenkins-ci.org/display/JENKINS/Copy+To+Slave+Plugin) (even though the name suggests otherwise, you can also copy from slave to master with it). – fishi0x01 Feb 16 '17 at 10:17
  • @fishi You are right. I'm not using the pipeline, but I will take a look into it, to see if it would be possible to modify the job to adopt it. Otherwise, I will go with the `Copy to slave` plugin approach. – Adri C.S. Feb 17 '17 at 10:55

3 Answers3

27

java.io.File methods will refer to files on the master where Jenkins is running, and not in the current workspace on the slave machine (in case this job indeed runs on a slave machine).

To refer to files on the slave machine, you should use the readFile method

def log = readFile("${WORKSPACE}/${LOG}");
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Pieter Meiresone
  • 1,910
  • 1
  • 22
  • 22
  • 3
    A side note: this will work if the groovy code is within the Pipeline. If you want to read a file on a slave from a shared library, it's not really possible, as described here - https://issues.jenkins-ci.org/browse/JENKINS-56384. – Jakub Kukul Oct 12 '19 at 16:51
15

Within a Jenkins pipeline, File log = new File("${WORKSPACE}/${LOG}"); doesn't work.
You can use readFile instead, e.g.:

def log = readFile "${WORKSPACE}/${LOG}"
Jakub Kukul
  • 12,032
  • 3
  • 54
  • 53
Joe M
  • 2,527
  • 1
  • 25
  • 25
0

I had a jenkins Job with the same problem. readFilemethod didn't work for me because I used a Job DSL (had an error like No signature of method readFile()), so I used the readFileFromWorkspace method and it worked for me.

Your code should be modified like:

def log = readFileFromWorkspace("${WORKSPACE}/${LOG}");
Marco Luly
  • 333
  • 4
  • 8