我正在尝试使用 job-dsl-plugin 编写之前手动配置的大量 Jenkins 作业的配置脚本。
这些作业的一种风格有多个步骤,其中包括使用 XShell 插件的几个步骤,job-dsl 不直接支持这一点。不过,我应该能够通过使用自定义“配置”块来解决这个问题。
使用“Job DSL Playground”:http://job-dsl.herokuapp.com/我已经做到了:
job {
name 'my-job'
jdk('JDK-17')
steps {
configure { node ->
node / builders {
'hudson.plugins.xshell.XShellBuilder'(plugin: '[email protected]') {
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: '[email protected]') {
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='[email protected]'>
<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='[email protected]'>
<commandLine>run-me-as-the-last-build-step</commandLine>
<executeFromWorkingDir>true</executeFromWorkingDir>
</hudson.plugins.xshell.XShellBuilder>
</builders>
...
</project>
我无法弄清楚 Groovy XML / Job-DSL 语法来将第二个块插入为“最后一个子块;Job-DSL 或 Groovy XMLParser 专家能否请给我一个关于如何匹配和插入到中的任意位置的指针”的孩子<builders>
?
(我很感激我可以使用job(type:Maven)
with preBuildSteps
and postBuildSteps
但实际上我还需要一些其他的东西,这是纯粹的行家工作所无法做到的。)
谢谢!