每当我的Jenkins作业的构建完成时,我都会使用Editable Email Notification插件(也称为Email- ext插件)发送包含结果的电子邮件。另外,如果系统中存在某个文件,则我正在修改消息的默认内容,以通知该文件的存在。
Editable Email Notification
Email- ext
为此,我正在使用Pre-send Script插件的字段。
Pre-send Script
__
_
LOG="log.txt"
#!/bin/bash touch ${WORKSPACE}/${LOG} echo "this is just a log" >> ${WORKSPACE}/${LOG}
_ 预发送脚本
File log = new File("${WORKSPACE}/${LOG}"); logger.println(log.text);
执行构建时,Jenkins将在中创建文件${WORKSPACE}并填充它(我能够从Jenkins从站的终端中打印内容)。
Jenkins
${WORKSPACE}
但是,当尝试从Email插件访问它时,java.io.FileNotFoundException会引发异常:
Email
java.io.FileNotFoundException
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)
我很茫然,为什么Groovy会抱怨丢失的文件,而如果我用
Groovy
logger.println(log.getPath());
它将成功打印。
注: 该logger在变量Pre-send Script被通知插件提供的访问直接生成日志。
logger
java.io.File 方法将引用运行Jenkins的主服务器上的文件,而不是从属计算机上当前的工作空间中的文件。
java.io.File
要引用从属计算机上的文件,应使用以下readFile方法
readFile
def log = readFile("${WORKSPACE}/${LOG}");