1

我有主从詹金斯设置。Editable Email Notification在詹金斯的一项工作中,只有当环境变量具有特定值时,我才需要通过插件发送电子邮件。所以我在Execute Shell命令中设置环境变量的值如下

echo "SHOULD_SEND_EMAIL=$should_send_email" > $WORKSPACE/build/env.properties

在构建后操作中,我有一个步骤可以使用触发器发送电子邮件,并且Script-After Build具有以下触发器脚本

def workspace = build.getWorkspace()
println "Value of workspace is ${workspace}"

filename = workspace.toString() + "/build/env.properties"
println "Value of filename is ${filename}"

def logs = readFile("${filename}")
println "Value of logs is ${logs}"

但它在错误以下失败

 15:05:54 Value of workspace is /Users/jenkins/workspace/experiment-job
15:05:54 Value of filename is /Users/jenkins/workspace/experiment-job/build/env.properties
15:05:54 ERROR: Build step failed with exception
15:05:54 groovy.lang.MissingMethodException: No signature of method: Script1.readFile() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [/Users/jenkins/workspace/experiment-job/build/env.properties]
15:05:54    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
15:05:54    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
15:05:54    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
15:05:54    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
15:05:54    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
15:05:54    at Script1.run(Script1.groovy:7)
15:05:54    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
15:05:54    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
15:05:54    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
15:05:54    at hudson.plugins.emailext.plugins.trigger.AbstractScriptTrigger.trigger(AbstractScriptTrigger.java:55)
15:05:54    at hudson.plugins.emailext.ExtendedEmailPublisher._perform(ExtendedEmailPublisher.java:269)
15:05:54    at hudson.plugins.emailext.ExtendedEmailPublisher.perform(ExtendedEmailPublisher.java:253)
15:05:54    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
15:05:54    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
15:05:54    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:720)
15:05:54    at hudson.model.Build$BuildExecution.cleanUp(Build.java:195)
15:05:54    at hudson.model.Run.execute(Run.java:1788)
15:05:54    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
15:05:54    at hudson.model.ResourceController.execute(ResourceController.java:98)
15:05:54    at hudson.model.Executor.run(Executor.java:410)
15:05:54 Build step 'Editable Email Notification' marked build as failure

我尝试传递文件的硬编码值,考虑到 readFile 将从工作区而不是绝对路径准备好,如下所示

def logs = readFile("/build/env.properties")
println "Value of logs is ${logs}"

但它仍然失败以下错误

14:55:32 Value of workspace is /Users/jenkins/workspace/experiment-job
14:55:32 Value of filename is /Users/jenkins/workspace/experiment-job/build/env.properties
14:55:32 ERROR: Build step failed with exception
14:55:32 groovy.lang.MissingMethodException: No signature of method: Script1.readFile() is applicable for argument types: (java.lang.String) values: [build/env.properties]
14:55:32    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
14:55:32    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
14:55:32    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
14:55:32    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
14:55:32    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
14:55:32    at Script1.run(Script1.groovy:7)
14:55:32    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
14:55:32    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
14:55:32    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
14:55:32    at hudson.plugins.emailext.plugins.trigger.AbstractScriptTrigger.trigger(AbstractScriptTrigger.java:55)
14:55:32    at hudson.plugins.emailext.ExtendedEmailPublisher._perform(ExtendedEmailPublisher.java:269)
14:55:32    at hudson.plugins.emailext.ExtendedEmailPublisher.perform(ExtendedEmailPublisher.java:253)
14:55:32    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
14:55:32    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
14:55:32    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:720)
14:55:32    at hudson.model.Build$BuildExecution.cleanUp(Build.java:195)
14:55:32    at hudson.model.Run.execute(Run.java:1788)
14:55:32    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
14:55:32    at hudson.model.ResourceController.execute(ResourceController.java:98)
14:55:32    at hudson.model.Executor.run(Executor.java:410)
14:55:32 Build step 'Editable Email Notification' marked build as failure

我不能使用https://stackoverflow.com/a/50503979/1465536中提到的文件。有人可以帮我理解这里的问题吗?如果您有任何其他解决方案可以根据情况发送电子邮件,我也很高兴知道。谢谢你。

4

1 回答 1

-1

错误提示使用错误readFile,更改如下:

def logs = readFile file: filename 
于 2019-04-18T23:56:09.817 回答