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

查看:110
本文介绍了使用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插件的一对,这不是由作业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/ 我还有:

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-我作为最后一步是第一个也是唯一的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专家给我一个关于如何匹配和插入< builders>

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) preBuildSteps code>和 postBuildSteps 但实际上我还需要一些其他的东西,以及纯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!

推荐答案

您可以使用< 运算符来追加节点,具有相同名称的节点将被替换。请参阅作业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天全站免登陆