Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Build, Publish, Deploy and Test Docker images and containers with Jenkins Workflow

31.725 visualizaciones

Publicado el

This lightning talk will show you how simple it is to apply CI to the creation of Docker images, ensuring that each time the source is changed, a new image is created, tagged, and published. I will then show how easy it is to then deploy containers from this image and run tests to verify the behaviour.

Publicado en: Tecnología
  • Inicia sesión para ver los comentarios

Build, Publish, Deploy and Test Docker images and containers with Jenkins Workflow

  1. 1. Build, Publish, Deploy and Test Docker images and containers with Jenkins Workflow. Nigel Harniman Senior Solutions Architect, CloudBees Inc
  2. 2. About me Nigel Harniman @harniman Build Engineer, Devops and Architect DevOps, Infra as Code, Continuous Delivery
  3. 3. “Software is eating the world” Marc Andreessen
  4. 4. 4 How Do You Deliver Better Software Faster? ProdDev
  5. 5. 5 Automation is the Key Photo courtesy of Steve Jurvetson via Flickr
  6. 6. The Docker Advantage
  7. 7. Docker Has Potential An example: Software Configuration Management Space
  8. 8. Docker Has Potential An example: Software Configuration Management Space
  9. 9. The New World Order: Containers Codify OS Config 9 ProdDev QA Staging DEV Server/VM QA Server/VM STG Server/VMPROD Server/VM <PROD OS config><STG OS config><QA OS config><DEV OS config> App <code> <APP OS config> App <code> <APP OS config> App <code> <APP OS config> App <code> <APP OS config>
  10. 10. So is this how I build a Docker Image? 10
  11. 11. Jenkins & Docker
  12. 12. How Can You Use Jenkins & Docker Together? +
  13. 13. How Can You Use Jenkins & Docker Together? 1. Run Jenkins Masters & Slaves in Docker 2. Build, Test, & Deploy Docker Images from Jenkins
  14. 14. 1. Run Jenkins Masters & Slaves in Docker Docker (Cloud) – use Docker images as standardized build environments to improve isolation and elasticity Docker Custom Build Environment – specify customized build environments as Docker containers CloudBees Docker Shared Config – manage Docker (or Swarm) host configuration centrally in CloudBees Jenkins Operations Center
  15. 15. 2. Build, Test, & Deploy Docker Images from Jenkins Build and Publish – build projects that have a Dockerfile and push the resultant tagged image to Docker Hub Docker Traceability – identify which build pushed a particular container and displays the build / image details in Jenkins Docker Hub Notification – trigger downstream jobs when a tagged container is pushed to Docker Hub
  16. 16. Jenkins Workflow & Docker
  17. 17. Jenkins Workflow Primer Jenkins powered CD pipelines Jenkins Workflow ProdDev Perf Test BuildCommit Selenium Test Stage Deploy Sonar Test Pipelines Need:  Branching  Looping  Restarts  Checkpoints  Manual Input ??
  18. 18. Key Workflow Features 18  Entire flow is one concise Groovy script using Workflow DSL • For loops, try-finally, fork-join …  Can restart Jenkins while flow is running  Allocate slave nodes and workspaces • As many as you want, when you want  Stages throttle concurrency of builds  Human input/approval integrated into flow  Standard project concepts: SCM, artifacts, plugins
  19. 19. Jenkins Workflow + Docker
  20. 20. Pipeline Stages 20 Build and Unit Test App Test Docker Image Publish Docker Image SCM Checkout mvn package mvn sonar:sonar mvn verify docker build docker tag docker run notify cucumber war img Sonar Analysis Prepare Release Build Docker Image Int Test docker push image.inside withServer
  21. 21. Build, unit test and package 21 Build and Unit Test App Test Docker Image Publish Docker Image SCM Checkout mvn package mvn sonar:sonar mvn verify docker build docker Tag docker run notify cucumber war img Sonar Analysis Prepare Release Build Docker Image Int Test docker push image.inside withServer
  22. 22. Build, unit test and package stage 'Build App’ node('docker') { docker.image(‘maven:3.3.3-jdk-8’).inside(‘-v /data:/data’ { mkdir –p /data/mvn writeFile file: 'settings.xml', text: ”(………)" git 'https://github.com/cloudbees/mobile-deposit-api.git’ sh 'mvn –s settings.xml clean package’ … Specify the Stage Name Specify the slave label Custom Build Env Mount volume from slave .m2 repo location co and build
  23. 23. Defining a Docker Slave Specify Image as template Assign labels
  24. 24. Test the app 24 Build and Unit Test App Test Docker Image Publish Docker Image SCM Checkout mvn package mvn sonar:sonar mvn verify docker build docker Tag docker run notify cucumber war img Sonar Analysis Prepare Release Build Docker Image Int Test docker push image.inside withServer
  25. 25. Test the app node('docker') { docker.image(‘maven:3.3.3-jdk-8’).inside(‘-v /data:/data’ { … stage 'Sonar analysis’ sh 'mvn -s settings.xml sonar:sonar’ stage 'Integration-test’ sh 'mvn -s settings.xml verify’ step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml']) } … In same env as build Sonar tests Run API Tests
  26. 26. Build, test and publish Docker image 26 Build and Unit Test App Test Docker Image Publish Docker Image SCM Checkout mvn package mvn sonar:sonar mvn verify docker build docker Tag docker run notify cucumber war img Sonar Analysis Prepare Release Build Docker Image Int Test docker push image.inside withServer
  27. 27. Build, test and publish Docker image docker.withServer('tcp://192.168.99.100:2376', 'slave-docker-us-east-1-tls'){ stage 'Build Docker image’ def mobileDepositApiImage dir('.docker') { sh "mv ../target/*-SNAPSHOT.jar mobile-deposit-api.jar” mobileDepositApiImage = docker.build "harniman/mobile-deposit-api:${buildVersion}” } stage 'Test Docker image’ container=mobileDepositApiImage.run("--name mobile-deposit-api -p 8080:8080”) sh "curl http://<user>:<token>@<host>:8080/docker-traceability/submitContainerStatus ...... // insert cucumber tests here stage 'Publish Docker image’ withDockerRegistry(registry: [credentialsId: 'dockerhub-harniman']) { mobileDepositApiImage.push() } } Bind to docker host Change directory Launch container Build docker image Bind to registry Push image Submit traceability report
  28. 28. 28 Tagged Version a Tagged Image in Docker Hub a
  29. 29. Traceability
  30. 30. Traceability Builds on existing Jenkins artifact traceability Allows the tracking of the creation and use of Docker containers in Jenkins and their future use. Combine with artifact fingerprinting for a comprehensive solution Each Build shows the image fingerprints created 30 Identify which build pushed a particular container and display the build / image details in Jenkins Image fingerprints
  31. 31. Traceability – registering events Jenkins can track actions against this image such as: • Creating a container • Container events such as start/stop To achieve this, it is necessary to call the Traceability API – see $(JENKINS_URL)/docker-traceability/api/ There are two endpoints to submit events to: 31 /docker- traceability/submitContai nerStatus Allows to submit the current container status snapshot with a minimal set of parameters. Outputs of docker inspect $(containerId) can be directly submitted to Jenkins server using this command. /docker- traceability/submitReport Submits a report using the extended JSON API. This endpoint can be used by scripts to submit the full available info about the container and its environment in a single command.
  32. 32. Traceability – registering events - example Workflow usage example: 32 container = mobileDepositApiImage.run("--name mobile-deposit-api -p 8080:8080") sh "curl http://<user>:<token>@<host>:8080/docker-traceability/submitContainerStatus --data-urlencode status=deployed --data-urlencode inspectData="$(docker inspect $container.id)" --data-urlencode environment=test --data-urlencode hostName=mymac --data-urlencode imageName=harniman/mobile-deposit-api" Spin up container Notify Jenkins
  33. 33. Docker Traceability View 33 Docker Traceability Container
  34. 34. Container Use View 34 Deployment Events Link to Build
  35. 35. Dockerhub Notifications
  36. 36. Docker Hub Notification Trigger downstream jobs when a tagged container is pushed to Docker Hub The Docker Hub Notification Trigger plugin lets you configure Jenkins to trigger builds when an image is pushed to Docker Hub. E.g. to run verification for the container. What are the steps Set up a WebHook Account for Notification Set up your Docker Registry to make callbacks on Image events Set up your builds 36
  37. 37. Docker Hub Notification – Docker Registry Webhook 37 In the format: http://<user>:<token>@<jenkins_url>/dockerhub-webhook/notify
  38. 38. Docker Hub Notification – Job Set up 38 Configure Trigger
  39. 39. In Conclusion
  40. 40. Docker and Jenkins with Workflow is the proven CD Platform 40 + TESTING STAGING PRODUCTION Workflow CD Pipeline Triggers: • New application code (i.e. feature, bug, etc.) • Updated certified stack (security fix in Linux, etc.) … will lead to a new gold image being built and available for… … TESTING … STAGING … PRODUCTION All taking place in a standardized/similar/consistent environment <OS config> Company “Gold” Docker Img (~per app) App <code> (git, etc.) <OS config> Certified Docker Images (Ubuntu, etc.) Jenkins Workflow
  41. 41. CloudBees: Leading the Way for Docker and CD Docker Workflow – Provides first-class support for Jenkins Workflow to build real world CD pipelines for containerized applications using Jenkins and Docker Build and Publish – Builds projects that have a Dockerfile and pushes the resultant tagged image to Docker Hub Docker Hub Notification – Triggers downstream jobs when a tagged container is pushed to Docker Hub Docker Traceability – Identifies which build pushed a particular container that is running in production and displays that on the Jenkins builds page Docker – Uses Docker containers as standardized build environments to improve isolation and elasticity – Dockerized Build Slaves Docker Custom Build Environment – Specifies customized build environments as Docker containers
  42. 42. Getting started Docker plugin documentation: http://documentation.cloudbees.com/docs/cje-user-guide/docker- workflow.html Workflow tutorial: https://github.com/jenkinsci/workflow-plugin/blob/master/TUTORIAL.md Example Source Code https://github.com/harniman/mobile-deposit-api/blob/master/flow.groovy
  43. 43. How Do You Manage CD at Enterprise Scale? 43 CloudBees Jenkins Platform Jenkins at Enterprise Scale for CI and CD
  44. 44. Thank you! Nigel Harniman @harniman nharniman@cloudbees.com

×