使用 Jenkins Job-DSL 配置块在特定位置放置自定义步骤 [英] Using Jenkins Job-DSL Configure block to place custom steps in particular positions

查看:27
本文介绍了使用 Jenkins Job-DSL 配置块在特定位置放置自定义步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 job-dsl-plugin,我尝试编写大量以前手动配置的 Jenkins 作业的配置脚本.

Using the job-dsl-plugin I am attempting to script the configuration of a fair number of Jenkins jobs which have previously been configured manually.

这些作业的一种有多个步骤,包括使用 XShell 插件的几个步骤,job-dsl 不直接支持.但是我应该能够通过使用自定义的配置"块来解决这个问题.

One flavour of these jobs has multiple steps including a couple which use the XShell plugin, this is not directly supported by job-dsl. However I should be able to get around that by using a custom "configure" block.

使用 http://job-dsl.herokuapp.com/ 我已经做到了:

Using the "Job DSL playground" at http://job-dsl.herokuapp.com/ I've got as far as:

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.我寻求的输出看起来像:

If I just include the first configure block only, I do get the first command at the first step position. But with the second (last) configure block present, the "node / builders" is matching on the first element again and overwriting it, so run-me-as-the-last-step is the first and only XShellBuilder. The output I seek would look something like:

    <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>

我无法弄清楚将第二个块插入为最后一个孩子的 Groovy XML/Job-DSL 语法;Job-DSL 或 Groovy XMLParser 专家能否请给我一个关于如何匹配和插入任意位置的指针"?

I can't figure out the Groovy XML / Job-DSL syntax to insert that second block as "last child; can a Job-DSL or Groovy XMLParser expert please give me a pointer on how to match and insert at an arbitrary position in the children of <builders>?

(我很感激我可以将 job(type:Maven)preBuildStepspostBuildSteps 一起使用,但实际上我还需要一些其他的东西那里也是一个纯粹的 maven 工作所排除的.)谢谢!

(I appreciate that I could use job(type:Maven) with preBuildSteps and postBuildSteps but actually I need a few other things in there as well which a pure maven job precludes.) Thanks!

推荐答案

您可以使用 << 操作符来追加节点,否则具有相同名称的现有节点将被替换.有关详细信息,请参阅 Job DSL wiki.

You can use the << operator to append nodes, otherwise an existing node with an identical name gets replaced. See the Job DSL wiki for details.

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')
    }
  }
}

这篇关于使用 Jenkins Job-DSL 配置块在特定位置放置自定义步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆