在我的管道脚本中,我添加了一些环境变量,但无法在email-ext插件中访问它
def myvariable = 'HELLO WORLD'
def content = readFile 'template.html'
content = content.replaceAll('MYVAR',myvariable)
...
由于我正在从文件中读取电子邮件正文,我还尝试替换字符串,但在org.codehaus.groovy.runtime.StringGroovyMethods.replaceAll
中获得了java.lang.NullPointerExceptiondo_something(){ int a = 0; }
有什么想法吗?谢谢!
答案 0 :(得分:0)
尝试使用双引号,例如:
emailext body: "${env.MYVAR}", subject: 'subject', to: 'hello@world.com'
答案 1 :(得分:0)
您可以为电子邮件正文指定单独的Groovy脚本文件,如下所示:
emailext body: '${SCRIPT, template="build-result.groovy"}', subject 'subject', to: 'hello@world.com'
模板文件需要存在于Jenkins安装的email-templates文件夹中。
然后,您可以在Groovy脚本中使用以下内容来访问管道中设置的环境变量:
<%
def envOverrides = it.getAction("org.jenkinsci.plugins.workflow.cps.EnvActionImpl").getOverriddenEnvironment()
if (envOverrides.containsKey("MYVAR"))
{
println "myvar = " + envOverrides["MYVAR"]
}
%>
答案 2 :(得分:0)
经过几个小时的尝试,我使用字符串连接完成了
pipeline {
agent any
stages {
....
}
post {
always {
script {
env.LAST_COMMIT = sh (
script: 'git log -n 3',
returnStdout:true
).trim()
}
echo "Last 3 commits: $LAST_COMMIT"
emailext (
attachLog: true,
body: '''$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS:
Build Url: $BUILD_URL
Last 3 commits: '''
+ env.LAST_COMMIT,
postsendScript: '$DEFAULT_POSTSEND_SCRIPT',
presendScript: '$DEFAULT_PRESEND_SCRIPT',
replyTo: '$DEFAULT_REPLYTO',
subject: '*$BRANCH_NAME - $BUILD_STATUS* - $DEFAULT_SUBJECT',
to: '''
name@domain.com, name@domain.com
''',
from: 'jenkins@domain.com'
)
}
}
}