Automate jenkins job creation

Automate jenkins job creation

Jenkins is a great CI tool. However when you need to manage a lot of jobs, this can quickly become a pain in the neck. What if you could automate all this? This talk will show you how to do this.

8b3f69dcdbfc477777ab524c316168c1?s=128

Toni Van de Voorde

September 28, 2017
Tweet

Transcript

  1. Automate jenkins job creation Belgium Jenkins Area Meetup Toni Van

    de Voorde – CTO @ Adlogix.eu
  2. Adlogix / Adsdaq / Buymedia Belgium Jenkins Area Meetup o

    Microservice oriented o Different techno's o Lot’s of small feature branches Many jobs to manage !!!
  3. The solution Belgium Jenkins Area Meetup Job DSL Plugin

  4. Job DSL Plugin Belgium Jenkins Area Meetup A groovy-based Domain

    Specific Language for Jenkins Jobs
  5. Job DSL Plugin Belgium Jenkins Area Meetup Installation FROM jenkins/jenkins:lts-alpine

    RUN /usr/local/bin/install-plugins.sh job-dsl
  6. Job DSL Plugin Belgium Jenkins Area Meetup Configuration 1. Create

    new Freestyle project
  7. Job DSL Plugin Belgium Jenkins Area Meetup Configuration 2. Add

    SCM
  8. Job DSL Plugin Belgium Jenkins Area Meetup Configuration 3. Add

    build step
  9. Job DSL Plugin Belgium Jenkins Area Meetup Configuration 4. Configure

    the build step
  10. Job DSL Plugin Belgium Jenkins Area Meetup Configuration 5. Save

    and execute the job
  11. Job DSL Plugin Belgium Jenkins Area Meetup Script Security Your

    first execution failed right ??? o Script Approval o Sandboxing o Disable
  12. Job DSL Plugin Belgium Jenkins Area Meetup Script Security Script

    Approval
  13. Job DSL Plugin Belgium Jenkins Area Meetup 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
  14. Job DSL Plugin Belgium Jenkins Area Meetup 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
  15. Job DSL Plugin Belgium Jenkins Area Meetup 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
  16. Job DSL Plugin Belgium Jenkins Area Meetup 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
  17. Job DSL Plugin Belgium Jenkins Area Meetup 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
  18. Job DSL Plugin Belgium Jenkins Area Meetup Currently supports up

    to 190 Plugins
  19. Job DSL Plugin Belgium Jenkins Area Meetup Community Driven o

    > 1000 Pull Requests o 155 contributors o > 1000 stars
  20. Job DSL Plugin Belgium Jenkins Area Meetup Extend the DSL

    Heeelp my plugin is not (yet) supported
  21. Job DSL Plugin Belgium Jenkins Area Meetup Extend the DSL

    demo2.groovy
  22. Job DSL Plugin Belgium Jenkins Area Meetup 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) } } }
  23. Job DSL Plugin Belgium Jenkins Area Meetup 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) } } }
  24. Job DSL Plugin Belgium Jenkins Area Meetup 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) } } }
  25. Job DSL Plugin Belgium Jenkins Area Meetup 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) } } }
  26. Job DSL Plugin Belgium Jenkins Area Meetup Generate Jobs/branch (3)

    demo5.groovy def project = 'automate-jenkins-job-creation’ def repo = "https://tonivdv@bitbucket.org/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 { … }
  27. Job DSL Plugin Belgium Jenkins Area Meetup Conditionals demo6.groovy branches.each

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

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

    { branch -> def myjob = job(jobName) { .. } if (branch == "master") { myjob.with { steps { maven('-e deploy clean test') } } } else { ... } }
  30. Job DSL Plugin Belgium Jenkins Area Meetup Many other features

    o Use any Java/Groovy libraries o Functions to reuse code o Create Views/Folders/Worflows/…
  31. Job DSL Plugin Belgium Jenkins Area Meetup 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/
  32. Job DSL Plugin Belgium Jenkins Area Meetup Tips o Commit

    your DSL scripts to SCM o Don’t create jobs manually anymore :p
  33. Job DSL Plugin Belgium Jenkins Area Meetup 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://bitbucket.org/tonivdv/automate-jenkins-job-creation
  34. Automate jenkins job creation Belgium Jenkins Area Meetup Questions?

  35. Automate jenkins job creation Belgium Jenkins Area Meetup Toni Van

    de Voorde toni.vdv@gmail.com @tonivdv www.devexp.eu https://github.com/tonivdv Thank You Join our Team info@adsdaq.eu Slides: http://bit.ly/2hCbkos demo-files: https://git.io/vds08