脚本控制台中的 Jenkins 作业执行

Jenkins job execution in script consol

我无法在 jenkins 脚本控制台中执行以下脚本。

job('testjob') {
  using('template_job')
  configure { node ->
    node / scm / branches / 'hudson.plugins.git.BranchSpec' / name('main')
  }
}

我低于错误

groovy.lang.MissingMethodException: No signature of method: Script1.job() is applicable for argument types: (java.lang.String, Script1$_run_closure1) values: [testjob, Script1$_run_closure1@4eaafbe6]
Possible solutions: run(), run(), any(), wait(), dump(), grep()
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)

您尝试 运行 的脚本是 Job DSL 脚本。
它不打算 运行 在 Jenkins 脚本控制台 菜单中,因此你会得到那个异常,DSL 脚本应该在专用的 Process Job DSLs 中执行 构建步骤(通常在自由式作业中)。使用以下 Official Documentation Guide 开始使用 Jon DSL 插件及其使用说明。
此外,这里还有一个非常好的 Tutorial 用于使用 Job DSL 插件。

顺便说一句,你可以使用 Job DSL Playground to create and test your configure blocks, but the playground only supports the DSL API that is available in the online API Reference - as oppose to the complete DSL API reference that is available in your Jenkins installation at https://your.jenkins.installation/plugin/job-dsl/api-viewer/index.html.