An example Job DSL project that uses Gradle for building and testing. Check out this presentation for a walkthrough of this example (starts around 14:00).
.
├── jobs # DSL script files
├── resources # resources for DSL scripts
├── src
│ ├── main
│ │ ├── groovy # support classes
│ │ └── resources
│ │ └── idea.gdsl # IDE support for IDEA
│ └── test
│ └── groovy # specs
└── build.gradle # build file
- Example 1 - the simplest example: 1 job saying hello
- Example 2 - very simple Maven job
- Example 3 - the simple Maven job but for each branch
- Example 4 - the simple Maven job for each branch but by created by builder class
./gradlew test
runs the specs.
JobScriptsSpec will loop through all DSL files and make sure they don't throw any exceptions when processed.
./gradlew debugXml -Dpattern=jobs/**/*Jobs.groovy
runs the DSL and writes the XML output to files to build/debug-xml
.
This can be useful if you want to inspect the generated XML before check-in.
You can create the example seed job via the Rest API Runner (see below) using the pattern jobs/seed.groovy
.
Or manually create a job with the same structure:
- Invoke Gradle script → Use Gradle Wrapper:
true
- Invoke Gradle script → Tasks:
clean test
- Process Job DSLs → DSL Scripts:
jobs/**/*Jobs.groovy
- Process Job DSLs → Additional classpath:
src/main/groovy
- Publish JUnit test result report → Test report XMLs:
build/test-results/**/*.xml
A gradle task is configured that can be used to create/update jobs via the Jenkins REST API, if desired. Normally a seed job is used to keep jobs in sync with the DSL, but this runner might be useful if you'd rather process the DSL outside of the Jenkins environment or if you want to create the seed job from a DSL script.
This is for debug only - you MUST disable security in Jenkins (CSRF protection) which is on by default in Jenkins 2+
./gradlew rest -Dpattern=<pattern> -DbaseUrl=<baseUrl> [-Dusername=<username>] [-Dpassword=<password>]
pattern
- ant-style path pattern of files to includebaseUrl
- base URL of Jenkins serverusername
- Jenkins username, if securedpassword
- Jenkins password or token, if secured