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.

Jenkins days workshop pipelines - Eric Long

1.533 visualizaciones

Publicado el

Austin Texas November 16, 2016

Publicado en: Software

Jenkins days workshop pipelines - Eric Long

  1. 1. 1
  2. 2. Jenkins Days Workshop Let’s Build a Jenkins Pipeline
  3. 3. 3 Goals for today Install Jenkins Enterprise Create a Jenkins Pipeline Try the basics See what else is possible and get connected! Welcome! 1 2 3 4
  4. 4. About your guide: Eric Long 4 Hands-on Delivery experience on CloudBees Jenkins and Pipelines Senior DevOps Consultant @ericlongtx
  5. 5. What is Jenkins? 5 Well that’s a funny question. A CI server? A CD Server? An automation server
  6. 6. Easy to Start 6 java -jar jenkins.war
  7. 7. CloudBees Jenkins Enterprise … part of CloudBees Jenkins Platform 7 Jenkins for the EnterpriseCommunity Innovation
  8. 8. What is Jenkins Pipeline? ● Formerly known as Jenkins Workflow, Jenkins Pipeline was introduced in 2014 ● New job type – a single Groovy script using an embedded DSL - no need to jump between multiple job configuration screens to see what is going on ● Durable - keeps running even if Jenkins master is not ● Distributable – a Pipeline job may be run across an unlimited number of nodes, including in parallel ● Pausable – allows waiting for input from users before continuing to the next step ● Visualized - Pipeline Stage View provides status at-a-glance dashboards to include trending 8
  9. 9. Lab Exercise: Install Jenkins Enterprise
  10. 10. Install Jenkins Enterprise 10 Have Docker? No Docker? No problem. docker run -d -p 80:8080 --name cje -v /var/run/docker.sock:/var/run/docker.sock beedemo/jenkins- enterprise-trial
  11. 11. Finish Install Unlock Jenkins Request a trial license Install suggested plugins Create First Admin User 11 docker logs -f cje Jenkins initial setup is required. An admin user has been created and a password generated. Please use the following password to proceed to installation: 1a90312e459b4d4693f18d48d102d6c6
  12. 12. Lab Exercise: Create a Pipeline
  13. 13. Pipeline: a new job type
  14. 14. Key Benefits ✓ Long-running ✓ Durable ✓ Scriptable ✓ One-place for Everything Pipeline: a new job type ✓ Makes building Pipelines Simple
  15. 15. Domain Specific Language 15 Simple, Groovy-based DSL (“Domain Specific Language”) to manage build step orchestration Can be defined as DSL in Jenkins or as Jenkinsfile in SCM Groovy is widely used in Jenkins ecosystem. You can leverage the large Jenkins Groovy experience If you don’t like/care about Groovy, just consider the DSL as a dedicated simple syntax
  16. 16. Snippet Generator 16
  17. 17. Create a Pipeline - core steps 17 stage - group your steps into its component parts and control concurrency node - schedules the steps to run by adding them to Jenkins' build queue and creates a workspace specific to this pipeline sh (or bat) - execute shell (*nix) or batch scripts (Windows) just like freestyle jobs, calling any tools in the agent
  18. 18. Create a Pipeline stage('build') echo 'hello from jenkins master' node { sh 'echo hello from jenkins agent' } stage('test') echo 'test some things' stage('deploy') echo 'deploy some things' 18
  19. 19. Lab Exercise: Checkout from SCM
  20. 20. Files stash - store some files for later use unstash - retrieve previously stashed files (even across nodes) writeFile - write a file to the workspace readFile - read a file from the workspace 20
  21. 21. Checkout from SCM and stash Files stage('build') node { git '' writeFile encoding: 'UTF-8', file: 'config', text: 'version=1' stash includes: 'pom.xml,config', name: 'pom-config' } stage('test') node { unstash 'pom-config' configValue = readFile encoding: 'UTF-8', file: 'config' echo configValue } 21
  22. 22. try up to N times retry(5) { // some block } wait for a condition waitUntil { // some block } Flow Control 22 wait for a set time sleep time: 1000, unit:'NANOSECONDS' timeout timeout(time: 30, unit: 'SECONDS'){ // some block }
  23. 23. You can also rely on Groovy control flow syntax! while(something) { // do something if (something_else) { // do something else } } Flow Control 23 try{ //some things }catch(e){ // }
  24. 24. Advanced Flow Control input - pause for manual or automated approval parallel - allows simultaneous execution of build steps on the current node or across nodes, thus increasing build speed parallel 'quality scan': { node {sh 'mvn sonar:sonar'} }, 'integration test': { node {sh 'mvn verify'} } checkpoint - capture the workspace state so it can be reused as a starting point for subsequent runs
  25. 25. Lab Exercise: Input and Checkpoints
  26. 26. Input Approval & Checkpoints 26 stage('deploy') input message: 'Do you want to deploy?' node { echo 'deployed' }
  27. 27. Input Approval & Checkpoints 27 checkpoint 'testing-complete' stage('approve') mail body: "Approval needed for '${env.JOB_NAME}' at ${env.BUILD_URL}, subject: "${env.JOB_NAME} Approval", to: "" timeout(time: 7, unit: 'DAYS') { input message: 'Do you want to deploy?', parameters: [string(defaultValue: '', description: 'Provide any comments regarding decision.', name: 'Comments')], submitter: 'ops' }
  28. 28. Lab Exercise: Tool Management
  29. 29. tool Step ● Binds a tool installation to a variable ● The tool home directory is returned ● Only tools already configured are available def mvnHome = tool 'M3' sh "${mvnHome}/bin/mvn -B verify" 29
  30. 30. Use Docker Containers ● The CloudBees Docker Pipeline plugin allows running steps inside a container ● You can even build the container as part of the same Pipeline docker.image('maven:3.3.3-jdk-8').inside() { sh 'mvn -B verify' } 30
  31. 31. Lab Exercise: Pipeline as Code
  32. 32. Pipeline script in SCM 32
  33. 33. 33 Pipeline-as-Code
  34. 34. Pipeline Multibranch 34
  35. 35. Pipeline Shared Libraries 35
  36. 36. What Next?
  37. 37. More Advanced Steps Send email mail body: 'Uh oh.', subject: 'Build Failed!', to: '' step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: '']) Deploy to Amazon Elastic Beanstalk wrap([$class: 'AmazonAwsCliBuildWrapper', credentialsId: 'aws- beanstalk-credentials', defaultRegion: 'us-east-1']) { sh 'aws elasticbeanstalk create-application-version' } Integrate with Jira jiraComment(issueKey: "EX-111", body: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) built. Please go to ${env.BUILD_URL}.") 37
  38. 38. @issc29 #JenkinsWorld Docker + Pipelines © 2016 CloudBees, Inc. All Rights Reserved
  39. 39. @issc29 #JenkinsWorld Agent Gartner: “Using Docker to Run Build Nodes is Ideal.” © 2016 CloudBees, Inc. All Rights Reserved Master z Agent Agent
  40. 40. @issc29 #JenkinsWorld Accelerating CD with Containers Workflow CD Pipeline Triggers: ✓ New application code (feature, bug fix, 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 OS environment © 2016 CloudBees, Inc. All Rights Reserved + Jenkins Pipeline TEST STAGE PRODUCTIO N App <code> (git, etc.) Gold Docker Image (~per app) <OS config> Certified Docker Images (Ubuntu, etc.) <OS config>
  41. 41. @issc29 #JenkinsWorld Jenkins Pipeline Docker Commands • withRegistry – Specifies which Docker Registry to use for pushing/pulling • withServer – Specifies which Server to issue Docker commands against • Build – Builds container from Dockerfile • – Runs a container • Image.inside – Runs container and allows you to execute command inside the container • Image.push – Pushed image to Docker Registry with specified credentials © 2016 CloudBees, Inc. All Rights Reserved
  42. 42. @issc29 #JenkinsWorld Pipeline - Docker Example stage 'Build' node('docker-cloud') { checkout scm docker.image('java:jdk-8').inside('-v /data:/data') { sh "mvn clean package" }} © 2016 CloudBees, Inc. All Rights Reserved stage 'Quality Analysis' node('docker-cloud') { unstash 'pom' parallel( JRE8Test: { docker.image('java:jre-8').inside('-v /data:/data') { sh 'mvn -Dmaven.repo.local=/data/mvn/repo verify' } },JRE7Test: { docker.image('java:jre-7').inside('-v /data:/data') { sh 'mvn -Dmaven.repo.local=/data/mvn/repo verify' } }, failFast: true )}
  43. 43. @issc29 #JenkinsWorld Pipeline - Docker Example node('docker-cloud') { stage 'Build Docker Image' dir('target') { mobileDepositApiImage = "beedemo/mobile-deposit-api:${dockerTag}" } stage 'Publish Docker Image' withDockerRegistry(registry: [credentialsId: 'docker-hub-beedemo']) { mobileDepositApiImage.push() } } © 2016 CloudBees, Inc. All Rights Reserved
  44. 44. Get Involved! 44 Twitter @ericlongtx