35,99 €
Continuous integration and continuous delivery (CI/CD) are ubiquitous concepts in modern development. Azure Pipelines is one of the most popular services that you can utilize for CI/CD, and this book shows you how it works by taking you through the process of building and automating CI/CD systems using Azure Pipelines and YAML, simplifying integration with Azure resources and reducing human error.
You’ll begin by getting an overview of Azure Pipelines and why you should use it. Next, the book helps you get to grips with build and release pipelines, and then builds upon this by introducing the extensive power of YAML syntax, which you can use to implement and configure any task you can think of. As you advance, you’ll discover how to integrate Infrastructure as Code tools, such as Terraform, and perform code analysis with SonarQube. In the concluding chapters, you’ll delve into real-life scenarios and hands-on implementation tasks with Microsoft Azure services, AWS, and cross-mobile application with Flutter, Google Firebase, and more.
By the end of this book, you’ll be able to design and build CI/CD systems using Azure Pipelines with consummate ease, write code using YAML, and configure any task that comes to mind.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Veröffentlichungsjahr: 2023
Implementing CI/CD Using Azure Pipelines
Manage and automate the secure flexible deployment of applications using real-world use cases
Piti Champeethong
Roberto Mardeni
BIRMINGHAM—MUMBAI
Copyright © 2023 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Preet Ahuja
Publishing Product Manager: Surbhi Suman
Book Project Manager: Deeksha Thakkar
Senior Editor: Shruti Menon
Technical Editor: Yash Bhanushali
Copy Editor: Safis Editing
Proofreader: Safis Editing
Indexer: Tejal Daruwale Soni
Production Designer: Prashant Ghare
DevRel Marketing Coordinator: Rohan Dobhal
Senior DevRel Marketing Coordinator: Linda Pearlson
First published: November 2023
Production reference: 1301123
Published by
Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK
ISBN 978-1-80461-249-1
www.packtpub.com
To my beautiful wife, Promyok, for always supporting me and challenging me to do better.
– Piti Champeethong
To my beautiful and incredibly talented wife, Marirose, for supporting me, always challenging me, and encouraging me to improve myself. To my kids, Joseph and Lamysse, for being the inspiration to never stop learning and always wanting to provide more.
– Roberto Mardeni
Piti Champeethong is a senior consulting engineer at MongoDB, Singapore. He has been a part of the software development community for over 20 years and specializes in database application development and CI/CD implementation. He is a Microsoft Certified Trainer, lecturer, and community leader. He lives in Thailand and engages with Thai tech communities, such as the MongoDB Thailand User Group and the Thailand .NET community by speaking at various events. He has made significant contributions to public repositories on .NET and MongoDB technologies, supporting the growth of the Thai tech community.
I want to thank all the people who have been close to me and supported me, especially my wife and family. I would also like to thank the Packt team for their efforts and support.
Roberto Mardeni has been working in the IT industry for over two decades. He specializes in software development, architecture, and DevSecOps practices around many different application platforms and CI/CD tools, typically focusing on Microsoft .NET and other open source programming languages. He has been working since 2017 as an application innovation specialist on the enterprise sales side at Microsoft, supporting their cloud business. He lives in the United States and helps some of the largest independent software vendors to adopt the Azure platform. This is his first foray into technical writing, but he has contributed to the open source community in different GitHub public repositories of his own and contributed to others as well.
First and foremost, I would like to thank my loving and patient wife and family for their continued support, patience, and encouragement throughout the long process of writing this book. Thanks also to the Packt team for their invitation to collaborate on the writing of this book.
James Wasson, is a technology leader at Heartland Payments Systems and a global ambassador for the DevOps Institute. He has over 15 years of expertise in technology and building large-scale organizations. Currently, he leads a global team of cloud engineers and site reliability engineers to support all Heartland SaaS products. He is dedicated to building beautiful people, character, and software, in that order.
Thank you to my wife, Deanna, for all her support in our lives, and for those I get the chance to serve.
Aditya Sharma, a 16-year veteran of Microsoft technologies and a skilled Azure cloud engineer, excels in global project success. His track record showcases cost-effective cloud solutions, enhancing application performance, and upholding security in cloud environments. Aditya boasts Microsoft certifications, including Azure Developer Associate and DevOps Engineer Expert, underscoring his tech commitment. Proficient in Azure services such as App Service and Logic Apps, Aditya optimizes performance and cost efficiency. His passion for innovation and problem-solving sets him apart as a valuable team asset.
I would like to extend my heartfelt gratitude to my family and friends for their unwavering understanding and support throughout my journey. As a highly skilled .NET technologies project manager, I have had the privilege of leading an exceptional team of professionals, and it is their unwavering dedication, expertise, and collaborative spirit that have driven our project to fruition.
Continuous integration (CI) is the practice of using automated tools to compile automatically and continuously, based on changes made by developers to source code, and packaging and testing them to ensure they are stable and able to provide the expected functionality.
Continuous delivery (CD) uses the artifacts created via CI and deploys those applications without human intervention to end users, ensuring these are always updated with the latest versions and, in most cases, performing additional verifications in multiple environments before reaching the final end users.
All of this is possible by taking advantage of Azure Pipelines, one of the leading platforms to support all aspects of the software development life cycle (SDLC); however, in this book, we will focus on the CI/CD aspects and how to take advantage of the features available to effect the different task automation options under the DevOps umbrella, even touching on the DevSecOps aspects.
In this book, we provide you with the tools to get started, by learning the basic concepts and building from there to more complex scenarios. This will lead to end-to-end scenarios that enable your software development teams with the tools to automate, every step of the way, the delivery of applications, from source code to a running platform.
From beginners to the most advanced users, anybody looking to better understand how to take advantage of Azure Pipelines can benefit from this book.
The three main personas who are the target audience of this content are as follows:
Software developers: They will learn how they can automatically build and deploy their software products, regardless of the target platform, at very early stages to speed up the SDLCDevOps engineers: They will learn how Azure Pipelines can support any automation need, no matter the stage in the process, infusing quality and checks every step of the waySecurity engineers: They will learn how they can integrate their tools in the CI/CD process to enforce security and quality at the beginning of the build and deployment processChapter 1, Understanding Azure Pipelines, provides an introduction to CI/CD, Azure DevOps, Azure Pipelines, and its components. It explains why an Azure pipeline is the right choice for certain cases, introduces other services under Azure DevOps such as Azure Repos, and guides you through setting up a new project, setting up a self-hosted agent, preparing a pipeline environment, and configuring agent pools and deployment groups.
Chapter 2, Creating Build Pipelines, teaches you how to create and manage pipelines, stages, jobs, tasks, triggers, and artifacts in Azure DevOps, as well as about running pipelines after code pushes to Azure Repos.
Chapter 3, Setting Variables, Environments, Approvals, and Checks, covers the creation of service connections, variable groups, secret files, and release pipelines in Azure DevOps. It also explains setting up service accounts for Azure Repos and GitHub connections. Additionally, you will learn about securely storing secret keys and using environments, with approvals and checks for stage progression control.
Chapter 4, Extending Advanced Azure Pipelines Using YAML, helps you to understand how to use YAML to create a build and release pipeline. It discusses in detail what the YAML syntax is to create stages, jobs, and tasks for web application deployment.
Chapter 5, Implementing the Build Pipeline Using Deployment Tasks, explores how to create and reuse a build task for the building process. This chapter covers the popular Node.js, NPM, .NET, Docker, and SQL Server deployment tasks using the YAML syntax.
Chapter 6, Integrating Testing, Security Tasks, and Other Tools, helps you understand how the extensibility of Azure Pipelines with other tools works. This chapter covers the popular tools SonarQube for code analysis and Jenkins for artifacts.
Chapter 7, Monitoring Azure Pipelines, teaches you how to monitor Azure Pipelines and related tasks, such as build tasks, deployment tasks, and pipeline agents. You will also learn how to build monitoring into pipelines to determine whether deployments improve or degrade the quality of a system.
Chapter 8, Provisioning Infrastructure Using Infrastructure as Code, examines how to create and reuse deployment tasks for the Infrastructure as Code (IaC) process. This chapter covers the popular IaC tools Terraform, Azure Bicep, and an ARM template using YAML syntax.
Chapter 9, Implementing CI/CD for Azure Services, shows you how to create the YAML and pipelines for Azure service deployment. You will learn how to set up and deploy applications on Azure App Service, Azure Kubernetes Service (AKS), Azure Container Apps, and Azure Container Instances (ACI).
Chapter 10, Implementing CI/CD for AWS, explores how to create YAML and pipelines to deploy containerized applications on different services such as AWS Lightsail, Elastic Kubernetes Service (EKS), and Elastic Container Service (ECS).
Chapter 11, Automating CI/CD for Cross-Mobile Applications by Using Flutter, dives into how to create a pipeline using YAML to automate the CI/CD of a mobile application build and release process. You will also learn how to implement YAML pipelines to deploy Flutter on Apple TestFlight and the Google Play Console, staging the environment of an end-to-end process.
Chapter 12, Navigating Common Pitfalls and Future Trends in Azure Pipelines, teaches you about common mistakes and suggests how to avoid them. This chapter also looks at potential future trends in Azure Pipelines.
You will need to have a basic understanding of building and deploying applications with automation; however, this book will walk you through how this is done in Azure Pipelines. Each chapter has specific technical requirements.
Software/hardware covered in the book
Operating system requirements
Docker
Windows, Linux, or macOS
Visual Studio Code
Windows, Linux, or macOS
If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Implementing-CI-CD-Using-Azure-Pipelines. If there’s an update to the code, it will be updated in the GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: Add the following basic script – echo "Hello Second Task on Linux".
A block of code is set as follows:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "", "apiProfile": "", "parameters": { }, "variables": { }, "functions": [ ], "resources": [ ], "outputs": { } }Any command-line input or output is written as follows:
$id=az ad sp list –display-name azure-pipelines –query "[].id" -o tsvBold: Indicates a new term, an important word, or words that you see on screen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “Start by clicking the Environments option under Pipelines in the main menu.”
Tips or important notes
Appear like this.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Once you’ve read Implementing CI/CD Using Azure Pipelines, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.
Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.
Thanks for purchasing this book!
Do you like to read on the go but are unable to carry your print books everywhere?
Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily
Follow these simple steps to get the benefits:
Scan the QR code or visit the link below:https://packt.link/free-ebook/978-1-80461-249-1
Submit your proof of purchaseThat’s it! We’ll send your free PDF and other benefits to your email directlyThis part will walk you through the basics of Azure Pipelines, help you understand its concepts, and show you how to get started quickly to implement automated build and deployment jobs.
This part has the following chapters:
Chapter 1, Understanding Azure PipelinesChapter 2, Creating Build PipelinesChapter 3, Setting Variables, Environments, Approvals, and ChecksChapter 4, Extending Advanced Azure Pipelines Using YAMLThis book will be one of your favorite books in the Microsoft DevOps world as it provides a comprehensive guide to learning all about Azure Pipelines and will allow you to become an experienced Azure DevOps engineer. An Azure DevOps engineer is an individual who is responsible for designing and implementing continuous integration and continuous deployment (CI/CD) pipelines using the Azure Pipelines service, which is a component of Azure DevOps. Azure DevOps is a group of Microsoft services that help a project team achieve the project’s goal.
In this chapter, you will be introduced to the CI/CD and Azure DevOps concepts in greater detail and will learn about the advantages of using Azure Pipelines to create CI/CD pipelines for the application deployment process. More specifically, in this chapter, you will learn about the following topics:
What is CI/CD?Introducing Azure DevOpsIntroducing Azure Pipelines and its componentsComparing Azure Pipelines with other CI/CD toolsSetting up agent poolsCreating a personal access token (PAT)Setting up and updating self-host agentsSetting up deployment groupsYou can find the code for this chapter at https://github.com/PacktPublishing/Implementing-CI-CD-Using-Azure-Pipelines/tree/main/ch01.
CI/CD is the workflow process for automation development and deployment that developers should know about to improve their skills.
CI is the workflow process for automating the process of building and testing code whenever a team member commits changes to Git, which is a form of version control that’s run on a source control repository platform such as Azure Repos, GitHub, GitLab, and others. CI creates a modern culture for all developers to share their code, including unit tests, by merging all changes into a shared version control repository after finishing a small task. CI runs based on committing code triggers to grab the latest code from the shared version control repository to build, test, and validate any branch that they commit. Using CI allows you to rapidly discover error code issues and correct them to ensure all developer code is of good quality.
CD involves automating the process of building, testing, configuring, and deploying from the CI workflow process to specific environments, such as QA, staging, and production.
This workflow is illustrated in the following figure:
Figure 1.1 – CI/CD diagram
CI/CD reduces human error and the routine operation of the manual build, test, and deploy stages for any developer. It helps the developer focus only on application development.
This book will focus on the CI/CD tool Azure Pipelines, which is a comprehensive service for DevOps and a part of the ecosystem of services in Azure DevOps. Before we look at this further, let’s introduce Azure DevOps.
Many CI/CD tools are used to support modern software development, such as Azure Pipelines, GitLab CI/CD, GitHub Actions, and Bitbucket Pipelines. One of the most widely used is Azure Pipelines; this is a part of Azure DevOps, which consists of the following five services:
Azure Boards is an Azure DevOps sub-service that’s used to track all tasks related to a project conveniently in one place. It is suitable for teamwork. It helps with collaboration because it supports Kanban boards, backlogs, team dashboards, and custom reporting, which can create a connection between the tasks and source version repositories such as GitHub or Azure Repos.Azure Pipelines is an Azure DevOps sub-service that’s used to build, test, integrate, and deploy CI/CD processes. It helps reduce delivery errors and allows teams to focus solely on developing clean and readable code in software development; this service can be accessed through the Azure DevOps web portal (https://dev.azure.com/{your-organization}). This book will focus on this service due to this benefit.Azure Repos is an Azure DevOps sub-service for controlling the version of the source code. It is easy to manage code in one place. Easy maintenance can also help you define rules so that you can deploy code safely to desired environments, such as merge checks or static code analysis after the team creates the pull request. The examples in this book will use Azure Repos.Azure Test Plans is an Azure DevOps sub-service that helps test or quality assurance teams write use case scenarios to easily deliver the test results to the customer. The tester or quality assurance team creates system integration testing (SIT) and user acceptance testing (UAT) on Azure Test Plans. It can display test results as dashboard reports and include comments or feedback. Azure Test Plans also helps the team understand the test process of the project on the same page.Azure Artifacts is an Azure DevOps sub-service that enables developers to share and manage all their packages that result from building code in one place. Developers can publish packages to their feeds and share them within the same team, organization, and even publicly. Developers can also load the packages from different public repositories such as https://www.nuget.org/ or https://www.npmjs.com/. Azure Artifacts also supports multiple package types, such as NuGet, npm, Python, Maven, and Universal Packages.All these services fall under the umbrella of Azure DevOps, which covers the necessary development process for a project. You don’t need to use additional services for development.
Azure Pipelines is a CI/CD platform for building, testing, and deploying your code to a live application. First, let’s take a look at its key components.
There are some key concepts that you need to understand when creating an Azure pipeline:
An agent is the software that runs a job within a server. It can be a Microsoft-hosted agent or a self-hosted agent.A pipeline is a workflow process for CI/CD for your application development. It can define your idea of how to build, test, integrate, and deploy your project.A trigger is an action that calls a pipeline to run.A stage is a flow of defined jobs in a pipeline, and each stage can have one or more jobs. The benefit of using a stage is that you can rerun job(s) under it. This means you do not need to rerun the whole pipeline. For example, let’s say the developer creates a pipeline containing two stages: the build stage and the deployment stage. If the deployment stage fails, then they can only rerun the failed job under the deployment stage.A job is a group of one or more steps set in a stage. It is useful when you need to run a set of steps in a different operating system environment.A step can be a task or script and is the smallest piece of a pipeline:A task is a pre-defined script that your idea can define.A script is an action that uses the command-line interface (CLI), PowerShell, or Bash. It depends on the operating system agent that you choose for a job. For example, if you use a command line to run on a Linux agent, it will use a bash script. PowerShell runs on a macOS agent and will use a PowerShell core for cross-platform scripts.A target is a destination of the pipeline. It can be Azure Artifacts, an Azure resource service (such as Azure App Services, Azure Functions, Azure Container Apps, Azure Kubernetes Services, and so on), or invoke a REST API such as webhooks on Microsoft Teams.Now, let’s look at how these components interact with each other:
Figure 1.2 – Key components
This section described the meaning of and relationship between key objects. Before we take a more in-depth look at the different aspects of this platform, let’s learn how we can start using it.
Two methods can be used for signing up:
Sign up with a Microsoft account: To do this, complete the following steps:Go to https://azure.microsoft.com/en-us/services/devops/pipelines.Click on Start free.Log in with your Microsoft account.Enter a name for your organization.You must always log in to your organization using https://dev.azure.com/{your-organization}.Sign up with a GitHub account: To do this, complete the following steps:Go to https://azure.microsoft.com/en-us/services/devops/pipelines.Click on Start free with GitHub.Log in with your GitHub account.Enter a name for your organization.You must always log in to your organization using https://dev.azure.com/{your-organization}.Once you’ve signed up for an Azure Pipelines account, you are ready to create a new project for building your code and release the built code to the live application.
Creating a new project is the first step after you sign up, before creating any CI/CD pipeline. Upon creating a project, you can set project visibility:
Figure 1.3 – Creating a new project
You can enter a project name and select Visibility, then click Create project.
When you need to work with a team, you must add a new member by inviting one or more team members. Follow these steps to invite a team member:
Click on your project’s name in the web portal and click Project settings:Figure 1.4 – Project settings
Select Teams | Add:Figure 1.5 – Adding a new team member
Enter the email addresses of your team members and click Save:Figure 1.6 – Inviting team members
Now that you’ve invited your collaborators to join your project, let’s delve into how we can start using this service.
There are two ways to create Azure pipelines:
Using the Classic interface (create Azure pipelines from the web Azure DevOps portal) while following these basic steps:Configure Azure Pipelines to use your Azure Repos Git repository.Use Azure Pipelines to create and configure your build and release pipeline using drag and drop from the Azure DevOps portal.Push your code to your version control repository. The pipeline will be automatically initiated by the default trigger and the defined tasks will be executed.Using YAML syntax (create YAML files and commit them to the code repository) while following these basic steps:Configure Azure Pipelines to use your Azure Repos Git repository.Edit your azure-pipelines.yml file by defining your custom build.Push your code to your version control repository. This action runs the default trigger.Let’s illustrate the Azure Pipelines YAML method for ease of understanding:
Figure 1.7 – Azure Pipelines YAML steps
There are different pipeline features available for both these methods, with some available for both and others only featuring in one. We’ll look at these in detail in the next section.
Some pipeline features are only available when using the classic interface or YAML. The following table displays which features are available for which of these methods:
Feature
YAML
Classic
Description
Agents
Yes
Yes
To define the resource where the pipeline can run.
Approvals
Yes
Yes
To define the validation steps for additional checking before completing a deployment stage.
Artifacts
Yes
Yes
To define the library packages for publishing or consuming different package types.
Caching
Yes
Yes
To define an additional task to reduce the build time by allowing outputs or downloaded dependencies to store them on the agents and reuse them again.
Conditions
Yes
Yes
To define the specific conditions before running a job.
Container jobs
Yes
No
To define the specific jobs to run in a container.
Demands
Yes
Yes
To define the specific pipeline to ensure requirements are met before a pipeline stage is run.
Dependencies
Yes
Yes
To define specific requirements for validation before running the next job or stage.
Deployment groups
No
Yes
To define a logical group for the code that will be deployed to the target machines.
Deployment group jobs
Yes
Yes
To define a job to release to a deployment group.
Deployment jobs
Yes
No
To define the deployment steps.
Environment
Yes
No
To define a collection of resources targeted for deployment.
Gates
Yes
Yes
To support automatic collection and evaluation of external health signals before completing a release stage. Available with classic release only.
Jobs
Yes
Yes
To define the execution sequence of a set of steps.
Service connections
Yes
Yes
To define a connection to a remote service required to execute tasks in a job.
Service containers
Yes
No
To define a service that you can use to manage the life cycle of a containerized service.
Stages
Yes
Yes
To define flow jobs within a pipeline.
Task groups
No
Yes
To define a set of sequence tasks as a single reusable task.
Tasks
Yes
Yes
To define the building blocks that construct a pipeline.
Templates
Yes
No
To define reusable content, logic, and parameters.
Triggers
Yes
Yes
To define a specific event that causes a pipeline to run.
Variables
Yes
Yes
To define a value for data replacement and pass it to the pipeline.
Variable groups
Yes
Yes
To define the storage of values that you want to control and make available across multiple pipelines.
Table 1.1 – Pipeline features
Apart from these features, there are source version control repositories that Azure Pipelines can connect to. We’ll look at these in detail in the next section.
YAML pipelines only support some version control repositories. The following table displays which version control repositories can support which method:
Repository
YAML
Classic Interface
Azure Repos
Yes
Yes
GitHub
Yes
Yes
GitHub Enterprise Server
Yes
Yes
Bitbucket Cloud
Yes
Yes
Bitbucket Server
No
Yes
Subversion
No
Yes
Table 1.2 – Comparing repositories
In this section, we discussed all the available features of Azure Pipelines. In the next section, we will convert the key components of Azure Pipelines into a YAML structure to manage it better.
Usually, creating a file called azure-pipelines.yml will help you remember which YAML file is used for azure-pipelines in the source code repository. The basic Azure Pipelines YAML structure is as follows:
Figure 1.8 – azure-pipelines.yml file
The azure-pipelines.yml file in this example contains a typical structure:
There are two stages, stage1 and stage2, and each stage contains a job step.