Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: Using XmlSlurper In Declarative Pipeline

Tags:

Using XmlSlurper, I am trying to read an XML file (specifically Web.config from a .Net-based API) as part of a Jenkins pipeline. I do not seem to be able to access any attributes of elements. The error I get is:

No such field found: field groovy.util.slurpersupport.NodeChild primary

Below is my attempt to break this down into the simplest case:

script {
    def xml = """
              <colors>
                  <color primary="true">Red</color>
                  <color primary="true">Yellow</color>
                  <color primary="true">Blue</color>
                  <color primary="false">Purple</color>
              </colors>                    
              """

    def colors = new XmlSlurper().parseText(xml)
    echo "First Color: ${colors.color[0]}" //works fine
    echo "First Color: ${colors.color[0]} Primary? ${colors.color[0].@primary}" //fails

}

I am using Jenkins 2.121.1.

Any help is appreciated.

like image 200
voodoobilly Avatar asked Aug 21 '18 01:08

voodoobilly


People also ask

Which pipeline approach is used in Jenkins as a best practice?

Using a shell script within Jenkins Pipeline can help simplify builds by combining multiple steps into a single stage. The shell script also allows users to add or update commands without having to modify each step or stage separately.

What is the difference between declarative and scripted pipeline?

Basically, declarative and scripted pipelines differ in terms of the programmatic approach. One uses a declarative programming model and the second uses an imperative programming mode. Declarative pipelines break down stages into multiple steps, while in scripted pipelines there is no need for this.

What is NonCPS in Jenkins?

The @NonCPS annotation is useful when you have methods which use objects which aren't serializable. Normally, all objects that you create in your pipeline script must be serializable (the reason for this is that Jenkins must be able to serialize the state of the script so that it can be paused and stored on disk).


1 Answers

Try changing ${colors.color[0].@primary} to ${colors.color[0]['@primary']}

like image 104
yong Avatar answered Sep 28 '22 19:09

yong