user3256114
user3256114

Reputation: 119

How to assign a shell command output as value to envrionment variable?

Below is my pipeline snippet and I am trying to assign RSTATE variable a value at run time. This value is basically stored in a text file but we need to grep and cut it. So a shell command output should be its value.

pipeline
{
    agent any
    environment
    {
        RSTATE = 'R4C' 
        ISO_REV = 'TA'
        BuildSource = '18'
    }
    stages
    {   
        stage('get Rstate')
        {  
            echo env.RSTATE
        }
    } 
}

I am trying to assign RSTATE value like:

 RSTATE = sh ( script: 'grep RSTATE /proj/MM/scm/com/iv_build/mm18_1/rstate/next_rstate.txt
                        |cut -d "=" -f2', returnStdout: true).trim()

But this is not working. I also tried to run a shell script but that also not works. Only hard coded value is working. Please suggest.

Upvotes: 0

Views: 2515

Answers (1)

cpanato
cpanato

Reputation: 429

I tested and worksm you need to validate if your script return the value you want.

    pipeline
    {
        agent any
        environment
        {
            RSTATE = 'R4C' 
            ISO_REV = 'TA'
            BuildSource = '18'
        }
        stages
        {   
            stage('get Rstate')
            {  
                steps {
                    script {
                        def RSTATE2 = sh ( script: 'echo \${RSTATE}', returnStdout: true).trim()
                        echo env.RSTATE
                        echo RSTATE2
                    }
                }
            }
        } 
    }

Upvotes: 2

Related Questions