Groovy 报告文件在系统中确实存在时不存在 [英] Groovy reports that a file doesn't exists when it really is present in the system

查看:32
本文介绍了Groovy 报告文件在系统中确实存在时不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我的 Jenkins 作业构建完成时,我都会使用 Editable Email Notification 插件(又名 Email-ext 插件)发送一封包含结果的电子邮件.此外,如果系统中存在某个文件,我将修改消息的默认内容以通知该文件的存在.

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.

为此,我使用插件的 Pre-send Script 字段.

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

准备运行环境

属性内容

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} 中创建文件并填充它(我可以在终端打印内容詹金斯奴隶).


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

但是当尝试从 Email 插件访问它时,会引发异常 java.io.FileNotFoundException:

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)

我不知道为什么 Groovy 抱怨丢失的文件,而如果我用

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());

它会打印成功.

注意:Pre-send Script 中的 logger 变量由通知插件提供,用于直接访问构建日志.

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

推荐答案

java.io.File 方法将引用 ma​​ster Jenkins 运行的位置,而不是 slave 机器上的当前工作区(以防此作业确实在从机器上运行).

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

要引用从机上的文件,您应该使用readFile方法

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

def log = readFile("${WORKSPACE}/${LOG}");

这篇关于Groovy 报告文件在系统中确实存在时不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆