2

Right now I am a newbie for Shell, Jenkins, Groovy pipeline. My requirement is I am reading file text into a variable under shell script and I need to pass this variable value out of shell script and to use in Groovy script.

Here is my code:

stages
    {
        stage('example')
        
        {
            steps
            {
                script
                {
                    
                sh'''
                    #!/bin/bash
                    set +x                  
                    readVal=$(<$WORKSPACE/test.json)
                    echo "$readVal"        //this is priting my entire json successfully                  
                    
                  echo 'closing shell script'
                    
                ''' 
                    
                    
                    println WORKSPACE    /// this is printing my workspace value successfully 
                    
                    println readVal     // not working 
                    println env.readVal     // not working 
                    println $readVal     // not working 

                
                }
            }
        }
    }

How to get readVal's value out of shell to access?

1 Answer 1

3

See Jenkins, Pipeline Best Practices:

a. Solution: Instead of using [a complex function], use a shell step and return the standard out. This shell would look something like this:

def JsonReturn = sh label: '', returnStdout: true, script: 'echo "$LOCAL_FILE"| jq "$PARSING_QUERY"'

Example

pipeline {
    agent any

    stages {
        stage('example') {
            steps {
                script {
                    def readVal = sh script: 'echo "READ_VAL_VALUE"', returnStdout: true
                    echo readVal
                    println readVal
                }
            }
        }
    }
}

Console Output

[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\Users\jenkins\AppData\Local\Jenkins\.jenkins\workspace\Pipeline project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (example)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo READ_VAL_VALUE
[Pipeline] echo
READ_VAL_VALUE

[Pipeline] echo
READ_VAL_VALUE

[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Sign up to request clarification or add additional context in comments.

4 Comments

Gerold, how can I access the variable value outside of shell?. I want to print that in groovy .Sorry I am new to this. Please let me know
@reachit The variable JsonReturn in the code sample is outside the sh(ell). 1. def is Groovy syntax. 2. sh is on the RHS (right-hand side) of the assignment and its stdout is assigned to the variable.
i am getting this exception groovy.lang.MissingPropertyException: No such property: $JsonReturn for class: groovy.lang.Binding ... i have used your code like below under script block ........ def JsonReturn = sh label: '', returnStdout: true, script: 'echo $WORKSPACE/test.json'
@reachit You wrote "to use in Groovy script". $JsonReturn is not Groovy syntax. It's simply JsonReturn there or "JsonReturn=${JsonReturn}" to be interpolated inside a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.