configuration as code: the job dsl plugin
TRANSCRIPT
![Page 1: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/1.jpg)
Configuration as Code: The Job DSL Plugin
Daniel Spilker – CoreMedia AG
![Page 2: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/2.jpg)
#jenkinsconfDaniel Spilker
• Works for CoreMedia in Hamburg, Germany• Software Architect on the Engineering Tools Team
• Maintainer of the– Job DSL Plugin– Gradle JPI Plugin
![Page 3: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/3.jpg)
#jenkinsconfAgenda
• Configuration as Code• The Job DSL Plugin• Q & A
![Page 4: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/4.jpg)
#jenkinsconf
Current Situation
• No single job that builds everything• Each branch needs its own pipeline• Every team has their own jobs
![Page 5: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/5.jpg)
#jenkinsconf
Problem
• Lots of copy&paste• Editing in HTML
text areas• Settings hidden behind
Advanced button• Working with the UI
can be slow
![Page 6: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/6.jpg)
#jenkinsconf
Configuration As Code
• Create newpipelines quickly
• Refactor jobs• Trace changes• Work with your
favorite tool set
![Page 7: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/7.jpg)
#jenkinsconf
There Is A Plugin For That
• Template Project Plugin• Job Generator Plugin• Literate Plugin• JobConfigHistory Plugin• Workflow Plugin• Job DSL Plugin• …
Open Icon Library / CC BY 3.0
![Page 8: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/8.jpg)
#jenkinsconf
Job DSL Language
![Page 9: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/9.jpg)
#jenkinsconfJob DSL Language
job('job-dsl-plugin') { scm { github('jenkinsci/job-dsl-plugin') } steps { gradle('clean build') } publishers { archiveArtifacts('**/job-dsl.hpi') }}
![Page 10: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/10.jpg)
#jenkinsconfJob DSL Language
job('job-dsl-plugin') { scm { github('jenkinsci/job-dsl-plugin') } steps { gradle('clean build') } publishers { archiveArtifacts('**/job-dsl.hpi') }}
![Page 11: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/11.jpg)
#jenkinsconfJob DSL Language
job('job-dsl-plugin') { scm { github('jenkinsci/job-dsl-plugin') } steps { gradle('clean build') } publishers { archiveArtifacts('**/job-dsl.hpi') }}
![Page 12: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/12.jpg)
#jenkinsconfJob DSL Language
job('job-dsl-plugin') { scm { github('jenkinsci/job-dsl-plugin') } steps { gradle('clean build') } publishers { archiveArtifacts('**/job-dsl.hpi') }}
![Page 13: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/13.jpg)
#jenkinsconf
Job DSL Plugin
![Page 14: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/14.jpg)
#jenkinsconfJob DSL Plugin
• Install Job DSL Plugin• Create free-style project• Add “Source Code Management”• Add “Process Job DSLs” build step• Configure scripts• Run job
![Page 15: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/15.jpg)
#jenkinsconfJob DSL Plugin
• Install Job DSL Plugin• Create free-style project• Add “Source Code Management”• Add “Process Job DSLs” build step• Configure scripts• Run job
![Page 16: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/16.jpg)
#jenkinsconfJob DSL Plugin
• Install Job DSL Plugin• Create free-style project• Add “Source Code Management”• Add “Process Job DSLs” build step• Configure scripts• Run job
![Page 17: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/17.jpg)
#jenkinsconfJob DSL Plugin
• Install Job DSL Plugin• Create free-style project• Add “Source Code Management”• Add “Process Job DSLs” build step• Configure scripts• Run job
![Page 18: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/18.jpg)
#jenkinsconfJob DSL Plugin
• Install Job DSL Plugin• Create free-style project• Add “Source Code Management”• Add “Process Job DSLs” build step• Configure scripts• Run job
![Page 19: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/19.jpg)
#jenkinsconfJob DSL Plugin
• Install Job DSL Plugin• Create free-style project• Add “Source Code Management”• Add “Process Job DSLs” build step• Configure scripts• Run job
![Page 20: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/20.jpg)
#jenkinsconf
Batteries Included
125 plugins supported by the DSL
![Page 21: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/21.jpg)
#jenkinsconf
Batteries Included
EnvInject
Groovy
Copy Artifact
GitSubversion
Folders
Extra Columns
StashNotifier
Gradle
Build Pipeline
Workspace Cleanup
GitHub Pull Request Builder
GitHub
JaCoCoRelease
Build Flow
Robot Framework
Tool Environment
Conditional BuildStep
Throttle Concurrent Builds
Associated Files
Workflow
Emma
Xvnc
AnsiColor
Timestamper
Text-FinderJob DSL Perforce
Ant
Maven Project
![Page 22: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/22.jpg)
#jenkinsconf
Batteries Included
Checkstyle
Categorized Jobs View
Build-timeout
Config File Provider
Golang
Delivery Pipeline
HipChatNaginator
Jabber
NestedViews
HTML Publisher
NodeJS
Flowdock
Credentials Binding
Plot
RunDeckRake
PowershellSBT
xUnit
xvfb
Warnings
SonarvSphere Cloud
RVM
Port Allocator
Sectioned View
SSH Agent
Build Monitor
Claim
Email-ext
Findbugs
Grails
Javadoc
Multijob
![Page 23: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/23.jpg)
#jenkinsconf
Community Driven
70 contributors 500 pull requests
![Page 24: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/24.jpg)
#jenkinsconf
Extending The DSL
![Page 25: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/25.jpg)
#jenkinsconf
Extending The DSL
![Page 26: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/26.jpg)
#jenkinsconf
Extending The DSL
![Page 27: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/27.jpg)
#jenkinsconf
Extending The DSL
job('example') {
...
configure { project ->
project / buildWrappers <<
EnvInjectPasswordWrapper {
injectGlobalPasswords(true)
}
}
}
![Page 28: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/28.jpg)
#jenkinsconf
Job DSL Playground
• http://job-dsl.herokuapp.com/
![Page 29: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/29.jpg)
#jenkinsconf
Everything is Groovy
![Page 30: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/30.jpg)
#jenkinsconf
Everything is Groovy
def branches = ['master', 'feature-a']
branches.each { branch ->
job("jenkins-${branch}") {
scm {
github('jenkinsci/jenkins', branch)
}
…
}
}
![Page 31: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/31.jpg)
#jenkinsconf
Using Libraries
• Any Java / Groovy library (JARs) can be used• Download and dependency resolution must be
handled before running the Job DSL build step• Anything can be used to download the JARs
– Build tools like Gradle– Repository Connector Plugin– Shell script with curl or wget
![Page 32: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/32.jpg)
#jenkinsconf
Using Libraries
import org.kohsuke.github.GitHub
def gitHub = GitHub.connect()def repo = gitHub.getRepository('jenkinsci/jenkins')repo.branches.keys.each { branch -> job("jenkins-${branch}") { … }}
![Page 33: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/33.jpg)
#jenkinsconf
Using Libraries – Gradle Example
repositories { jcenter() }
configurations { libs }
dependencies { libs 'org.kohsuke:github-api:1.50'}
task libs(type: Copy) { into 'libs' from configurations.libs}
![Page 34: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/34.jpg)
#jenkinsconf
Using Libraries – Gradle Example
![Page 35: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/35.jpg)
#jenkinsconf
Using Functions
def createMavenJob(def jobFactory, def name) { jobFactory.mavenJob(name) { goals('clean verify') jdk('Java 7 latest') mavenInstallation('Maven 3.2.5') publishers { sonar() } }}
![Page 36: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/36.jpg)
#jenkinsconf
Using Functions
def createMavenJob(def jobFactory, def name) { … }
def jobA = createMavenJob(this, 'project-a') def jobB = createMavenJob(this, 'project-b')
![Page 37: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/37.jpg)
#jenkinsconf
Using Functions
def createMavenJob(def jobFactory, def name) { … }
def jobA = createMavenJob(this, 'project-a') def jobB = createMavenJob(this, 'project-b')
jobB.with { scm { github('example-corp/project-a') }}
![Page 38: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/38.jpg)
#jenkinsconf
Logging
println 'awesome!'
(1..10).each { println "loop ${it}"}
![Page 39: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/39.jpg)
#jenkinsconf
Logging
class Test { static demo(def out) { out.println 'Must use out here!' }}
Test.demo(out)
![Page 40: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/40.jpg)
#jenkinsconf
IDE Support
![Page 41: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/41.jpg)
#jenkinsconf
IDE Support
• IntelliJ IDEA only• Use a Gradle build file for configuration
apply plugin: 'groovy'
sourceSets { … }
repositories { … }
dependencies { compile 'org.jenkins-ci.plugins:job-dsl-core:1.34'}
![Page 42: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/42.jpg)
#jenkinsconf
Syntax Highlighting
![Page 43: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/43.jpg)
#jenkinsconf
Parameter Information
![Page 44: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/44.jpg)
#jenkinsconf
Documentation
![Page 45: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/45.jpg)
#jenkinsconf
Using Credentials
![Page 46: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/46.jpg)
#jenkinsconf
Do not putplain text credentialsin a DSL script
![Page 47: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/47.jpg)
#jenkinsconf
Credentials Plugin
• Use the Credentials Pluginfor managing credentials
• The essentials plugins canconsume these credentials(Git, Subversion, …)
• Use the Credentials BindingPlugin to map credentials toenvironment variables
![Page 48: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/48.jpg)
#jenkinsconf
Using Credentials
job('example') { scm { git { remote { github('example-corp/example') credentials('example-corp-github') } } }}
![Page 49: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/49.jpg)
#jenkinsconf
The DSL In Depth
![Page 50: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/50.jpg)
#jenkinsconf
Supported Project Types
// https://wiki.jenkins-ci.org/display/JENKINS/Maven+Project+PluginmavenJob(…) { … }
// https://wiki.jenkins-ci.org/display/JENKINS/Matrix+Project+PluginmatrixJob(…) { … }
// https://wiki.jenkins-ci.org/display/JENKINS/Multijob+PluginmultiJob(…) { … }
// https://wiki.jenkins-ci.org/display/JENKINS/Workflow+PluginworkflowJob(…) { … }
// https://wiki.jenkins-ci.org/display/JENKINS/Build+Flow+PluginbuildFlowJob(…) { … }
![Page 51: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/51.jpg)
#jenkinsconf
Workflow Job Example
workflowJob('acme') { definition { cps { script(readFileFromWorkspace('acme.groovy')) sandbox() } }}
![Page 52: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/52.jpg)
#jenkinsconf
Reading Files
workflowJob('acme') { definition { cps { script(readFileFromWorkspace('acme.groovy')) sandbox() } }}
![Page 53: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/53.jpg)
#jenkinsconf
Extending The DSL – Project Types
![Page 54: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/54.jpg)
#jenkinsconf
Extending The DSL – Project Types
job('multi-branch') { configure { project -> project.name = 'freestyle-multi-branch-project' … }}
![Page 55: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/55.jpg)
#jenkinsconf
Creating Views
listView('project-a') { jobs { regex('project-a-.+') } columns { status() weather() name() }}
![Page 56: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/56.jpg)
#jenkinsconf
Supported View Types
sectionedView(…) { … }
nestedView(…) { … }
deliveryPipelineView(…) { … }
buildPipelineView(…) { … }
buildMonitorView(…) { … }
categorizedJobsView(…) { … }
![Page 57: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/57.jpg)
#jenkinsconf
Using Folders
folder('team-a')
job('team-a/compile') { ...}
https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Folders+Plugin
![Page 58: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/58.jpg)
#jenkinsconf
Best Practices
![Page 59: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/59.jpg)
#jenkinsconfBest Practices
• Start by converting a few jobs
• Create new jobs from DSL scripts
• Gradually convert all jobs to DSL scripts
![Page 60: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/60.jpg)
#jenkinsconfBest Practices
• Commit your DSL scripts to SCM
• Do not put plain text credentials in DSL scripts
• Use Groovy code to avoid repetition
![Page 61: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/61.jpg)
#jenkinsconf
Further Information
• Documentationhttps://github.com/jenkinsci/job-dsl-plugin/wiki
• Mailing Listhttps://groups.google.com/forum/?fromgroups#!forum/job-dsl-plugin
• Exampleshttps://github.com/sheehan/job-dsl-gradle-example
• Playgroundhttp://job-dsl.herokuapp.com/
![Page 62: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/62.jpg)
#jenkinsconf
Questions?
![Page 63: Configuration as Code: The Job DSL Plugin](https://reader038.vdocument.in/reader038/viewer/2022102622/55d14a89bb61eb18578b4675/html5/thumbnails/63.jpg)
#jenkinsconf
Thank You
Daniel Spilker
@daspilker
We‘re hiring
www.coremedia.com
@CoreMediaMinds