Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-48875

No closing tag while building xml file with MarkupBuilder in Pipeline Job

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Critical
    • Resolution: Fixed
    • Component/s: workflow-cps-plugin
    • Labels:
    • Environment:
      Jenkins 2.89.2
      Pipeline: Groovy 2.41
      Pipeline 2.5

      Windows Server 2012 R2
    • Similar Issues:

      Description

      I want to use Groovy in my Jenkins-Pipeline-Job to build a Statuslist of my Jenkins Slaves and the installed nodes. Therefore I have some lists which should be displayed in xml. While testing with IntelliJ i wrote the following code.

       

       

       

      import groovy.xml.MarkupBuilder
      
      def listNodeNames = []
      listNodeNames << 'SLAVE1_NODE_1'
      listNodeNames << 'SLAVE1_NODE_2'
      listNodeNames << 'SLAVE2_NODE_1'
      listNodeNames << 'SLAVE2_NODE_2'
      
      
      def listComputerNames = []
      listComputerNames << 'SLAVE1'
      listComputerNames << 'SLAVE2'
      
      def stringWriter = new StringWriter()
      def xmlBuilder = new MarkupBuilder(stringWriter)
      
      xmlBuilder.computers {
          for (i = 0; i < listComputerNames.size(); i++){
      
              xmlBuilder.computer {
                  xmlBuilder.computerName("${listComputerNames[i]}")
                  xmlBuilder.computerStatus("online")
      
                  for (j = 0; j < listNodeNames.size(); j++){
                      if (listNodeNames[j].contains(listComputerNames[i])){
                          xmlBuilder.slave {
                              xmlBuilder.slaveName("${listNodeNames[j]}")
                              xmlBuilder.slaveStatus('test')
                              xmlBuilder.slaveReason('test')
                          }
                      }
                  }
              }
      
          }
      }
      
      def xml = stringWriter.toString()
      println xml
      

      As long as i run the code in IntelliJ, everything works fine and i get a valid xml file:

       

      <computers>
            <computer>
              <computerName>SLAVE1</computerName>
              <computerStatus>online</computerStatus>
              <slave>
                <slaveName>SLAVE1_NODE1</slaveName>
                <slaveStatus>test</slaveStatus>
                <slaveReason>test</slaveReason>
              </slave>
              <slave>
                <slaveName>SLAVE1_NODE2</slaveName>
                <slaveStatus>test</slaveStatus>
                <slaveReason>test</slaveReason>
              </slave>
            </computer>
            <computer>
              <computerName>SLAVE2</computerName>
              <computerStatus>online</computerStatus>
              <slave>
                <slaveName>SLAVE2_NODE1</slaveName>
                <slaveStatus>test</slaveStatus>
                <slaveReason>test</slaveReason>
              </slave>
              <slave>
                <slaveName>SLAVE2_NODE2</slaveName>
                <slaveStatus>test</slaveStatus>
                <slaveReason>test</slaveReason>
              </slave>
            </computer>
          </computers>
      

       

      But if i execute the same script on my Jenkins instance most closing tags are missing:

      <computers>
        <computer>
          <computerName>SLAVE1</computerName>
          <computerStatus>online</computerStatus>
          <slave>
            <slaveName>SLAVE1_NODE1</slaveName>
            <slaveStatus>test</slaveStatus>
            <slaveReason>test</slaveReason>
            <slave>
              <slaveName>SLAVE1_NODE2</slaveName>
              <slaveStatus>test</slaveStatus>
              <slaveReason>test</slaveReason>
              <computer>
                <computerName>SLAVE2</computerName>
                <computerStatus>online</computerStatus>
                <slave>
                  <slaveName>SLAVE2_NODE1</slaveName>
                  <slaveStatus>test</slaveStatus>
                  <slaveReason>test</slaveReason>
                  <slave>
                    <slaveName>SLAVE2_NODE2</slaveName>
                    <slaveStatus>test</slaveStatus>
                    <slaveReason>test</slaveReason>
      

       

        Attachments

          Activity

          Hide
          abayer Andrew Bayer added a comment -

          I believe this is because MarkupBuilder is calling the closures outside of the CPS context, so they don't work quite right. I don't think there's much we can do about this - you should probably move to calling MarkupBuilder within a method annotated with @NonCPS.

          Show
          abayer Andrew Bayer added a comment - I believe this is because MarkupBuilder is calling the closures outside of the CPS context, so they don't work quite right. I don't think there's much we can do about this - you should probably move to calling MarkupBuilder within a method annotated with @NonCPS .
          Hide
          rkimble Arno Nym added a comment -

          Andrew Bayer Thank you! The CPS context was the right guess, After capsuling the Markup Builder in a nonCPS marked method everything works fine 

          @nonCPS
          def buildXml(list1, list2){
              //SOMETHING
          }
          Show
          rkimble Arno Nym added a comment - Andrew Bayer Thank you! The CPS context was the right guess, After capsuling the Markup Builder in a nonCPS marked method everything works fine  @nonCPS def buildXml(list1, list2){ //SOMETHING }
          Hide
          rkimble Arno Nym added a comment -
          @nonCPS
          def buildXml(list1, list2){
              //SOMETHING
          }
          

          Capsuling the build step inside a nonCPS marked method resolves the issue

          Show
          rkimble Arno Nym added a comment - @nonCPS def buildXml(list1, list2){ //SOMETHING } Capsuling the build step inside a nonCPS marked method resolves the issue

            People

            • Assignee:
              rkimble Arno Nym
              Reporter:
              rkimble Arno Nym
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: