简体   繁体   中英

Why am I not seeing terminal output in the Build Console for this Jenkinsfile?

I'm a little confused as to why I am not seeing the output from the echo() statements in the jenkinsSlack() function in this Jenkinsfile. The stages are definitely running as I can see them executing in the Pipeline visualization.

#!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

Echo messages are missing because jenkinsSlack(type, channel=slack_channel()) just returns the value of slack_channel() without executing the method body including echo.

This is Jenkins specific problem related to CPS transform. Jenkins pipeline script is based on groovy but it has some constraints with regard to its syntax and usage. See more details here: https://github.com/jenkinsci/workflow-cps-plugin/blob/master/README.md#technical-design

Possible workarounds below.

1.Use @NonCPS annotation for slack_channel()

@NonCPS
def slack_channel() {
  try { if ('' != SLACK_CHANNEL) { return SLACK_CHANNEL } }
  catch (MissingPropertyException e) { return '#nathan-webhooks' }
}

2.determine SLACK_CHANNEL in advance and pass it to the default argument 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM