每当我的Jenkins作业的构建完成,我就会使用Editable Email Notification
插件(又名Email-ext
插件)发送一封包含结果的电子邮件.此外,如果系统中存在某个文件,我正在修改消息的默认内容以通知该文件的存在.
为此,我正在使用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从站中的终端打印内容).
但是当尝试从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.(FileInputStream.java:146) at groovy.util.CharsetToolkit. (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
抱怨丢失的文件,而如果我改为打印路径
logger.println(log.getPath());
它会成功打印出来.
注意:通知插件提供
的logger
变量Pre-send Script
可直接访问构建日志.
在Jenkins管道中,File log = new File("${WORKSPACE}/${LOG}");
不起作用.
您可以使用READFILE而不是
如
高清登录= READFILE "$ {WORKSPACE}/$ {} LOG"
java.io.File
方法将引用运行Jenkins的主服务器上的文件,而不是从属计算机上当前的工作空间中的文件。
要引用从属计算机上的文件,应使用以下readFile
方法
def log = readFile("${WORKSPACE}/${LOG}");