我有一些看起来像这样的jobDsl:
multibranchPipelineJob('foo/bar') {
branchSources {
git {
remote('https://github.com/jenkinsci/job-dsl-plugin.git')
credentialsId('github-ci')
includes('JENKINS-*')
}
}
}
生成以下xml(代码段):
<!-- 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>
我想做的就是通过configure块将一个节点(称为traits
)附加到jobDsl multibranchJob api创建的jenkins.branch.BranchSource
节点上:
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')
}
}
}
}
但是,根据https://job-dsl.herokuapp.com/,上述dsl会这样创建xml:
<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>
基本上,是否有任何方法可以附加到通过BranchSource
调用创建的branchSources
节点上?
答案 0 :(得分:1)
参加聚会有点晚,但遇到了同样的问题,这让我发疯...看起来将配置块移动到管道定义的末尾会有所帮助 - 也使用正确的 XPath 工具。
Fred 的博文让我找到了正确的方向。去看看:https://ftclausen.github.io/infra/jenkins/jenkins-jobdsl-git-traits/
管道定义
multibranchPipelineJob('foo/bar') {
branchSources {
git {
id("foobar")
remote('https://github.com/jenkinsci/job-dsl-plugin.git')
credentialsId('github-ci')
includes('JENKINS-*')
}
}
configure {
def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits {
traits << 'jenkins.plugins.git.traits.LocalBranchTrait'{}
}
}
}
结果 config.xml
<sources class='jenkins.branch.MultiBranchProject$BranchSourceList'>
<data>
<jenkins.branch.BranchSource>
<source class='jenkins.plugins.git.GitSCMSource'>
<id>foobar</id>
<remote>https://github.com/jenkinsci/job-dsl-plugin.git</remote>
<credentialsId>github-ci</credentialsId>
<includes>JENKINS-*</includes>
<excludes></excludes>
<ignoreOnPushNotifications>false</ignoreOnPushNotifications>
<traits>
<jenkins.plugins.git.traits.LocalBranchTrait></jenkins.plugins.git.traits.LocalBranchTrait>
</traits>
</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>
答案 1 :(得分:0)
问题在于configure
上下文中没有branchSources
方法。因此,将调用外部上下文中的configure
方法。在这种情况下,来自multibranchPipelineJob
上下文。由于branchSources
方法在调用configure
方法时尚未完成,因此尚未生成任何分支源节点,因此configure块将创建一个新节点。
尝试以下方法:
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')
}
}
}