使用 job-dsl-plugin 我试图编写大量以前手动配置的 Jenkins 作业的配置脚本。
这些作业的一种有多个步骤,包括使用 XShell 插件的几个步骤,job-dsl 不直接支持。但是,我应该能够通过使用自定义的“配置”块来解决这个问题。
在 http://job-dsl.herokuapp.com/ 使用“作业 DSL 游乐场”我已经到了:
job {
name 'my-job'
jdk('JDK-17')
steps {
configure { node ->
node / builders {
'hudson.plugins.xshell.XShellBuilder'(plugin: 'xshell@0.9') {
commandLine('run-me-as-the-first-build-step')
executeFromWorkingDir('true')
}
}
}
maven {
mavenInstallation('Default')
goals('clean')
goals('verify')
property('prop1', 'value1')
property('user.timezone', 'UTC')
mavenOpts('--batch-mode')
}
maven {
mavenInstallation('Default')
goals('deploy')
property('prop2', 'value2')
property('user.timezone', 'UTC')
mavenOpts('--batch-mode')
}
shell('shell-task')
configure { node ->
node / builders {
'hudson.plugins.xshell.XShellBuilder'(plugin: 'xshell@0.9') {
commandLine('run-me-as-the-last-build-step')
executeFromWorkingDir('true')
}
}
}
}
}
如果我只包含第一个配置块,我会在第一步位置得到第一个命令。但是在存在第二个(最后一个)配置块时,
"node / builders"
再次匹配第一个元素并覆盖它,所以 run-me-as-the-last-step
是第一个也是唯一的 XShellBuilder。我寻求的输出看起来像: <project>
...
<builders>
<hudson.plugins.xshell.XShellBuilder plugin='xshell@0.9'>
<commandLine>run-me-as-the-first-build-step</commandLine>
<executeFromWorkingDir>true</executeFromWorkingDir>
</hudson.plugins.xshell.XShellBuilder>
<hudson.tasks.Maven>
<targets>clean verify</targets>
<properties>prop1=value1
user.timezone=UTC</properties>
<mavenName>Default</mavenName>
<jvmOptions>--batch-mode</jvmOptions>
<usePrivateRepository>false</usePrivateRepository>
</hudson.tasks.Maven>
<hudson.tasks.Maven>
<targets>deploy</targets>
<properties>prop2=value2
user.timezone=UTC</properties>
<mavenName>Default</mavenName>
<jvmOptions>--batch-mode</jvmOptions>
<usePrivateRepository>false</usePrivateRepository>
</hudson.tasks.Maven>
<hudson.tasks.Shell>
<command>shell-task</command>
</hudson.tasks.Shell>
<hudson.plugins.xshell.XShellBuilder plugin='xshell@0.9'>
<commandLine>run-me-as-the-last-build-step</commandLine>
<executeFromWorkingDir>true</executeFromWorkingDir>
</hudson.plugins.xshell.XShellBuilder>
</builders>
...
</project>
我无法弄清楚将第二个块作为“最后一个 child ”插入的 Groovy XML/Job-DSL 语法;Job-DSL 或 Groovy XMLParser 专家能否请给我一个关于如何匹配和插入到
<builders>
的 child ?(我很感激我可以将
job(type:Maven)
与 preBuildSteps
和 postBuildSteps
一起使用,但实际上我还需要一些其他的东西,这是纯 maven 工作所排除的。)谢谢!
最佳答案
您可以使用 <<
运算符来附加节点,否则具有相同名称的现有节点将被替换。有关详细信息,请参阅 Job DSL wiki。
job {
name('foo')
steps {
shell('echo AAA')
}
configure {
it / builders << 'hudson.plugins.xshell.XShellBuilder' {
commandLine('123')
}
}
steps {
shell('echo BBB')
}
configure {
it / builders << 'hudson.plugins.xshell.XShellBuilder' {
commandLine('456')
}
}
}
关于groovy - 使用 Jenkins Job-DSL 配置 block 在特定位置放置自定义步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28548472/