在jenkinsfile管道脚本中为emailext构建变量

时间:2018-07-30 06:58:30

标签: jenkins-plugins

我正在尝试在jenkinsfile管道脚本中的emailext的presendScript中使用构建变量,如下所示。

emailext body: "Pipeline error: Please go to ${BUILD_URL} and verify the build",
subject: "'${JOB_NAME}' (${BUILD_NUMBER}) failed",
presendScript: '''
    logger.print(build.getEnvironment());
   ''',
to: 'my email address'

在运行构建时出现以下错误。

  

java.lang.NullPointerException:无法调用方法getEnvironment()   在空对象上   org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)     在   org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)

当在自由式项目中运行与presendScript相同的脚本时,build变量可用

我不确定我是否在适当的情况下不使用构建变量。

1 个答案:

答案 0 :(得分:0)

我不确定您要达到的目标。

如果要将环境添加到邮件正文中,请执行以下操作:

sh echo "Pipeline error: Please go to ${BUILD_URL} and verify the build" > mail.txt
sh printenv >> mail.txt

emailext body: readFile 'mail.txt',
subject: "'${JOB_NAME}' (${BUILD_NUMBER}) failed",
to: 'your email address'

我不确定传统的Jenkins语法;我对声明性管道会更熟悉:

sh 'echo "Pipeline error: Please go to ${BUILD_URL} and verify the build" > mail.txt'
sh 'printenv >> mail.txt'
emailext (
  body: readFile('mail.txt'),
  subject: "'${JOB_NAME}' (${BUILD_NUMBER}) failed",
  to: 'your email address'
)