Jenkins: Retrieve a variable from json file and use it after -
i have issue on jenkins. make http request , return me json file this:
httprequest authentication: 'b689fe3c-117e-4076-b10d-fe16ab14742f', httpmode: 'post', outputfile: 'merge.json', responsehandle: 'none', url:'http://address:port/prweb/api/v1/branches/testb/merge' { "id": "system-queue-merge 50304628545400035ca951969013040610a435733eceae8", "pxobjclass": "pega-api-ci-branch", "statusvalue": "ok" }
i want id use in other http request:
http://address:port/prweb/api/v1/merges/{$id}
i try catch id this: id=$(cat merge.json |grep -o sy.*[a-z] (all json file same)
i try catch id in sh pipe doesn't work, tried define on step, same before. if had solution, it'll great me! continue search , i'll edit if suceed
edit: code of pipe:
pipeline{ agent stages{ stage ('merge branch') { steps{ httprequest authentication: 'b689fe3c-117e-4076-b10d-fe16ab14742f', httpmode: 'post', outputfile: 'merge.json', responsehandle: 'none', url: 'http://address:port/prweb/api/v1/branches/testb/merge' httprequest authentication: 'b689fe3c-117e-4076-b10d-fe16ab14742f', httpmode: 'get', outputfile: 'merge3.json', responsehandle: 'none', url: 'http://address:port/prweb/api/v1/merges/' } } }
}
that grep doesn't work me. i'm sure working enough messing around.
if install jq, can following:
id="$(cat merge.json | jq -r .id)" # produces system-queue-merge 50304628545400035ca951969013040610a435733eceae8 # expected echo "$id"
of course when output parsed shell, still need result groovy context in order use httprequest
. hot solution avoiding httprequest
entirely , using curl 3 requests. :) approach (using external scripts builds) is, perhaps unintuitively, advocated jenkins folks.
if must keep http requests in groovy, here full pipeline answers question:
pipeline { agent { label 'docker' } stages { stage('build') { steps { script { def idfromjson = sh(script: "cat merge.json | jq -r .id", returnstdout: true).trim() # produces output: idfromjson: system-queue-merge 50304628545400035ca951969013040610a435733eceae8 echo "idfromjson: ${idfromjson}" } } } } }
Comments
Post a Comment