1

In the below code I am fetching the version inside the script block, but I can not use it in the sh block and even not in the post block. Can someone please help. I am able to print the value in the println function. Variable is generating at the run time so I can not define inside the global environment block of Jenkinsfile. Can someone please help and tell me what am missing here?

stage('test build') {
            steps {
                script{
                        version = sh (
                        script: "cat ${WORKSPACE}/version.txt | grep var | awk -F ':' '{print \$2}'",
                        returnStdout: true
                    ).trim()
                }
                println(version)
                sh '''
                    echo "${version}"                    
                '''
            }
            post {
                success {
                    sh '''
                        echo "${version}"
                        
                    '''
                }
            } 
        }
  • Does this answer your question? [Using global variables in Jenkins Pipeline](https://stackoverflow.com/questions/52063864/using-global-variables-in-jenkins-pipeline) – Kai Jun 25 '20 at 14:57

0 Answers0