Slide 1

Slide 1 text

Automate jenkins job creation Toni Van de Voorde – CTO @ Adsdaq.eu

Slide 2

Slide 2 text

Why Jenkins ? o Free o Open-Source o Customization o Plugin system o Full control of the system

Slide 3

Slide 3 text

But o Time needed for configurations / customization

Slide 4

Slide 4 text

Adsdaq o Microservice oriented o Different techno's o Lot’s of small feature branches Many jobs to manage !!!

Slide 5

Slide 5 text

The solution Job DSL Plugin

Slide 6

Slide 6 text

Job DSL Plugin A groovy-based Domain Specific Language for Jenkins Jobs

Slide 7

Slide 7 text

Job DSL Plugin Installation FROM jenkins/jenkins:lts-alpine RUN /usr/local/bin/install-plugins.sh job-dsl

Slide 8

Slide 8 text

Job DSL Plugin Configuration 1. Create new Freestyle project

Slide 9

Slide 9 text

Job DSL Plugin Configuration 2. Add SCM

Slide 10

Slide 10 text

Job DSL Plugin Configuration 3. Add build step

Slide 11

Slide 11 text

Job DSL Plugin Configuration 4. Configure the build step

Slide 12

Slide 12 text

Job DSL Plugin Configuration 5. Save and execute the job

Slide 13

Slide 13 text

Job DSL Plugin Script Security Your first execution failed right ??? o Script Approval o Sandboxing o Disable

Slide 14

Slide 14 text

Job DSL Plugin Script Security Script Approval

Slide 15

Slide 15 text

Job DSL Plugin def gitUrl = 'git://github.com/jenkinsci/job-dsl-plugin.git’ job('demo1') { scm { git(gitUrl) } triggers { scm('*/15 * * * *') } steps { maven('-e clean test') } } Example demo1.groovy

Slide 16

Slide 16 text

Job DSL Plugin def gitUrl = 'git://github.com/jenkinsci/job-dsl-plugin.git’ job('demo1') { scm { git(gitUrl) } triggers { scm('*/15 * * * *') } steps { maven('-e clean test') } } Example demo1.groovy

Slide 17

Slide 17 text

Job DSL Plugin def gitUrl = 'git://github.com/jenkinsci/job-dsl-plugin.git’ job('demo1') { scm { git(gitUrl) } triggers { scm('*/15 * * * *') } steps { maven('-e clean test') } } Example demo1.groovy

Slide 18

Slide 18 text

Job DSL Plugin def gitUrl = 'git://github.com/jenkinsci/job-dsl-plugin.git’ job('demo1') { scm { git(gitUrl) } triggers { scm('*/15 * * * *') } steps { maven('-e clean test') } } Example demo1.groovy

Slide 19

Slide 19 text

Job DSL Plugin def gitUrl = 'git://github.com/jenkinsci/job-dsl-plugin.git’ job('demo1') { scm { git(gitUrl) } triggers { scm('*/15 * * * *') } steps { maven('-e clean test') } } Example demo1.groovy

Slide 20

Slide 20 text

Job DSL Plugin Currently supports up to 190 Plugins

Slide 21

Slide 21 text

Job DSL Plugin Community Driven o > 1000 Pull Requests o 155 contributors o > 1000 stars

Slide 22

Slide 22 text

Job DSL Plugin Extend the DSL Heeelp my plugin is not (yet) supported

Slide 23

Slide 23 text

Job DSL Plugin Extend the DSL demo2.groovy

Slide 24

Slide 24 text

Job DSL Plugin Generate Jobs/branch (1) demo3.groovy def gitUrl = 'git://github.com/jenkinsci/job-dsl-plugin.git’ def branches = ['master','dev','hotfix'] branches.each { branch -> job("demo3-${branch}") { scm { git(gitUrl, branch) } } }

Slide 25

Slide 25 text

Job DSL Plugin Generate Jobs/branch (1) demo3.groovy def gitUrl = 'git://github.com/jenkinsci/job-dsl-plugin.git’ def branches = ['master','dev','hotfix'] branches.each { branch -> job("demo3-${branch}") { scm { git(gitUrl, branch) } } }

Slide 26

Slide 26 text

Job DSL Plugin Generate Jobs/branch (1) demo3.groovy def gitUrl = 'git://github.com/jenkinsci/job-dsl-plugin.git’ def branches = ['master','dev','hotfix'] branches.each { branch -> job("demo3-${branch}") { scm { git(gitUrl, branch) } } }

Slide 27

Slide 27 text

Job DSL Plugin Generate Jobs/branch (2) demo4.groovy def project = 'Netflix/asgard’ 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("https://github.com/${project}.git", branchName) } } }

Slide 28

Slide 28 text

Job DSL Plugin Generate Jobs/branch (3) demo5.groovy def project = 'automate-jenkins-job-creation’ def repo = "https://[email protected]/tonivdv/${project}.git" Process proc1 = "git ls-remote -h ${repo}".execute() Process proc2 = 'sed s/.*refs\\/heads\\///g'.execute() Process all = proc1 | proc2 all.waitFor() def branches = "${all.in.text}".split('\\n') branches.each { … }

Slide 29

Slide 29 text

Job DSL Plugin Generate Jobs/branch (4) demo5bis.groovy package utilities public class GitUtils { public static String[] getRemoteBranches(def repoUrl, def out) { Process proc1 = "git ls-remote -h ${repoUrl}".execute() Process proc2 = 'sed s/.*refs\\/heads\\///g'.execute() Process all = proc1 | proc2 all.waitFor() out.println "### DEBUG: Begin ###" ... out.println "### DEBUG: End ###" def branches = "${all.in.text}".split('\\n') return branches } }

Slide 30

Slide 30 text

Job DSL Plugin Generate Jobs/branch (5) demo5bis.groovy import utilities.GitUtils def project = 'automate-jenkins-job-creation’ def repo = https://[email protected]/tonivdv/${project}.git def branches = GitUtils.getRemoteBranches(repo, out) folder('demo5bis') branches.each { … }

Slide 31

Slide 31 text

Job DSL Plugin Conditionals demo6.groovy branches.each { branch -> def myjob = job(jobName) { .. } if (branch == "master") { myjob.with { steps { maven('-e deploy clean test') } } } else { ... } }

Slide 32

Slide 32 text

Job DSL Plugin Conditionals demo6.groovy branches.each { branch -> def myjob = job(jobName) { .. } if (branch == "master") { myjob.with { steps { maven('-e deploy clean test') } } } else { ... } }

Slide 33

Slide 33 text

Job DSL Plugin Conditionals demo6.groovy branches.each { branch -> def myjob = job(jobName) { .. } if (branch == "master") { myjob.with { steps { maven('-e deploy clean test') } } } else { ... } }

Slide 34

Slide 34 text

Job DSL Plugin Many other features o Use any Java/Groovy libraries o Functions to reuse code o Create Views/Folders/Worflows/…

Slide 35

Slide 35 text

Job DSL Plugin Tips o Credential Plugin o JobConfigHistory Plugin o Use the great API Viewer o (https://[jenkins-host]/plugin/job-dsl/api-viewer/index.html) o https://jenkinsci.github.io/job-dsl-plugin/

Slide 36

Slide 36 text

Job DSL Plugin Tips o Commit your DSL scripts to SCM o Don’t create jobs manually anymore :p

Slide 37

Slide 37 text

Job DSL Plugin References o Wiki o https://github.com/jenkinsci/job-dsl-plugin/wiki o API Viewer o https://jenkinsci.github.io/job-dsl-plugin/ o Presentation demo files o https://github.com/tonivdv/automate-jenkins-job-creation

Slide 38

Slide 38 text

Automate jenkins job creation Questions?

Slide 39

Slide 39 text

Automate jenkins job creation Toni Van de Voorde [email protected] @tonivdv www.devexp.eu https://github.com/tonivdv Thank You Join our Team [email protected] demo-files: https://git.io/vds08 Rate this talk: https://joind.in/talk/26f3d