Christophe
Christophe

Reputation: 25

Jenkins: Retrieve a variable from json file and use it after

I have a issue on jenkins. I make a http request and this return me a json file like 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 the ID to use in a other http request:

http://address:port/prweb/api/v1/merges/{$ID}

I try to catch the Id like this: ID=$(cat merge.json |grep -o SY.*[a-z] (all json file are the same)

I try to catch the ID in a sh pipe but he doesn't work, so I tried to define on the step, same as before. If someone had the solution, it'll be great for me! I continue to search and I'll edit if I suceed

Edit: the code of my pipe:

pipeline{
agent any
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/'

        }
    }
}

}

Upvotes: 1

Views: 8653

Answers (2)

JarJarrr
JarJarrr

Reputation: 410

I believe the easiest way would be to use the readJson plugin from jenkins pipeline:

pipeline{
agent any
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'
        def jsonResponse = readJSON file: 'merge.json'

        httpRequest authentication: 'b689fe3c-117e-4076-b10d-fe16ab14742f', httpMode: 'GET', outputFile: 'merge3.json', responseHandle: 'NONE', url: "http://address:port/prweb/api/v1/merges/${jsonResponse.ID}"

    }
}
}

you can alternatively read the response content rather than saving to a file:

pipeline{
agent any
stages{
stage ('Merge Branch') {

    steps{

        def response = httpRequest authentication: 'b689fe3c-117e-4076-b10d-fe16ab14742f', httpMode: 'POST', responseHandle: 'NONE', url: 'http://address:port/prweb/api/v1/branches/TestB/merge'
        def jsonResponse = readJSON text: "${response.content}"

        httpRequest authentication: 'b689fe3c-117e-4076-b10d-fe16ab14742f', httpMode: 'GET', outputFile: 'merge3.json', responseHandle: 'NONE', url: "http://address:port/prweb/api/v1/merges/${jsonResponse.ID}"
    }
}
}

Upvotes: 0

burnettk
burnettk

Reputation: 14047

that grep doesn't work for me. i'm sure you could get it working with enough messing around.

if you install jq, you can do the following:

ID="$(cat merge.json | jq -r .ID)"

# produces SYSTEM-QUEUE-MERGE 50304628545400035CA951969013040610A435733ECEAE8 
# as expected
echo "$ID"

of course even when you successfully get the output parsed by the shell, then you would still need to get the result back into the groovy context in order to use httpRequest. a hot solution is avoiding httpRequest entirely and just using curl for the three requests. :) this approach (using external scripts for your builds) is, perhaps unintuitively, advocated by the jenkins folks.

if you must keep the http requests in groovy, here is a full pipeline that answers your 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}"
        }
      }
    }
  }
}

Upvotes: 1

Related Questions