简体   繁体   English

扩展 Jenkins Groovy DSL

[英]Extending the Jenkins Groovy DSL

How can I add/edit new code to my Jenkins instance that would be accesible in a DSL script?如何向我的 Jenkins 实例添加/编辑可以在 DSL 脚本中访问的新代码? Context follows上下文如下

I've inherited a Jenkins instance.我继承了一个 Jenkins 实例。 Part of this inheritance includes spending the night in a haunted house writing some new automation in groovy via the Jobs DSL plugin . inheritance 的一部分包括 在鬼屋 过夜,通过Jobs DSL 插件在 groovy 中编写一些新的自动化。 Since I'm fearful of ruining our jenkins instance, my first step is setting up a local development instance.因为我害怕破坏我们的 jenkins 实例,所以我的第一步是建立一个本地开发实例。

I'm having trouble running one of our existing DSL Scripts on my local development instance -- my builds on the local server fail with the following in the Jenkins error console.我在本地开发实例上运行我们现有的DSL 脚本之一时遇到问题——我在本地服务器上的构建失败,并在 Jenkins 错误控制台中显示以下内容。

Processing DSL script jobs.groovy
ERROR: startup failed:
jobs.groovy: 1: unable to resolve class thecompanysname.jenkins.extensions

The script in question starts off like this.有问题的脚本是这样开始的。

import thecompanysname.jenkins.extensions

use(extensions) {
  def org = 'project-name'
  def project = 'test-jenkins-repo'
  def _email = 'foo@example.com'

So, as near I can tell, it seems like a predecesor has written some custom Groovy code that they're importing所以,据我所知,似乎前任已经编写了一些他们正在导入的自定义 Groovy 代码

import thecompanysname.jenkins.extensions

What's not clear to me is我不清楚的是

  1. Where this code lives此代码所在的位置
  2. How I can find it in our real Jenkins instance我如何在我们真正的 Jenkins 实例中找到它
  3. How I can add to to my local instance如何添加到我的本地实例

Specific answers are welcome, as our here's how you can learn to fish answers.欢迎提供具体答案,因为我们在这里介绍了如何学习钓鱼答案。

While there may be other ways to accomplish this, after a bit of poking around I discovered虽然可能有其他方法可以做到这一点,但经过一番探索后,我发现

  1. The Jenkins instance I've installed has an older version of the Jobs DSL plugin installed.我安装的 Jenkins 实例安装了旧版本的Jobs DSL 插件

  2. This version of the Jobs DSL plugin allowed you to set an additional classpath in your Process DSL Builds job section that pointed to additional jar files.这个版本的 Jobs DSL 插件允许您在Process DSL Builds作业部分设置一个额外的classpath ,该类路径指向额外jar文件。

  3. These jar files can give you access to additional classes in your groovy scripts (ie thecompanysname.jenkins.extensions )这些 jar 文件可以让您访问 groovy 脚本中的其他类(即公司名称thecompanysname.jenkins.extensions

Unfortunately, more recent versions of the Jobs DSL plugin have removed this option, and it's not clear if it's possible to add it back.不幸的是,最新版本的Jobs DSL 插件已经删除了这个选项,目前还不清楚是否可以重新添加它。 That, however, is another question.然而,这是另一个问题。

Configure Global Security -> uncheck "Enable script security for Job DSL scripts".配置全局安全 -> 取消选中“为作业 DSL 脚本启用脚本安全”。
works for me为我工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM