Configuration as Code: The Job DSL Plugin

63
Configuration as Code: The Job DSL Plugin Daniel Spilker – CoreMedia AG

Transcript of Configuration as Code: The Job DSL Plugin

Page 1: Configuration as Code: The Job DSL Plugin

Configuration as Code: The Job DSL Plugin

Daniel Spilker – CoreMedia AG

Page 2: Configuration as Code: The Job DSL Plugin

#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

#jenkinsconfAgenda

• Configuration as Code• The Job DSL Plugin• Q & A

Page 4: Configuration as Code: The Job DSL Plugin

#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

#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

#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

#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

#jenkinsconf

Job DSL Language

Page 9: Configuration as Code: The Job DSL Plugin

#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

#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

#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

#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

#jenkinsconf

Job DSL Plugin

Page 14: Configuration as Code: The Job DSL Plugin

#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

#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

#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

#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

#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

#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

#jenkinsconf

Batteries Included

125 plugins supported by the DSL

Page 21: Configuration as Code: The Job DSL Plugin

#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

#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

#jenkinsconf

Community Driven

70 contributors 500 pull requests

Page 24: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Extending The DSL

Page 25: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Extending The DSL

Page 26: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Extending The DSL

Page 27: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Extending The DSL

job('example') {

...

configure { project ->

project / buildWrappers <<

EnvInjectPasswordWrapper {

injectGlobalPasswords(true)

}

}

}

Page 28: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Job DSL Playground

• http://job-dsl.herokuapp.com/

Page 29: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Everything is Groovy

Page 30: Configuration as Code: The Job DSL Plugin

#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

#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

#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

#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

#jenkinsconf

Using Libraries – Gradle Example

Page 35: Configuration as Code: The Job DSL Plugin

#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

#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

#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

#jenkinsconf

Logging

println 'awesome!'

(1..10).each { println "loop ${it}"}

Page 39: Configuration as Code: The Job DSL Plugin

#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

#jenkinsconf

IDE Support

Page 41: Configuration as Code: The Job DSL Plugin

#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

#jenkinsconf

Syntax Highlighting

Page 43: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Parameter Information

Page 44: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Documentation

Page 45: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Using Credentials

Page 46: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Do not putplain text credentialsin a DSL script

Page 47: Configuration as Code: The Job DSL Plugin

#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

#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

#jenkinsconf

The DSL In Depth

Page 50: Configuration as Code: The Job DSL Plugin

#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

#jenkinsconf

Workflow Job Example

workflowJob('acme') { definition { cps { script(readFileFromWorkspace('acme.groovy')) sandbox() } }}

Page 52: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Reading Files

workflowJob('acme') { definition { cps { script(readFileFromWorkspace('acme.groovy')) sandbox() } }}

Page 53: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Extending The DSL – Project Types

Page 54: Configuration as Code: The Job DSL Plugin

#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

#jenkinsconf

Creating Views

listView('project-a') { jobs { regex('project-a-.+') } columns { status() weather() name() }}

Page 56: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Supported View Types

sectionedView(…) { … }

nestedView(…) { … }

deliveryPipelineView(…) { … }

buildPipelineView(…) { … }

buildMonitorView(…) { … }

categorizedJobsView(…) { … }

Page 57: Configuration as Code: The Job DSL Plugin

#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

#jenkinsconf

Best Practices

Page 59: Configuration as Code: The Job DSL Plugin

#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

#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

#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

#jenkinsconf

Questions?

Page 63: Configuration as Code: The Job DSL Plugin

#jenkinsconf

Thank You

Daniel Spilker

[email protected]

@daspilker

We‘re hiring

www.coremedia.com

@CoreMediaMinds