1. Take control of your Jenkins jobs via jobDSL
#1 SysOps / DevOps Krak´ow Meetup
Lukasz Proszek
@lukaszproszek
2017-11-29, Krak´ow
Senior DevOps Engineer @Lumesse
1
3. About Me
• Senior Development Operation Engineer @ Lumesse
• If something doesn’t have a CLI, it is as if it never existed
at all
• Automates almost everything
• My goal: ”Everything works. What do we pay you for?”
• My reality: ”Nothing works. What do we pay you for?”
2
4. Why am I even here?
• A medium sized jenkins installation
• 1 master, 10 slaves
• 1032 Jenkins Jobs
• 29 jobs marked ’to-delete’
• 84 jobs marked ’usunsed’
• dozens of jobs that nobody remembers
• everybody has configure rights
• post-mortem job changes analysis
3
7. Working with Jenkins: GUI. cont.
My opinion about the GUI:
• manual
• error prone
• wrong kind of boring
• workable for a dozen of jobs
• unmaintanable for hundreds
5
13. Working with Jenkins: Script Console
• Script Console: http://jenkins.local/script
9
14. Working with Jenkins: Scrip Console (example)
Cancelling running builds
1 for (job in this.hudson.instance.items) {
2 for (build in job.builds) {
3 if (build == this.build) { continue; } // don’t cancel ourself!
4 if (!build.hasProperty(’causes’)) { continue; }
5 if (!build.isBuilding()) { continue; }
6 for (cause in build.causes) {
7 if (!cause.hasProperty(’upstreamProject’)) { continue; }
8 if (cause.upstreamProject == this.upstreamProject &&
9 cause.upstreamBuild == this.upstreamBuild) {
10 this.printer.println(’Stopping ’ + build.toString());
11 build.doStop();
12 this.printer.println(build.toString() + ’ stopped.’);
13 }}}}
10
15. Working with Jenkins: Script Console
• Allows to run Groovy scripts on master and/or slave nodes
• Web based shell into the Jenkins runtime
• Can do anything. ANYTHING
• "cat /etc/passwd".execute().text
• No administrative controls
• ⇒ everyone with access is basically a jenkins admin
• Originally was intended as a debugging interface for Jenkins
developers.
11
24. Configuration via GUI/XML: conclusion
• Done in web browser
• How to control changes?
• Imagine ediding this xml for more complicted jobs
• XML editing is not human friendly
19
25. No mouse chalenge
Try to write a job without the GUI1
1
https://mn.gov/mnit/blog/?id=38-232094
20
27. Configuration as Code
• Treat your job configuration as every other piece of
config/code.
• Version your configuration.
• Do not Repeat Yourself
• Validate and review your changes
21
28. Jenkins Job DSL
• Simple (YMMV).
• Intuitve (YMMV again).
• Human and machine friendly.
• Groovy DSL.
• Generate all your jobs from a seed job.
• Abstracts your utility functions.
• Supports plugins through extensions.
• Easily (YMMV :)) template your jobs.
• Originally developed at Netflix
22
33. DSL: unsupported plugins - issue
• Currently not all jenkins plugins are covered by the DSL.
• How to deal with that challenge?
• Create a sample job in GUI and inspect it.
• At the time of DSL introduction at Lumesse, it did not
support Maven Metadata Parameter.
27
39. Job factory: use case
• Each job should have the same block configured, e.g.
• colorized output
• timestamped output
• e-mail notification on failure
• e-mail notification on success
• execution permissions dependant on the environment stage
• ...
32
45. Jenkins job DSL: master SeedJob enablement
• The first job can be uploaded using a simple api call.
1 ./gradlew rest
2 -Dpattern="jobs/MasterSeedJob.groovy"
3 -DbaseUrl=http://jenkins.local/
4 -Dusername=myusername
5 -Dpassword=secret
38
46. Jenkins job DSL: master SeedJob
• Will poll the repository every 5 minutes
• On changed source, it will execute:
• gradle clean test
• a DSL build step that will recursively include all
”*SeedJob.groovy” files from the ”jobs” directory
• archive test results
39
47. Jenkins job DSL: jenkins harness
• You can test jobs before commiting them to the repository
• You can run it as a ”verfy pull request job”
• ./gradlew test
40
49. Conclusion
• Jobs are storred in git
• Builders for common jobs
• AdHoc extensible with .build(this).with{}
• Pull requests with approvals
• Pull request verification job
• Automagic job updates
41
50. Thank You
• jenkins.io/doc/book/managing/cli
• jenkinsci.github.io/job-dsl-plugin
• github.com/jenkinsci/job-dsl-plugin/wiki/Frequently-Asked-
Questions
• github.com/sheehan/job-dsl-gradle-example
• github.com/sheehan/job-dsl-rest-example
• wiki2beamer - latex-beamer made easy
• impressive - pdf presentations with sugar added
• github.com/frogu/presentations
42