how can I get the value of a global variable within a function in a pipeline?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

how can I get the value of a global variable within a function in a pipeline?

jesus fernandez
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/91e7dbcd-c806-4d55-bc31-bc9a9ad4057cn%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: how can I get the value of a global variable within a function in a pipeline?

Ven H
Try defining it inside the "environment" stage / step.

Regards,
Venkatesh


On Thu, Feb 11, 2021 at 11:26 PM jesus fernandez <[hidden email]> wrote:
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/91e7dbcd-c806-4d55-bc31-bc9a9ad4057cn%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPp28eqF44WGYh0b-61Eji_TyTrceyvqp78ti4o%3Dx7i-eEmOjQ%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: how can I get the value of a global variable within a function in a pipeline?

jesus fernandez
Thanks for replying, any example or any link where to get information about how to do so? I am a bit new to Jenkins

El jueves, 11 de febrero de 2021 a las 19:11:24 UTC+1, [hidden email] escribió:
Try defining it inside the "environment" stage / step.

Regards,
Venkatesh


On Thu, Feb 11, 2021 at 11:26 PM jesus fernandez <[hidden email]> wrote:
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/91e7dbcd-c806-4d55-bc31-bc9a9ad4057cn%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f2756185-f00c-4bf8-a382-f7659da29ca6n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: how can I get the value of a global variable within a function in a pipeline?

Ven H

On Thu, Feb 11, 2021 at 11:44 PM jesus fernandez <[hidden email]> wrote:
Thanks for replying, any example or any link where to get information about how to do so? I am a bit new to Jenkins

El jueves, 11 de febrero de 2021 a las 19:11:24 UTC+1, [hidden email] escribió:
Try defining it inside the "environment" stage / step.

Regards,
Venkatesh


On Thu, Feb 11, 2021 at 11:26 PM jesus fernandez <[hidden email]> wrote:
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/91e7dbcd-c806-4d55-bc31-bc9a9ad4057cn%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f2756185-f00c-4bf8-a382-f7659da29ca6n%40googlegroups.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPp28epL%2BCgm8yvzX0qNMNZbQ0Odxj5xaRsgJz73i40pQ5tRwg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: how can I get the value of a global variable within a function in a pipeline?

Victor Martinez
In reply to this post by jesus fernandez
You can simplify it quite a bit with the post stage event https://www.jenkins.io/doc/book/pipeline/syntax/#post

The example pipeline uses the post stages section, you can use the one specific the stage 3


Environment variables within an environment section are immutable, though you can use the script closure with the env map to set a new env variable that can be override

For instance:

stage(“stage 3”) {
  steps{
     script{
       env.FAILED_STAGE=env.STAGE_NAME
     }
     error ...
  }
}

Cheers





See 

On Thursday, 11 February 2021 at 17:56:40 UTC [hidden email] wrote:
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/196f4d91-2b7b-441f-b080-f54595c71439n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: how can I get the value of a global variable within a function in a pipeline?

jesus fernandez
Thanks a lot Victor, the links provided made it much clear now and the example worked just fine. You are always saving me hehe

El jueves, 11 de febrero de 2021 a las 22:11:15 UTC+1, [hidden email] escribió:
You can simplify it quite a bit with the post stage event https://www.jenkins.io/doc/book/pipeline/syntax/#post

The example pipeline uses the post stages section, you can use the one specific the stage 3


Environment variables within an environment section are immutable, though you can use the script closure with the env map to set a new env variable that can be override

For instance:

stage(“stage 3”) {
  steps{
     script{
       env.FAILED_STAGE=env.STAGE_NAME
     }
     error ...
  }
}

Cheers





See 

On Thursday, 11 February 2021 at 17:56:40 UTC [hidden email] wrote:
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/16c44a41-f7a2-4c4e-94d3-c820bebdf138n%40googlegroups.com.