使用 Jenkins Job-DSL 配置块将自定义步骤放置在特定位置

2023-11-26

我正在尝试使用 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但实际上我还需要一些其他的东西,这是纯粹的行家工作所无法做到的。) 谢谢!


您可以使用<<运算符追加节点,否则具有相同名称的现有节点将被替换。请参阅工作 DSL 维基了解详情。

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')
    }
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Jenkins Job-DSL 配置块将自定义步骤放置在特定位置 的相关文章

随机推荐