Reputation: 39
I am using the below Jenkinsfile
. It is printing the value of upload_id
when it is executing with the jq
command, but when printing the value it is showing null
.
Please help me to fix this variable issue.
sh label: '', script: 'upload_id=$(cat output_file.json | jq -r \'.upload_id\')'
sh "echo \"$upload_id\""**
Output :
[Pipeline] sh cat output_file.json jq -r .upload_id upload_id=8f304c6d-804b-440a-xxxx** [Pipeline] sh echo upload_id : null upload_id : null
[Pipeline] }
Upvotes: 0
Views: 1613
Reputation: 27776
I would strongly recommend to go with Matt's answer, because it is the cleanest way.
Anyway, there are situations where there is no other choice than to use the shell, so here is the shell way:
script {
def upload_id = sh label: '',
script: 'echo $(cat output_file.json | jq -r \'.upload_id\')',
returnStdout: true
upload_id = upload_id.trim() // remove extraneous whitespace
sh "echo \"$upload_id\""
}
I've already linked to a more detailed answer of mine but you were probably not getting it to work, because you are using a declarative pipeline. In declarative pipeline, you have to use a script
block to be able to store return values of steps.
Upvotes: 1
Reputation: 28854
You can easily do this intrinsically in Jenkins Pipeline to avoid all of the issues with subprocess execution. First, read in and parse the JSON file:
upload_info = readJSON(file: 'output_file.json')
Then, you can access the returned values in the assigned upload_info
Map normally:
upload_id = upload_info['upload_id']
Upvotes: 1