How to append to node created by Jenkins job dsl in same configure block?

我们两清 提交于 2021-02-10 18:48:48

问题


I have some jobDsl that looks like this:

multibranchPipelineJob('foo/bar') {
        branchSources {
        git {
            remote('https://github.com/jenkinsci/job-dsl-plugin.git')
            credentialsId('github-ci')
            includes('JENKINS-*')
        }
    }
}

which generates the following xml (snippet):

<!-- 1. foo/bar -->
<org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject>
    <sources class='jenkins.branch.MultiBranchProject$BranchSourceList'>
        <data>
            <jenkins.branch.BranchSource>
                <source class='jenkins.plugins.git.GitSCMSource'>
                    <id>d45cd641-7223-4b58-9de5-837c3fe584a7</id>
                    <remote>https://github.com/jenkinsci/job-dsl-plugin.git</remote>
                    <credentialsId>github-ci</credentialsId>
                    <includes>JENKINS-*</includes>
                    <excludes></excludes>
                    <ignoreOnPushNotifications>false</ignoreOnPushNotifications>
                </source>
                <strategy class='jenkins.branch.DefaultBranchPropertyStrategy'>
                    <properties class='empty-list'></properties>
                </strategy>
            </jenkins.branch.BranchSource>
        </data>
        <owner class='org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject' reference='../..'></owner>
    </sources>

What I would like to do is append a node (call it traits) to the jenkins.branch.BranchSource node created by the jobDsl multibranchJob api via the configure block:

multibranchPipelineJob('foo/bar') {
        branchSources {
        git {
            remote('https://github.com/jenkinsci/job-dsl-plugin.git')
            credentialsId('github-ci')
            includes('JENKINS-*')
        }

            configure {
               def first =  it / 'sources'(class: 'jenkins.branch.MultiBranchProject$BranchSourceList') / 'data' / 'jenkins.branch.BranchSource'
                first << traits {
                    foo('bar')
                }
            }
    }
}

However, according to https://job-dsl.herokuapp.com/, the above dsl would create the xml as such:

<sources class='jenkins.branch.MultiBranchProject$BranchSourceList'>
    <data>
        <jenkins.branch.BranchSource>
            <traits>
                <foo>bar</foo>
            </traits>
        </jenkins.branch.BranchSource>
        <jenkins.branch.BranchSource>
            <source class='jenkins.plugins.git.GitSCMSource'>
                <id>7fd47865-fffa-4f8f-98f1-ac6de65249f7</id>
                <remote>https://github.com/jenkinsci/job-dsl-plugin.git</remote>
                <credentialsId>github-ci</credentialsId>
                <includes>JENKINS-*</includes>
                <excludes></excludes>
                <ignoreOnPushNotifications>false</ignoreOnPushNotifications>
            </source>
            <strategy class='jenkins.branch.DefaultBranchPropertyStrategy'>
                <properties class='empty-list'></properties>
            </strategy>
        </jenkins.branch.BranchSource>
    </data>
</sources>

Basically, is there any way to append to the BranchSource node that is created from the branchSources call?


回答1:


The problem is that there is no configure method within the branchSources context. So the configure method from an outer context will be called. In this case from the multibranchPipelineJob context. Since the branchSources method has not completed by the time the configure method is called, no branch source node have been generated and thus the configure block will create a new node.

Try this instead:

multibranchPipelineJob('foo/bar') {
  branchSources {
    git {
      remote('https://github.com/jenkinsci/job-dsl-plugin.git')
      credentialsId('github-ci')
      includes('JENKINS-*')
    }
  }
  configure {
    def source =  it / 'sources'(class: 'jenkins.branch.MultiBranchProject$BranchSourceList') / 'data' / 'jenkins.branch.BranchSource'
    source << traits {
      foo('bar')
    }
  }
}


来源:https://stackoverflow.com/questions/53825744/how-to-append-to-node-created-by-jenkins-job-dsl-in-same-configure-block

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