2

I am on Jenksin 2.46.2 and have a job that uses Active Choices Reactive Parameter. I select my servers in my first selection from a XML file using XMLSlurper and reference that for my second selection. When I hardcode the server name the code works fine. When I use the variable in my code I get an error.

This code works:

def serverList = new XmlSlurper().parse("/app/jenkins/jobs/servers.xml")
def SERVER = 'testserver1'
def output = []
serverList.Server.find { it.@name == SERVER}.CleanUp.GZIP.File.each{ 
    it.output.add(p)
}

return output

When I reference the variable selection from my previous selection I get the error:

def serverList = new XmlSlurper().parse("/app/jenkins/jobs/servers.xml")
def SERVER = SERVER
def output = []
serverList.Server.find { it.@name == SERVER}.CleanUp.GZIP.File.each{ 
    it.output.add(p)
}

return output

The error that I am getting is below. Any idea why I get an error?

WARNING: failed to serialize [[/app/test2/log], [/app/test2/log]] for ...*other text*... net.sf.json.JSONException: There is a cycle in the hierarchy!

Here is my XML file:

<ServerList>
  <Server name="testserver1">
      <CleanUP>
        <GZIP>
          <File KeepDays="30">/app/test1/log</File>
        </GZIP>
      </CleanUP>
    </Server>  
  <Server name="testserver2">
    <CleanUP>
      <GZIP>
        <File KeepDays="30">/app/test2/log</File>
      </GZIP>
    </CleanUP>
  </Server>
</ServerList>

NE.jpg

Rao
  • 20,781
  • 11
  • 57
  • 77
Ryan Johnson
  • 109
  • 1
  • 2
  • 8

1 Answers1

1

Here is the script that you need, which reads the value of File element and returns a list:

def serverList = new XmlSlurper().parse("/app/jenkins/jobs/servers.xml")
return serverList.'**'.findAll{ it.name() == 'File'}*.text()

Output:

[/app/test1/log, /app/test2/log]

EDIT: based on OP comments

def server = 'testserver1'
def serverList = new XmlSlurper().parse("/app/jenkins/jobs/servers.xml")
def result = serverList.'**'.find{ it.@name == server}.CleanUP.GZIP.File
println result
return result

EDIT2:

If you want list or array, try below:

def server = 'testserver1'
def serverList = new XmlSlurper().parse("/app/jenkins/jobs/servers.xml")
def result = serverList.'**'.findAll{ it.@name == server}*.CleanUP.GZIP.File.text()
println result
return result
Rao
  • 20,781
  • 11
  • 57
  • 77
  • @RyanJohnson, did the above helped? If not can you clarify what is needed? – Rao May 29 '17 at 16:56
  • @RyanJohnson, do you just want value of file for matching `testserver1`?which will be single value not a list. – Rao May 30 '17 at 16:50
  • I think my issue is with my code to build my array of servers. I am trying to put the code in here but it is not pasting it nicely. My output should be ['testserver1','testserver2']..... def output = [] serverList.Server.findAll {output.add(it.@name)} return output – Ryan Johnson May 30 '17 at 16:51
  • @RyanJohnson, can you please check the edit part of the answer and see if that is helpful? check the demo [here](http://groovyconsole.appspot.com/script/6323578999930880) – Rao May 30 '17 at 17:03
  • I removed all my other code and I do see the "failed to serialize" error on my code around getting the servers. So maybe the issue is with that code causing issues with other code. – Ryan Johnson May 30 '17 at 17:53
  • @RyanJohnson, have you checked the demo link from the previous comment? Is that what you are looking for? Is it showing desired output? – Rao May 30 '17 at 18:02
  • I tried that but it did not work yet....thank you for your assistance. I need the values to be in an array because they are going into a dropdown box where someone can select a value. I was not sure how to modify that code so the return values are in an array. – Ryan Johnson May 30 '17 at 19:26
  • @RyanJohnson, if you are expecting array, check `EDIT2`. Just need to use `findAll`. – Rao May 31 '17 at 02:26