0

Not a lot of experience programming but happy to learn.

Question: Sending jenkinsfile build error data to mattermost works just fine locally in the file, but when loaded as a .groovy script it fails "silently". Any guidance on why / how I can post this information to a channel in a loaded groovy script?

Jenkinsfile

#!groovy
properties([[$class: 'jenkins.model.BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '5', artifactNumToKeepStr: '5']]])

node('node_name'){

    def err = null
    currentBuild.result = "SUCCESS"
    role_name = "deploy"

    try {
        timeout(60){
            stage "${role_name}"
                deleteDir()
                checkout scm
                sh 'introduce error here'
        }
    }
    catch (error){
        err = error
        currentBuild.result = "FAILURE"
        load "ci/curlBuildFailed.groovy"
    }
    finally {
        if (err){
            throw err
        }
    }
}

curlBuildFailed.groovy

sh "curl -i -X POST -d \'payload={" +
"\"text\": \"${env.JOB_NAME}: ${env.BUILD_URL} - build failed with ${err}!\", " +
"\"username\": \"JenkinsBot\", " +
"\"icon_url\": \"$FAIL_BUILD\"}\' $DEVOPS_NOTIFY_URL "

Running the above produces this:

[cure-deploy] Running shell script
+ introduce error here
/home/jenkins/remote/workspace/blablabla/script.sh: line 2: introduce: command not found
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // timeout
[Pipeline] load
[Pipeline] { (ci/curlBuildFailed.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] }
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // load
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

Notes:

  • Dropping the contents of curlBuildFailed.groovy into the same location as load "ci/curlBuildFailed.groovy" creates the desired output.
  • Have narrowed down the culprit to ${err} in curlBuildFailed.groovy, as soon as I remove that variable it posts to mattermost just fine.
  • (We have about 30 ansible roles similar to this one so trying to abstract the duplicate code to a easier to maintain structure)
  • Have searched around for a few hours and it seems similar to these but I hadn't found a solve yet:
  • jenkins plain catch blocks
  • don't think I need a 'returns this;' but who knows
  • Passing Variables in Jenkinsfile Closure

Thank you for your time! - Sam

stach
  • 1
  • 1
  • 3

1 Answers1

0

You dont need to use the groovy script to send a message to Mattermost. There is a Jenkins Plugin that helps you to send notification messages to Mattermost. You can follow this link: https://github.com/jenkinsci/mattermost-plugin

After the installation and configuration you can use the mattermostSend to send a notification.

then your pipeline will similar to this:

node('node_name') {def err = null
    currentBuild.result = "SUCCESS"
    role_name = "deploy"

    try {
        timeout(60){
            stage "${role_name}"
                deleteDir()
                checkout scm
                sh 'introduce error here'
        }
    }
    catch (error){
        err = error
        currentBuild.result = "FAILURE"
        mattermostSend color: 'danger', message: 'Message from Jenkins Pipeline', text: \"${env.JOB_NAME}: ${env.BUILD_URL} - build failed with ${err}!\"
    }
    finally {
       if (err){
          throw err
       }
    }
}
cpanato
  • 409
  • 5
  • 8