Search code examples
jenkinsgroovy

Extending the Jenkins Groovy DSL


How can I add/edit new code to my Jenkins instance that would be accesible in a DSL script? Context follows

I've inherited a Jenkins instance. Part of this inheritance includes spending the night in a haunted house writing some new automation in groovy via the Jobs DSL plugin. Since I'm fearful of ruining our jenkins instance, my first step is setting up a local development instance.

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.

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 = '[email protected]'

So, as near I can tell, it seems like a predecesor has written some custom Groovy code that they're importing

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


Solution

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

    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.

    3. These jar files can give you access to additional classes in your groovy scripts (i.e. 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. That, however, is another question.