jenkinsci / google-chat-notification-plugin

Google Chat Notification Jenkins Plugin to send build status
https://plugins.jenkins.io/google-chat-notification/
MIT License
39 stars 20 forks source link

Unable to use defined variables #15

Closed Redirts closed 5 years ago

Redirts commented 5 years ago

I have a pipeline script like this:


pipeline{
    agent any
        environment {
def author_name = 'UNKNOWN'
def committer_name = 'UNKNOWN'
def commit_message = 'UNKNOWN'
    }
...
   stages{
        stage("Checkout"){
            steps{
                checkout scm

                            script {
              // extract author name, committer name, and commit message of GIT_COMMIT
              author_name = sh(script: "git log -n 1 ${env.GIT_COMMIT} --format=%aN", returnStdout: true).trim()
              echo "Author name of last commit is ${author_name}"
              committer_name = sh(script: "git log -n 1 ${env.GIT_COMMIT} --format=%cN", returnStdout: true).trim()
              echo "Committer name of last commit is ${committer_name}"
              commit_message = sh(script: "git log -1 --format=%B ${GIT_COMMIT}", returnStdout: true).trim()
              echo "Commit message of last commit is ${commit_message}"
            }
....
    post{
        failure{
            echo "Author name of last commit is ${author_name}"

            googlechatnotification url: '...', message: env.JOB_NAME + 'Jenkins build failed! \n User: ${author_name}', notifyAborted: 'true', notifyFailure: 'true', notifyNotBuilt: 'true', notifySuccess: 'false', notifyUnstable: 'true', notifyBackToNormal: 'true', suppressInfoLoggers: 'true', sameThreadNotification: 'true'
        }
    }

The echo prints fine but the google notification message prints:

BuildName Jenkins build failed! 
 URL: ${author_name}

What I am missing here?

chirag-vuclip commented 5 years ago

You are using single quotes '' in message -> env.JOB_NAME + 'Jenkins build failed! \n User: ${author_name}' Single quote does not resolve variables defind in ${}. Instead use double quotes "" in message -> env.JOB_NAME + "Jenkins build failed! \n User: ${author_name}"

This is how groovy works. Please try above change and let me know

Redirts commented 5 years ago

Works