Jenkins Docker

41,022 views

Published on

Docker and Jenkins. Orchestrating Continuous Delivery
Through the use of build pipelines, Continuous Delivery will enable faster and more frequent build, test and deployment cycles of software.

To ensure that what you are delivering has the required quality: how do we build a continuous delivery pipeline in the real world?

In this session, instead of relying on static step configurations, we are going to demonstrate how to code a pipeline using Jenkins and Gradle and how Docker can help on this task. The end result is faster application releases with higher quality.

Published in: Technology

Jenkins Docker

  1. 1. Orchestrating Continuous Delivery D O C K E R A N D J E N K I N S .
  2. 2.  A L E X S O T O B - L O R D O F T H E J A R S . C O M  alexsotob  lordofthejars
  3. 3. S O F T W A R E I S E A T I N G T H E W O R L D  alexsotob  lordofthejars
  4. 4.  alexsotob  lordofthejars
  5. 5.  alexsotob  lordofthejars
  6. 6.  alexsotob  lordofthejars
  7. 7.  alexsotob  lordofthejars
  8. 8.  alexsotob  lordofthejars
  9. 9.  alexsotob  lordofthejars
  10. 10.   alexsotob  lordofthejars
  11. 11.  Faster  alexsotob  lordofthejars
  12. 12.  Faster Sooner  alexsotob  lordofthejars
  13. 13.  Faster Sooner Better  alexsotob  lordofthejars
  14. 14. K E Y I S A U T O M A T I O N  alexsotob  lordofthejars
  15. 15.  alexsotob  lordofthejars
  16. 16.  alexsotob  lordofthejars
  17. 17.  alexsotob  lordofthejars
  18. 18. S P E E D + F R I C T I O N = H E A T  alexsotob  lordofthejars
  19. 19. G O - G O - G O  alexsotob  lordofthejars
  20. 20. C O N T A I N E R S C O D I F Y O S C O N F I G  alexsotob  lordofthejars
  21. 21.  alexsotob  lordofthejars
  22. 22. $ docker run $ docker run --ti ubuntuti ubuntu::12.1012.10 bash bash [[a2ba2b]] apt apt--getget update update [[a2ba2b]] apt apt--getget install curl install curl [[a2ba2b]] exit exit $ docker commit $ docker commit --m m "curl installed""curl installed" a2b username a2b username//imageimage $ docker build $ docker build .. H O W I B U I L D A D O C K E R I M A G E ?  alexsotob  lordofthejars
  23. 23. J E N K I N S : D O C K E R A N D C D Docker Hub Notification: triggers downstream jobs when a tagged container is pushed to Docker Hub  alexsotob  lordofthejars
  24. 24. J E N K I N S : D O C K E R A N D C D Docker Hub Notification: triggers downstream jobs when a tagged container is pushed to Docker Hub Docker Traceability: identifies which build pushed a particular container, displays on Jenkins builds page  alexsotob  lordofthejars
  25. 25. J E N K I N S : D O C K E R A N D C D Docker Hub Notification: triggers downstream jobs when a tagged container is pushed to Docker Hub Docker Traceability: identifies which build pushed a particular container, displays on Jenkins builds page Docker Custom Build Environment: specifies customized build environments as Docker containers  alexsotob  lordofthejars
  26. 26. J E N K I N S : D O C K E R A N D C D Docker Hub Notification: triggers downstream jobs when a tagged container is pushed to Docker Hub Docker Traceability: identifies which build pushed a particular container, displays on Jenkins builds page Docker Custom Build Environment: specifies customized build environments as Docker containers Docker: use a docker host to dynamically provision a slave, run a single build, then tear-down  alexsotob  lordofthejars
  27. 27. J E N K I N S : D O C K E R A N D C D Docker Hub Notification: triggers downstream jobs when a tagged container is pushed to Docker Hub Docker Traceability: identifies which build pushed a particular container, displays on Jenkins builds page Docker Custom Build Environment: specifies customized build environments as Docker containers Docker: use a docker host to dynamically provision a slave, run a single build, then tear-down Build and Publish : builds projects that have a Dockerfile and pushes the resultant tagged image to Docker Hub  alexsotob  lordofthejars
  28. 28. B U I L D , T E S T , & D E P L O Y D O C K E R I M A G E F R O M J E N K I N S  alexsotob  lordofthejars
  29. 29. D O C K E R H U B N O T I F I C A T I O N P L U G I N  alexsotob  lordofthejars
  30. 30.  alexsotob  lordofthejars
  31. 31.  alexsotob  lordofthejars
  32. 32. D O C K E R T R A C E A B I L I T Y P L U G I N  alexsotob  lordofthejars
  33. 33.  alexsotob  lordofthejars
  34. 34.  alexsotob  lordofthejars
  35. 35.  alexsotob  lordofthejars
  36. 36. D O C K E R P L U G I N  alexsotob  lordofthejars
  37. 37.  alexsotob  lordofthejars
  38. 38. D O C K E R C U S T O M B U I L D E N V I R O N M E N T P L U G I N  alexsotob  lordofthejars
  39. 39.  alexsotob  lordofthejars
  40. 40.  alexsotob  lordofthejars
  41. 41. B U I L D A N D P U B L I S H P L U G I N  alexsotob  lordofthejars
  42. 42.  alexsotob  lordofthejars
  43. 43.  alexsotob  lordofthejars
  44. 44.  alexsotob  lordofthejars
  45. 45. N O M O R E B U I L D J O B S / U P / D O W N S T R E A M S P L E A S E ! ! ! !  alexsotob  lordofthejars
  46. 46. Continuous Delivery as Code P I P E L I N E P L U G I N  alexsotob  lordofthejars
  47. 47. F E A T U R E S O F P I P E L I N E P L U G I N Configuration in Source Repositories
  48. 48. F E A T U R E S O F P I P E L I N E P L U G I N Configuration in Source Repositories Less click-and-type, more code
  49. 49. F E A T U R E S O F P I P E L I N E P L U G I N Configuration in Source Repositories Less click-and-type, more code From simple to complex
  50. 50. F E A T U R E S O F P I P E L I N E P L U G I N Configuration in Source Repositories Less click-and-type, more code From simple to complex Survives Jenkins restarts & connection losses ($)
  51. 51. F E A T U R E S O F P I P E L I N E P L U G I N Configuration in Source Repositories Less click-and-type, more code From simple to complex Survives Jenkins restarts & connection losses ($) Reusable Definitions
  52. 52. F E A T U R E S O F P I P E L I N E P L U G I N Configuration in Source Repositories Less click-and-type, more code From simple to complex Survives Jenkins restarts & connection losses ($) Reusable Definitions Build history/trend segregated per branches
  53. 53. F E A T U R E S O F P I P E L I N E P L U G I N Configuration in Source Repositories Less click-and-type, more code From simple to complex Survives Jenkins restarts & connection losses ($) Reusable Definitions Build history/trend segregated per branches UI ($ -> OSS)
  54. 54. stage stage 'compileAndUnit''compileAndUnit' node node {{ git branch git branch:: 'master''master',, url url:: 'https://github.com/lordofthejars/starwars.git''https://github.com/lordofthejars/starwars.git' gradle gradle 'clean test''clean test' stash excludes stash excludes:: 'build/''build/',, includes includes:: '**''**',, name name:: 'source''source' stash includes stash includes:: 'build 'build//jacocojacoco/*.exec', name: 'unitCodeCoverage'/*.exec', name: 'unitCodeCoverage' step([$class: 'JUnitResultArchiver', testResults: '**/ step([$class: 'JUnitResultArchiver', testResults: '**/buildbuild//testtest--resultsresults//**..xml'xml']])) }} stage stage 'codeQuality''codeQuality' parallel parallel 'pmd''pmd' :: {{ node node {{ unstash unstash 'source''source' gradle gradle 'pmdMain''pmdMain' stepstep(([[$$classclass:: 'PmdPublisher''PmdPublisher',, pattern pattern:: 'build/reports/pmd/*.xml''build/reports/pmd/*.xml']])) }} }},, 'jacoco''jacoco':: {{ node node {{ unstash unstash 'source''source' unstash unstash 'unitCodeCoverage''unitCodeCoverage' gradle gradle 'jacocoTestReport''jacocoTestReport' }} }} J E N K I N S F I L E ( 1 / 3 )  alexsotob  lordofthejars
  55. 55. stage stage 'assemble-binaries''assemble-binaries' nodenode(('linux''linux')) {{ unstash unstash 'source''source' withEnvwithEnv(([["SOURCE_BUILD_NUMBER=${env.BUILD_NUMBER}""SOURCE_BUILD_NUMBER=${env.BUILD_NUMBER}"]])) {{ gradle gradle 'assemble''assemble' }} }} input messageinput message:: "Deploy Application to QA ?""Deploy Application to QA ?" stage namestage name:: 'Deploy to QA''Deploy to QA',, concurrency concurrency:: 11 node node {{ echo echo "Application Deployed to QA.""Application Deployed to QA." }} J E N K I N S F I L E ( 2 / 3 )  alexsotob  lordofthejars
  56. 56. gradle gradle == load load 'jenkins/gradle.groovy''jenkins/gradle.groovy' withCredentialswithCredentials(([[[[$$classclass:: 'UsernamePasswordMultiBinding''UsernamePasswordMultiBinding',, credentialsId credentialsId:: 'MyID''MyID',, usernameVariable usernameVariable:: 'GIT_USERNAME''GIT_USERNAME',, passwordVariable passwordVariable:: 'GIT_PASSWORD''GIT_PASSWORD']]]])) {{ sh sh "git tag -a some_tag -m 'Jenkins'""git tag -a some_tag -m 'Jenkins'" sh sh "git push https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@ --tags""git push https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@ --tags" }} def mvnHome tool def mvnHome tool 'Maven 3.3.3''Maven 3.3.3' sh sh "${mvnHome}/bin/mvn -B verify""${mvnHome}/bin/mvn -B verify" J E N K I N S F I L E ( 3 / 3 )  alexsotob  lordofthejars
  57. 57. D O C K E R P I P E L I N E I N T E G R A T I O N
  58. 58. dockerdocker..withRegistrywithRegistry(('https://lordofthejars-docker-continuous_delivery.bintray.io''https://lordofthejars-docker-continuous_delivery.bintray.io',, 'd4fc3fa9-39f7-47ea-a57c-795642f90989''d4fc3fa9-39f7-47ea-a57c-795642f90989')) {{ git git 'git@github.com:lordofthejars/busybox.git''git@github.com:lordofthejars/busybox.git' def newApp def newApp == docker docker..build build "lordofthejars-docker-continuous_delivery.bintray.io/lordofthejars/javatest:${env.BUILD_TAG}""lordofthejars-docker-continuous_delivery.bintray.io/lordofthejars/javatest:${env.BUILD_TAG}" newApp newApp..pushpush(()) }} dockerdocker..imageimage(('lordofthejars/javatest''lordofthejars/javatest'))..withRun withRun {{c c -->> sh sh './executeTests.sh''./executeTests.sh' }} J E N K I N S F I L E ( 1 / 2 )  alexsotob  lordofthejars
  59. 59. dockerdocker..imageimage(('maven:3.3.3-jdk-8''maven:3.3.3-jdk-8'))..insideinside(('-v ~/.m2/repository:/m2repo -u 1000:50''-v ~/.m2/repository:/m2repo -u 1000:50')) {{ git git '…your-sources…''…your-sources…' sh sh 'mvn -Dmaven.repo.local=/m2repo clean install''mvn -Dmaven.repo.local=/m2repo clean install' }} dockerdocker..withServerwithServer(('tcp://swarm.mycorp.com:2376''tcp://swarm.mycorp.com:2376',, 'swarm-certs''swarm-certs')) {{ docker docker..imageimage(('httpd''httpd'))..withRunwithRun(('-p 8080:80''-p 8080:80')) {{c c -->> sh sh "curl -i http://${hostIp(c)}:8080/""curl -i http://${hostIp(c)}:8080/" }} }} def def hostIphostIp((containercontainer)) {{ sh sh "docker inspect -f {{.Node.Ip}} ${container.id} > hostIp""docker inspect -f {{.Node.Ip}} ${container.id} > hostIp" readFilereadFile(('hostIp''hostIp'))..trimtrim(()) }} J E N K I N S F I L E ( 2 / 2 )  alexsotob  lordofthejars
  60. 60.  alexsotob  lordofthejars
  61. 61. D I F F E R E N T W A Y S O F D O I N G S A M E T H I N G S W I T H D S L  alexsotob  lordofthejars
  62. 62. S I M P L E B U I L D F O R P I P E L I N E P L U G I N  alexsotob  lordofthejars
  63. 63. simpleBuild simpleBuild {{ machine machine == "hi-speed""hi-speed" docker docker == "java:1.9""java:1.9" env env == [[ FOO FOO :: 4242,, BAR BAR :: "YASS""YASS" ]] git_repo git_repo == "https://github.com/cloudbeers/PR-demo""https://github.com/cloudbeers/PR-demo" before_script before_script == "echo before""echo before" script script == 'echo after $FOO''echo after $FOO' after_script after_script == 'echo done now''echo done now' notifications notifications == [[ email email :: "foo@bar.com""foo@bar.com" ]] }} J E N K I N S F I L E  alexsotob  lordofthejars
  64. 64. L E T ' S W I N D D O W N  alexsotob  lordofthejars
  65. 65. C O N C L U S I O N S Automation is the Key  alexsotob  lordofthejars
  66. 66. C O N C L U S I O N S Automation is the Key Docker Simplifies Environment Problems  alexsotob  lordofthejars
  67. 67. C O N C L U S I O N S Automation is the Key Docker Simplifies Environment Problems Jenkins is Ready For Docker and CD  alexsotob  lordofthejars
  68. 68. C O N C L U S I O N S Automation is the Key Docker Simplifies Environment Problems Jenkins is Ready For Docker and CD Pipleline as Code gives you Freedom  alexsotob  lordofthejars
  69. 69. O N L Y O N E P I E C E O F T H E J I G S A W  alexsotob  lordofthejars
  70. 70. F O L L O W T H E R U L E S , A D A P T T O Y O U R N E E D S  alexsotob  lordofthejars
  71. 71.  C L O U D B E E S J E N K I N S
  72. 72. It is not necessary to change. Survival is not mandatory. W . E D W A R D S D E M I N G  alexsotob  lordofthejars
  73. 73. Q U E S T I O N S  alexsotob  lordofthejars
  74. 74.  http://www.slideshare.net/asotobu/jenkins-docker  asotobu@gmail.com  alexsotob  lordofthejars
  75. 75. https://github.com/lordofthejars/starwars https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Docker+Pipeline+Plugin https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Docker+Traceability https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Docker+Build+and+Publish+plugin https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Docker+Hub+Notification https://wiki.jenkins-ci.org/display/JENKINS/Docker+Plugin https://github.com/jenkinsci/simple-build-for-pipeline-plugin

×