3. Jenkins Job DSL

3.1. Podstawy składni Job DSL

Jedyne wymagane to nazwa Job:

DSL Methods:
job('my-job')
Job:
job(String name, Closure closure = null)
freeStyleJob(String name, Closure closure = null)
buildFlowJob(String name, Closure closure = null)
ivyJob(String name, Closure closure = null)
matrixJob(String name, Closure closure = null)
mavenJob(String name, Closure closure = null)
multiJob(String name, Closure closure = null)
workflowJob(String name, Closure closure = null)
multibranchWorkflowJob(String name, Closure closure = null)
def myJob = freeStyleJob('SimpleJob')
myJob.with {
    description 'A Simple Job'
}
View:
listView(String name, Closure closure = null)
sectionedView(String name, Closure closure = null)
nestedView(String name, Closure closure = null)
deliveryPipelineView(String name, Closure closure = null)
buildPipelineView(String name, Closure closure = null)
buildMonitorView(String name, Closure closure = null)
categorizedJobsView(String name, Closure closure = null)
Folder:
folder(String name, Closure closure = null)
folder('project-a')
freeStyleJob('project-a/compile')
listView('project-a/pipeline')
folder('project-a/testing')
Config:
configFiles(Closure configFilesClosure = null)
Queue:
queue(String jobName)
queue(Job job)
Reading from workspace:
 
InputStream streamFileFromWorkspace(String filePath)
String readFileFromWorkspace(String filePath)
String readFileFromWorkspace(String jobName, String filePath)
job('example') {
    steps {
        shell(readFileFromWorkspace('build.sh'))
    }
}

job('acme-tests') {
    description(readFileFromWorkspace('acme-tests', 'README.txt'))
}
Logging:
out.println('Hello from a Job DSL script!')
println('Hello from a Job DSL script!')
import java.util.logging.Logger

Logger logger = Logger.getLogger('org.example.jobdsl')
logger.info('Hello from a Job DSL script!')
Confiugure:
job('example') {
    ...
    configure { project ->
        project / buildWrappers / EnvInjectPasswordWrapper {
            injectGlobalPasswords(true)
        }
    }
}

3.2. Przykłady Job DSL

job('DSL-Tutorial-1-Test') {
    scm {
        git('git://github.com/quidryan/aws-sdk-test.git')
    }
    triggers {
        scm('H/15 * * * *')
    }
    steps {
        maven('-e clean test')
    }
}
def project = 'quidryan/aws-sdk-test'
def branchApi = new URL("https://api.github.com/repos/${project}/branches")
def branches = new groovy.json.JsonSlurper().parse(branchApi.newReader())

branches.each {
    def branchName = it.name
    def jobName = "${project}-${branchName}".replaceAll('/','-')

    job(jobName) {
        scm {
            git("git://github.com/${project}.git", branchName)
        }
        steps {
            maven("test -Dproject.name=${project}/${branchName}")
        }
    }
}
def giturl = 'https://github.com/quidryan/aws-sdk-test.git'

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