O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Jenkins 2: Up and Running

Book Description

Design, implement, and execute continuous delivery pipelines with a level of flexibility, control, and ease of maintenance that was not possible with Jenkins before. With this practical book, build administrators, developers, testers, and other professionals will learn how the features in Jenkins 2 let you define pipelines as code, leverage integration with other key technologies, and create automated, reliable pipelines to simplify and accelerate your DevOps environments.

Author Brent Laster shows you how Jenkins 2 is significantly different from the more traditional, web-only versions of this popular open source automation platform. If you’re familiar with Jenkins and want to take advantage of the new technologies to transform your legacy pipelines or build new modern, automated continuous delivery environments, this is your book.

  • Create continuous delivery pipelines as code with the Jenkins domain-specific language
  • Get practical guidance on how to migrate existing jobs and pipelines
  • Harness best practices and new methods for controlling access and security
  • Explore the structure, implementation, and use of shared pipeline libraries
  • Learn the differences between declarative syntax and scripted syntax
  • Leverage new and existing project types in Jenkins
  • Understand and use the new Blue Ocean graphical interface
  • Take advantage of the capabilities of the underlying OS in your pipeline
  • Integrate analysis tools, artifact management, and containers

Table of Contents

  1. Foreword
  2. Preface
    1. How to Use This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Safari
    5. How to Contact Us
    6. Acknowledgments
  3. 1. Introducing Jenkins 2
    1. What Is Jenkins 2?
      1. The Jenkinsfile
      2. Declarative Pipelines
      3. Blue Ocean Interface
      4. New Job Types in Jenkins 2
    2. Reasons for the Shift
      1. DevOps Movement
      2. Assembling Pipelines
      3. Resumability
      4. Configurability
      5. Sharing Workspaces
      6. Specialized Knowledge
      7. Access to Logic
      8. Pipeline Source Management
      9. Competition
    3. Meeting the Challenges
    4. Compatibility
      1. Pipeline Compatibility
      2. Plugin Compatibility
      3. Checking Compatibility
    5. Summary
  4. 2. The Foundations
    1. Syntax: Scripted Pipelines Versus Declarative Pipelines
      1. Choosing Between Scripted and Declarative Syntax
    2. Systems: Masters, Nodes, Agents, and Executors
      1. Master
      2. Node
      3. Agent
      4. Executor
      5. Creating Nodes
    3. Structure: Working with the Jenkins DSL
      1. node
      2. stage
      3. steps
    4. Supporting Environment: Developing a Pipeline Script
      1. Starting a Pipeline Project
      2. The Editor
      3. Working with the Snippet Generator
      4. Running a Pipeline
      5. Replay
    5. Summary
  5. 3. Pipeline Execution Flow
    1. Triggering Jobs
      1. Build After Other Projects Are Built
      2. Build Periodically
      3. GitHub Hook Trigger for GitSCM Polling
      4. Poll SCM
      5. Quiet Period
      6. Trigger Builds Remotely
    2. User Input
      1. input
      2. Parameters
      3. Return Values from Multiple Input Parameters
      4. Parameters and Declarative Pipelines
    3. Flow Control Options
      1. timeout
      2. retry
      3. sleep
      4. waitUntil
    4. Dealing with Concurrency
      1. Locking Resources with the lock Step
      2. Controlling Concurrent Builds with Milestones
      3. Restricting Concurrency in Multibranch Pipelines
      4. Running Tasks in Parallel
    5. Conditional Execution
    6. Post-Processing
      1. Scripted Pipelines Post-Processing
      2. Declarative Pipelines and Post-Processing
    7. Summary
  6. 4. Notifications and Reports
    1. Notifications
      1. Email
      2. Collaboration Services
    2. Reports
      1. Publishing HTML Reports
    3. Summary
  7. 5. Access and Security
    1. Securing Jenkins
      1. Enabling Security
      2. Other Global Security Settings
    2. Credentials in Jenkins
      1. Credential Scopes
      2. Credential Domains
      3. Credential Providers
      4. Credential Stores
    3. Administering Credentials
      1. Selecting Credential Providers
      2. Selecting Credential Types
      3. Specifying Credential Types by Provider
    4. Creating and Managing Credentials
      1. Context Links
      2. Adding a New Domain and Credential
      3. Using the New Domain and Credential
    5. Advanced Credentials: Role-Based Access
      1. Basic Use
      2. Manage Roles
      3. Assign Roles
      4. Role Strategy Macros
    6. Working with Credentials in the Pipeline
      1. Username and Password
      2. SSH Keys
      3. Token Credentials
    7. Controlling Script Security
      1. Script Checking
      2. Script Approval
    8. Groovy Sandboxing
    9. Using Jenkins Credentials with Vault
      1. Approach
      2. Setup
      3. Creating a Policy
      4. Authentication
      5. Using Vault in Jenkins
    10. Summary
  8. 6. Extending Your Pipeline
    1. Trusted Versus Untrusted Libraries
    2. Internal Versus External Libraries
      1. Internal Libraries
      2. External Libraries
    3. Getting a Library from the Source Repository
      1. Modern SCM
      2. Legacy SCM
    4. Using Libraries in Your Pipeline Script
      1. Automatic Downloading of Libraries from Source Control
      2. Loading Libraries into Your Script
    5. Library Scope Within Jenkins Items
    6. Library Structure
    7. Sample Library Routine
      1. Structure of Shared Library Code
    8. Using Third-Party Libraries
    9. Loading Code Directly
    10. Loading Code from an External SCM
    11. Replaying External Code and Libraries
    12. A Closer Look at Trusted Versus Untrusted Code
    13. Summary
  9. 7. Declarative Pipelines
    1. Motivation
      1. Not Intuitive
      2. Getting Groovy
      3. Additional Assembly Required
    2. The Structure
      1. Block
      2. Section
      3. Directives
      4. Steps
      5. Conditionals
    3. The Building Blocks
      1. pipeline
      2. agent
      3. environment
      4. tools
      5. options
      6. triggers
      7. parameters
      8. libraries
      9. stages
      10. post
    4. Dealing with Nondeclarative Code
      1. Check Your Plugins
      2. Create a Shared Library
      3. Place Code Outside of the Pipeline Block
      4. The script Statement
    5. Using parallel in a Stage
    6. Script Checking and Error Reporting
    7. Declarative Pipelines and the Blue Ocean Interface
    8. Summary
  10. 8. Understanding Project Types
    1. Common Project Options
      1. General
      2. Source Code Management
      3. Build Triggers
      4. Build Environment
      5. Build
      6. Post-Build Actions
    2. Types of Projects
      1. Freestyle Projects
      2. The Maven Project Type
      3. The Pipeline Project Type
      4. The External Job Project Type
      5. The Multiconfiguration Project Type
      6. Ivy Projects
      7. Folders
      8. Multibranch Pipeline Projects
      9. GitHub Organization Projects
      10. Bitbucket Team/Project Projects
    3. Summary
  11. 9. The Blue Ocean Interface
    1. Part 1: Managing Existing Pipelines
      1. The Dashboard
      2. The Project-Specific Page
      3. The Run Page
    2. Part 2: Working with the Blue Ocean Editor
      1. Creating a New Pipeline Without an Existing Jenkinsfile
      2. Working in the Editor
      3. Editing an Existing Pipeline
      4. Importing and Editing Existing Pipelines
      5. Working with Pipelines from Non-GitHub Repositories
    3. Summary
  12. 10. Conversions
    1. Common Preparation
      1. Logic and Accuracy
      2. Project Type
      3. Systems
      4. Access
      5. Global Configuration
      6. Plugins
      7. Shared Libraries
    2. Converting a Freestyle Pipeline to a Scripted Pipeline
      1. Source
      2. Compile
      3. Unit Tests
      4. Integration Testing
      5. Migrating the Next Parts of the Pipeline
    3. Converting from a Jenkins Pipeline Project to a Jenkinsfile
      1. Approach
      2. Final Steps
    4. Converting from a Scripted Pipeline to a Declarative Pipeline
      1. Sample Pipeline
      2. The Conversion
      3. Completed Conversion
    5. General Guidance for Conversions
    6. Summary
  13. 11. Integration with the OS (Shells, Workspaces, Environments, and Files)
    1. Using Shell Steps
      1. The sh Step
      2. The bat Step
      3. The powershell Step
    2. Working with Environment Variables
      1. The withEnv Step
    3. Working with Workspaces
      1. Creating a Custom Workspace
      2. Cleaning a Workspace
    4. File and Directory Steps
      1. Working with Files
      2. Working with Directories
      3. Doing More with Files and Directories
    5. Summary
  14. 12. Integrating Analysis Tools
    1. SonarQube Survey
    2. Working with Individual Rules
    3. Quality Gates and Profiles
    4. The Scanner
    5. Using SonarQube with Jenkins
      1. Global Configuration
      2. Using SonarQube in a Freestyle Project
      3. Using SonarQube in a Pipeline Project
      4. Leveraging the Outcome of the SonarQube Analysis
      5. SonarQube Integration Output with Jenkins
    6. Code Coverage: Integration with JaCoCo
      1. About JaCoCo
      2. Integrating JaCoCo with the Pipeline
      3. JaCoCo Output Integration with Jenkins
    7. Summary
  15. 13. Integrating Artifact Management
    1. Publishing and Retrieving Artifacts
    2. Setup and Global Configuration
    3. Using Artifactory in a Scripted Pipeline
    4. Performing Other Tasks
      1. Downloading Specific Files to Specific Locations
      2. Uploading Specific Files to Specific Locations
      3. Setting Build Retention Policies
      4. Build Promotion
    5. Integration with a Declarative Pipeline
    6. Artifactory Integration with Jenkins Output
    7. Archiving Artifacts and Fingerprinting
    8. Summary
  16. 14. Integrating Containers
    1. Configured as a Cloud
      1. Global Configuration
      2. Using Docker Images as Agents
      3. Using Cloud Images in a Pipeline
    2. Agent Created on the Fly for a Declarative Pipeline
    3. Docker Pipeline Global Variable
      1. Global Variables
      2. Docker Application Global Variable Methods
        1. Docker Image Global Variable Methods
          1. Docker Container Global Variable Methods
    4. Running Docker via the Shell
    5. Summary
  17. 15. Other Interfaces
    1. Using the Command-Line Interface
      1. Using the Direct SSH Interface
      2. Using the CLI Client
    2. Using the Jenkins REST API
      1. Filtering Results
      2. Initiating Builds
    3. Using the Script Console
    4. Summary
  18. 16. Troubleshooting
    1. Diving into Pipeline Steps
    2. Dealing with Serialization Errors
      1. Continuous Passing Style
      2. Serializing Pipelines
      3. NotSerializableException
      4. Handling Nonserializable Errors
    3. Identifying the Line in Your Script that Caused an Error
    4. Handling Exceptions in a Pipeline
    5. Using Nondeclarative Code Within a Declarative Pipeline
    6. Unapproved Code (Script and Method Approval)
    7. Unsupported Operations
    8. System Logs
    9. Timestamps
    10. Pipeline Durability Settings
    11. Summary
  19. Index