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}"
'''
}
}
}