0

This is the pipeline I have created for processing a json from an API. However when I run this it fails after the first loop.

import groovy.json.JsonSlurper
pipeline {
agent none

stages {
    stage('Query Pull Requests'){
        steps{
            script {
                def response =  httpRequest authentication: 'BitbucketAuth', url: "https://api.bitbucket.org/2.0/repositories/repo-name/pullrequests"
                echo "Status: ${response.status}"
                def json = new JsonSlurper().parseText(content)
                def pullrequests = json.values;
                for (int i = 0; i < pullrequests.size(); i++) {
                    stage("Processing Pull Request ID : ${pullrequests[i].id}"){
                        echo "${pullrequests[i].source.branch.name}"
                        echo "${pullrequests[i].destination.branch.name}"
                        echo "${pullrequests[i].destination.repository.full_name}"
                    }
                }
            }
        }
    }
  }
}

This is the error I am getting

Jenkins ver. 2.107

Gayan Jayasingha
  • 752
  • 2
  • 17
  • 33

1 Answers1

1

You have to unset the "json" variable to null once you do not need that variable anymore:-

def json = new JsonSlurper().parseText(content)
def pullrequests = json.values;
for (int i = 0; i < pullrequests.size(); i++) {
  stage("Processing Pull Request ID : ${pullrequests[i].id}"){
  echo "${pullrequests[i].source.branch.name}"
  echo "${pullrequests[i].destination.branch.name}"
  echo "${pullrequests[i].destination.repository.full_name}"
}

// unset response because it's not serializable and Jenkins throws NotSerializableException.
json = null

For more information follow this link

user_9090
  • 1,884
  • 11
  • 28