Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automate jenkins job creation (#phpbnl18)

Automate jenkins job creation (#phpbnl18)

Improved version given at phpbnl18

Toni Van de Voorde

January 27, 2018
Tweet

More Decks by Toni Van de Voorde

Other Decks in Technology

Transcript

  1. Why Jenkins ? o Free o Open-Source o Customization o

    Plugin system o Full control of the system
  2. Adsdaq o Microservice oriented o Different techno's o Lot’s of

    small feature branches Many jobs to manage !!!
  3. Job DSL Plugin Script Security Your first execution failed right

    ??? o Script Approval o Sandboxing o Disable
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Job DSL Plugin Community Driven o > 1000 Pull Requests

    o 155 contributors o > 1000 stars
  10. 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) } } }
  11. 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) } } }
  12. 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) } } }
  13. 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) } } }
  14. 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 { … }
  15. 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 } }
  16. 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 { … }
  17. 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 { ... } }
  18. 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 { ... } }
  19. 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 { ... } }
  20. Job DSL Plugin Many other features o Use any Java/Groovy

    libraries o Functions to reuse code o Create Views/Folders/Worflows/…
  21. 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/
  22. Job DSL Plugin Tips o Commit your DSL scripts to

    SCM o Don’t create jobs manually anymore :p
  23. 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
  24. 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