2. Job DSL Examples

2.1. About Job DSL Syntax

  • job - only required expression

job('my-job-name')

2.2. 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'
}

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

2.4. Folder

folder(String name, Closure closure = null)
folder('project-a')
freeStyleJob('project-a/compile')
listView('project-a/pipeline')
folder('project-a/testing')

2.5. Config

configFiles(Closure configFilesClosure = null)

2.6. Queue

queue(String jobName)
queue(Job job)

2.7. Reading from workspace

InputStream streamFileFromWorkspace(String filePath)
String readFileFromWorkspace(String filePath)
String readFileFromWorkspace(String jobName, String filePath)
job('my-build') {
    steps {
        shell(readFileFromWorkspace('build.sh'))
    }
}
job('my-tests') {
    description(readFileFromWorkspace('acme-tests', 'README.txt'))
}

2.8. 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!')

2.9. Configure

job('my-job') {
    ...
    configure { project ->
        project / buildWrappers / EnvInjectPasswordWrapper {
            injectGlobalPasswords(true)
        }
    }
}

2.10. Examples

2.10.1. Example 1

job('my-job') {
    scm {
        git('https://github.com/AstroTech/ecosystem-example-java.git')
    }
    triggers {
        scm('H/15 * * * *')
    }
    steps {
        maven('-e clean test')
    }
}

2.10.2. Example 2

def project = 'AstroTech/ecosystem-example-java'
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}")
        }
    }
}

2.10.3. Example 3

def giturl = 'https://github.com/AstroTech/ecosystem-example-java.git'

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

2.11. Further Reading

2.12. Assignments

2.12.1. Jenkinsfile and Pipeline DSL

  1. Przepisz całą konfigurację wykorzystując Pipeline DSL zapisany w Jenkinsfile