How to convert jenkins config.xml configuration to YAML format in python to use jenkins-job-builder?

jenkins-job-builder is a good tool to help me maintain jobs in files YAML. see the example in the configuration chapter .

Now that I had many old jenkins jobs, it would be nice to have a python script xml2yamlto convert the existing jenkins file format config.xmlto YAML.

Do you have any suggestions for a quick solution in python?

I don't need it to be used in jenkins-job-builderdirectly, it just can be converted to YAML for reference.

For conversion, the part can be ignored as a namespace.

Segment

config.xml as follows:

<project>
  <logRotator class="hudson.tasks.LogRotator">
    <daysToKeep>-1</daysToKeep>
    <numToKeep>20</numToKeep>
    <artifactDaysToKeep>-1</artifactDaysToKeep>
    <artifactNumToKeep>-1</artifactNumToKeep>
  </logRotator>
  ...
</project>

YAML :

- project:
   logrotate:
     daysToKeep: -1
     numToKeep: 20
     artifactDaysToKeep: -1
     artifactNumToKeep: -1

config.xml jenkins, infra_backend-merge-all-repo https://ci.jenkins-ci.org

+4
3

, , , :

Python XML. , - , -. . XML python python.

, project, , , yaml.

from xml.dom.minidom import parse

def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)

def getTextForTag(nodelist,tag):
    elements = nodelist.getElementsByTagName(tag)
    if (elements.length>0):
        return getText( elements[0].childNodes)
    return ''

def printValueForTag(parent, indent, tag, valueName=''):
    value = getTextForTag( parent,tag)
    if (len(value)>0):
        if (valueName==''):
            valueName = tag
        print indent + valueName+": "+value

def emitLogRotate(indent, rotator):
    print indent+"logrotate:"
    indent+='  '
    printValueForTag( rotator,indent, 'daysToKeep')
    printValueForTag( rotator,indent, 'numToKeep')  
def emitProject(project):
    print "- project:"
    # all projects have log rotators, so no need to chec
    emitLogRotate("   ",project.getElementsByTagName('logRotator')[0])
    # next section...

dom = parse('config.xml')
emitProject(dom)

, . , , - . , " ", , DOM python.

+2

, XML YAML. Jenkins YAML.

https://github.com/ktdreyer/jenkins-job-wrecker

. / , , XML, .

+2

, . XML. XMLStarlet - - XPath .

"xmlstarlet el" XML XPath.

"xmlstarlet sel -t -c XPath-expression" , .

Perhaps you want to spend an hour (or two) innovating your XPath know-how in advance.

You will lose a couple of tears as soon as you find out how much time you spent programming XML access before using XMLStarlet.

+1
source

Source: https://habr.com/ru/post/1523697/


All Articles