我有点困惑,为什么在这个Jenkinsfile的jenkinsSlack()函数中看不到echo()语句的输出。阶段肯定在运行,因为我可以看到它们在管道可视化中执行。
#!groovy
def slack_channel() {
try { if ('' != SLACK_CHANNEL) { return SLACK_CHANNEL } }
catch (MissingPropertyException e) { return '#nathan-webhooks' }
}
// simplify the generation of Slack notifications for start and finish of Job
def jenkinsSlack(type, channel=slack_channel()) {
echo("echo 'In jenkinsSlack()...")
echo("echo 'type specified as : ${type}'")
echo("echo 'channel specified as : ${channel}'")
if ( 'SUCCESS' == currentBuild.result ) {
slackSend channel: channel, color: 'good', message: "type: ${type}"
} else if ( 'FAILURE' == currentBuild.result ) {
slackSend channel: channel, color: 'danger', message:"type: ${type}"
} else {
slackSend channel: channel, color: 'warning', message: "type: ${type}"
}
// node - action starts here
node {
wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm', 'defaultFg': 2, 'defaultBg':1]) {
stage ("send Slack start message") {
checkout scm
jenkinsSlack('start')
}
stage("send Slack finish message") {
jenkinsSlack('finish')
}
} // AnsiColorBuildWrapper
}Thx
发布于 2016-10-29 11:49:40
由于jenkinsSlack(type, channel=slack_channel())只返回slack_channel()的值,而不执行包含echo的方法体,因此缺少Echo消息。
这是与CPS变换相关的Jenkins特有的问题。Jenkins管道脚本是基于groovy的,但是它在语法和用法方面有一些限制。点击此处查看更多详情:https://github.com/jenkinsci/workflow-cps-plugin/blob/master/README.md#technical-design
下面是可能的解决方法。
1.对slack_channel()使用@NonCPS注释
@NonCPS
def slack_channel() {
try { if ('' != SLACK_CHANNEL) { return SLACK_CHANNEL } }
catch (MissingPropertyException e) { return '#nathan-webhooks' }
}2.提前确定SLACK_CHANNEL,并将其传递给默认参数channel
def slack_channel() {
try { if ('' != SLACK_CHANNEL) { return SLACK_CHANNEL } }
catch (MissingPropertyException e) { return '#nathan-webhooks' }
}
SLACK_CHANNEL = slack_channel()
def jenkinsSlack(type, channel=SLACK_CHANNEL) {
echo type
echo channel
}https://stackoverflow.com/questions/40312316
复制相似问题