Search code examples
jenkinsgroovyxml-parsingjenkins-pipelinejenkins-groovy

How to get HTML Table Value created in Jenkins


I have a situation where I have created the html table for Jenkins Parameter which looks like below and I want to get the value in the Desired Count column for each component if its NOT equal to Current count or if it is null/zero.

For example, if I change Desired Count of A1 to 2, then my Jenkins parameter should return that A1=2 when I build the job so I can update that to proceed further steps in my Jenkins pipeline. Could somebody help

My Table looks like this

<html>
    <head>
        <style>
            table, th, td {  
                border: 1px solid black;  
                text-align: center; 
                width: 40%; 
                border-collapse: collapse;
                }
                </style>
                </head>
                <table>
                    <tr>
                        <th>COMPONENT</th>
                        <th>CURRENT SCALE</th> 
                        <th>DESIRED SCALE</th>
                    </tr>
                    <tr>
                        <td>A1</td>
                        <td>-1</td>
                        <td><input type='number' step='1' min='0' max='5'/></td>
                    </tr>
                    <tr>
                        <td>B1</td>
                        <td>1</td>
                        <td><input type='number' step='1' min='0' max='5'/></td>
                    </tr>
                    <tr>
                        <td>C1</td>
                        <td>-1</td>
                        <td><input type='number' step='1' min='0' max='5'/></td>
                    </tr>
                </table>
             </html>

enter image description here


Solution

  • Check the following Groovy code.

    def text = '''
        <html>
        <head>
            <style>
                table, th, td {  
                    border: 1px solid black;  
                    text-align: center; 
                    width: 40%; 
                    border-collapse: collapse;
                    }
                    </style>
                    </head>
                    <table>
                        <tr>
                            <th>COMPONENT</th>
                            <th>CURRENT SCALE</th> 
                            <th>DESIRED SCALE</th>
                        </tr>
                        <tr>
                            <td>A1</td>
                            <td>-1</td>
                            <td><input type='number' step='1' min='0' max='5'>1234321</input></td>
                        </tr>
                        <tr>
                            <td>B1</td>
                            <td>1</td>
                            <td><input type='number' step='1' min='0' max='5'>555</input></td>
                        </tr>
                        <tr>
                            <td>C1</td>
                            <td>-1</td>
                            <td><input type='number' step='1' min='0' max='5'/></td>
                        </tr>
                    </table>
                 </html>
    '''
    
    def html = new XmlParser().parseText(text)
    println html.table
    
    def values = [:]
    html.table.tr.each{ row -> 
      
      if(row['td']) {
        if(row['td'][2].input.text()) {
          values[row['td'][0].text()] = row['td'][2].input.text()
        }  
      }
      
    }
    
    println values
    

    Output

    [A1:1234321, B1:555]
    

    Update

    Assuming your Active Choice Parameter name is versions you can do something like the below to get the parameters. You may have to reply on the order of the inputs.

    pipeline {
        agent any
    
        stages {
            stage('Hello') {
                steps {
                    script {
                        def versionParams = params.versions.split(',')
                        A1=versionParams[0]
                        B1=versionParams[1]
                        C1=versionParams[2]
                        println "A1: " + A1
                        println "B1: " + B1
                        println "C1: " + C1
                    }
                }
            }
        }
    }
    

    Also, your HTML content inputs should be tweeked like below.

    <html>
        <head>
            <style>
                table, th, td {  
                    border: 1px solid black;  
                    text-align: center; 
                    width: 40%; 
                    border-collapse: collapse;
                    }
                    </style>
                    </head>
                    <table>
                        <tr>
                            <th>COMPONENT</th>
                            <th>CURRENT SCALE</th> 
                            <th>DESIRED SCALE</th>
                        </tr>
                        <tr>
                            <td>A1</td>
                            <td>-1</td>
                            <td><input class="setting-input" name='value' type='number' step='1' min='0' max='5'/></td>
                        </tr>
                        <tr>
                            <td>B1</td>
                            <td>1</td>
                            <td><input class="setting-input" name='value' type='number' step='1' min='0' max='5'/></input></td>
                        </tr>
                        <tr>
                            <td>C1</td>
                            <td>-1</td>
                            <td><input class="setting-input" name='value'  type='number' step='1' min='0' max='5'/></td>
                        </tr>
                    </table>
                 </html>