Real World Examples

Daniel Spilker edited this page Jan 26, 2018 · 24 revisions

This page shows how to use Groovy features in the DSL for advanced scripting.


def jobName = 'example'

job(jobName) {


def giturl = ''
for(i in 0..10) {
    job("DSL-Tutorial-1-Test-${i}") {
        scm {
        steps {
            maven("test -Dtest.suite=${i}")

Be aware of problems with the Groovy SDK loop methods.

Multi-line strings

def viewspec = '''
//depot/Tools/build/... //jryan_car/Tools/build/...
//depot/commonlibraries/utils/... //jryan_car/commonlibraries/utils/...
//depot/helloworld/... //jryan_car/helloworld/...

job('PerforceJob') {
    scm {

REST API calls

def project = 'Netflix/asgard'
def branchApi = new URL("${project}/branches")
def branches = new groovy.json.JsonSlurper().parse(branchApi.newReader())
branches.each {
    def branchName =
    def jobName = "${project}-${branchName}".replaceAll('/','-')
    job(jobName) {
        scm {
            git("${project}.git", branchName)

Import other files (i.e. with class definitions) into your script

Make a directory at the same level as the DSL called utilities and create a file called MyUtilities.groovy in the utilities directory with the following contents:

package utilities

class MyUtilities {
    static void addMyFeature(def job) {
        job.with {
            description('Arbitrary feature')

Then from the DSL, add something like this:

import utilities.MyUtilities

def myJob = job('example')

Note that importing other files is not possible when Script Security is enabled and not using Groovy Sandbox.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.