I have a declarative pipeline with a few steps. at the end I send a message to 
slack with some statistics of the compilation times and some more information. 
I want to send in case it fails the stage at which it fails. I have set a 
global variable but the function does not seem to be able to read its value. 
This is a simplified version of what I have: 

```
def FAILED_STAGE

def sendSlackNotifcation() 
{
    if ( currentBuild.currentResult == "SUCCESS" ) {
        slackSend message: "${currentBuild.result}", channel: '#jenkins_bot2'
    }
        
    else {
        slackSend color : "#FF0000", message: "${FAILED_STAGE}", channel: 
'#jenkins_bot2'

    }
}

pipeline {
    agent any

    stages {
        stage('stage1') {
            steps{
                echo "stage1"
            }
        }     
        
        stage('stage2') {
            steps{
                echo "stage2"
            }
        }
        
        stage("Stage 3") {
            steps {
                script {
                    FAILED_STAGE=env.STAGE_NAME
                    echo "${FAILED_STAGE}"
                    error "failed for some reason."
                }
            }
        }
    }
    post {
    failure {
        sendSlackNotifcation()
        }
    }

}
```
And this is the output I get:
```
13:38:53  [Pipeline] {
13:38:53  [Pipeline] stage
13:38:53  [Pipeline] { (stage1)
13:38:53  [Pipeline] echo
13:38:53  stage1
13:38:53  [Pipeline] }
13:38:53  [Pipeline] // stage
13:38:53  [Pipeline] stage
13:38:53  [Pipeline] { (stage2)
13:38:53  [Pipeline] echo
13:38:53  stage2
13:38:53  [Pipeline] }
13:38:53  [Pipeline] // stage
13:38:53  [Pipeline] stage
13:38:53  [Pipeline] { (Stage 3)
13:38:53  [Pipeline] script
13:38:53  [Pipeline] {
13:38:53  [Pipeline] echo
13:38:53  Stage 3
13:38:53  [Pipeline] error
13:38:53  [Pipeline] }
13:38:53  [Pipeline] // script
13:38:53  [Pipeline] }
13:38:53  [Pipeline] // stage
13:38:53  [Pipeline] stage
13:38:53  [Pipeline] { (Declarative: Post Actions)
13:38:53  Error when executing failure post condition:
13:38:53  groovy.lang.MissingPropertyException: No such property: FAILED_STAGE 
for class: WorkflowScript
```

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/91e7dbcd-c806-4d55-bc31-bc9a9ad4057cn%40googlegroups.com.

Reply via email to