Parsing an XML file within a Jenkins pipeline

折月煮酒 提交于 2019-12-03 15:11:10
Kirill

You could use XmlSlurper, it works for me.

def xmlText = new XmlSlurper().parse(MyURL)
xmlText.data.artifact.each {******

As branch is the root element, you don't need to explicitly specify it when accessing your parsed nodes

Try changing

println buildPlan.branch

To

println buildPlan.stage

To print out the stage nodes

A @NonCPS method should only accept or return Serializable types. Try returning .branch from the method.

In the end I think my approach was wrong: I decided to convert the XML file into a separate groovy script and load it within the pipeline

Update: Recently people started editing my answer for clarity, but the fact is that I just ditched storing my configuration in XML files and opted for groovy scripts, which gave me more flexibility. I understand it may not be a common practice, but it suits my needs.

For example - instead of:

config.xml:
<settings>
  <floopi>2</floopi>
</settings>

I used:

config.groovy:
def call() {[
  floopi: 2
]}
return this

And in the pipeline script:

stage('init') {
    def settings = load('config.groovy')()
    echo "floopi: ${settings.floopi}"
}

I hope that's a better answer :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!