【问题标题】:Using Jenkins Job-DSL Configure block to place custom steps in particular positions使用 Jenkins Job-DSL 配置块将自定义步骤放置在特定位置
【发布时间】:2015-02-16 18:59:19
【问题描述】:

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

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

(我很欣赏我可以将job(type:Maven)preBuildStepspostBuildSteps 一起使用,但实际上我还需要一些其他的东西,而纯粹的maven 工作排除了这些东西。) 谢谢!

【问题讨论】:

    标签: groovy jenkins jenkins-job-dsl


    【解决方案1】:

    您可以使用&lt;&lt; 运算符来附加节点,否则将替换具有相同名称的现有节点。详情请见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')
        }
      }
    }
    

    【讨论】:

    • 知道了,配置必须在步骤之外,谢谢
    猜你喜欢
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    相关资源
    最近更新 更多