How to implement CI/CD with Azure DevOps? - Part 4
Implement the CI/CD workflow with Azure DevOps: pipeline building blocks, recommended tools and tasks, a CI pipeline skeleton and lessons learned.
How to implement CI/CD with Azure DevOps? - Part 4
A process on paper only helps when the tooling enforces it. If the version stamping, code coverage checks or vulnerability scans depend on someone remembering to run them, they will be skipped on the day it matters most. In the previous article, we discussed the end-to-end workflow from a check-in to production and how build failures are handled. In this last article of the series, we are going to see how to implement this workflow with Azure DevOps, the pipeline building blocks, the tools and tasks we recommend, the challenges we faced and our recommendations.
CI/CD Process series:
- What is CI/CD and why do we need it?
- Who does what in CI/CD? Roles and environments
- How does a change flow from commit to production in CI/CD?
- How to implement CI/CD with Azure DevOps? (this article)
TL;DR Summary
- Azure DevOps covers the workflow end-to-end: Boards for Plan, Repos for Code and Pipelines for Build, Test and Release.
- A pipeline is made of stages, a stage of jobs and a job of steps; a step is either a script or a task, and a job runs on an agent.
- ReSharper and Checkmarx guard code quality and security; code coverage is published and compared with the previous build on every build.
- The biggest challenges are people and process, not tools: alignment between Dev, QA and Ops, automating the wrong things first and keeping environments alike.
- Release often in micro-increments, deploy to every environment the same way and automate everything, including database changes and infrastructure.
Azure DevOps
Our tool of choice to implement the above workflow end-to-end is Azure DevOps.
| Phase | Azure DevOps service |
|---|---|
| Plan | Boards (work items) |
| Code | Repos |
| Build | Pipelines (build) |
| Test, Release & Deploy | Pipelines (release), with approvals at each environment |
Azure Pipelines is one of the services available in Azure DevOps. A pipeline defines the continuous integration and deployment process for the app, and it has the following building blocks,
- Stage - A pipeline is made up of one or more stages, where each stage is a logical boundary in the pipeline. It can be used to mark separation of concerns (e.g. Build, QA and Production).
- Job - Each stage contains one or more jobs. When your build or deployment runs, the system begins one or more jobs. A job represents an execution boundary of a set of steps.
- Step - A step is the smallest building block of a pipeline. For example, a pipeline might consist of build and test steps. A step can either be a script or a task.
- Task - A task is simply a pre-created script offered as a convenience.
- Agent - All of the steps of a job run together on the same agent. An agent is installable software that runs one job at a time.
- Trigger - A trigger is something that’s set up to tell the pipeline when to run. The pipeline can be configured to run upon a push to the repository, at scheduled times or upon the completion of another build.
Think of it like a factory: the pipeline is the production line, stages are the departments, jobs are the workstations, steps are the individual operations, and the agent is the worker at the workstation.
Tools
The following are a few of the tools and tasks we recommend to include in the Azure DevOps pipeline to enhance the quality of the build and aid Continuous Delivery.
ReSharper
ReSharper is primarily used in the “Code” phase for code quality analysis. It applies over 2200 code inspections to your code at design time, so you can instantly see whether your current file or even your whole solution contains any errors or problems. ReSharper highlights the detected errors and problems right in the Visual Studio editor. We also run the ReSharper command line tools in the build, so the same rules are enforced in CI.
Checkmarx
Checkmarx is used during the “Build” phase to identify security vulnerabilities. It is an enterprise-grade, flexible and accurate static analysis solution capable of identifying hundreds of security vulnerabilities and weaknesses in the code.
Microsoft.CodeCoverage
This is a NuGet package which is used for converting the .coverage file (the format generated by the Visual Studio Test task, see below) to XML for .NET Framework projects. The converted XML file is used in the build pipeline to publish the code coverage report.
Azure DevOps Pipeline Tasks
| Task | Phase | Purpose |
|---|---|---|
| Version .NET Core / .NET Framework | Build | Version stamp the assemblies |
| File Creator | Build | Create a new file to hold the version number |
| Visual Studio Test & DotNet Test | Build | Run unit and functional tests |
| MSBuild & DotNet | Build | Create the binaries from the source code |
| Report Generator | Build | Convert the coverage XML format to HTML |
| Publish Code Coverage | Build | Publish code coverage results from a build |
| Compare Code Coverage | Build | Compare code coverage with the previous build |
| Tag Artifacts | Release | Tag artifacts during release with the build number |
Example: CI pipeline skeleton
The following YAML shows how the CI steps from the previous article map to a pipeline for a .NET Core project. It is an illustrative skeleton, not a copy of our pipeline. Replace the input place holders with actual values and add the marketplace tasks (versioning, Checkmarx, coverage comparison) which are available in your organization.
trigger:
branches:
include:
- release/*
pool:
vmImage: 'windows-latest'
variables:
buildConfiguration: 'Release'
steps:
# 1. Set the version number (marketplace versioning task)
# 2. Compile the source code
- task: DotNetCoreCLI@2
displayName: 'Build'
inputs:
command: 'build'
projects: '<path to solution or projects>'
arguments: '--configuration $(buildConfiguration)'
# 3. Run the unit tests and collect code coverage
- task: DotNetCoreCLI@2
displayName: 'Unit tests'
inputs:
command: 'test'
projects: '<path to test projects>'
arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"'
# 4. Publish the code coverage (then compare with the previous build)
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
# 5. Vulnerability assessment (Checkmarx task)
# 6. Publish the build artefact
- task: DotNetCoreCLI@2
displayName: 'Publish'
inputs:
command: 'publish'
publishWebProjects: true
arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
The release side (Dev, QA, UAT and production) uses the same build artefact (drop) and adds pre-deployment approvals for each environment, as we saw in the workflow diagram.
Info: We created a sample project in Azure DevOps with the build pipelines (.NET Framework and .NET Core) and a common release pipeline, so teams can export and import them into their own projects. Note that the sample pipelines are based only on the release branch workflow. Suitable modifications have to be done for the user story branch and hotfix branch workflows.
Challenges
Following are the challenges we have seen while moving to CI/CD,
- Getting Development, QA and Operations to work towards the same goal. Disconnects between these groups often result in miscommunication, delays and tension.
- Automating the wrong processes first.
- Confusing Continuous Deployment with Continuous Delivery.
- Balancing the frequency of running continuous integration jobs and resource utilization.
- Keeping the environments alike in all respects.
- Reducing the noise in all the coordination that needs to happen during sign-off and approval to deploy.
- Lack of clarity in a hybrid environment on which processes/tests have been automated and which are manual.
Recommendations
- Release often in micro-increments, and always deliver functional software.
- Deploy to every environment the same way. Production will be just another environment.
- Avoid tweaking the settings of an environment by adding a manual step or two, which compounds as the number of environments increases and impairs automation.
- Automate everything. Take a good look at your end-to-end process, uncover where we are doing things manually and plan to automate it.
- Define non-functional requirements (NFRs) as early as possible and ensure they are part of the development cycle or sprint, and not an afterthought.
- To have a truly automated and repeatable application deployment, include all changes to the database as part of the releases.
- Use a version control system for every script, database change and configuration file that is part of the release package.
- Manage and provision servers or services in the cloud through code. Move towards Infrastructure as Code.
Wrap up
In this article, we have looked at how Azure DevOps covers the workflow end-to-end, the building blocks of a pipeline, the tools and tasks we recommend, a CI pipeline skeleton, and the challenges and recommendations from our experience. With this, we have completed the CI/CD process series: from what CI/CD is and why we need it, the roles and environments, the workflow from commit to production, to implementing it with Azure DevOps. I really appreciate that you took the time to read this series. I hope it helps you to define a CI/CD process which builds trust in every build your team ships.
Related Posts
How does a change flow from commit to production in CI/CD? - Part 3
How a change flows from commit to production: the release branch workflow, CI and CD steps, quality gates and how we handle build failures.
What is CI/CD and why do we need it? - Part 1
What CI, Continuous Delivery and Continuous Deployment mean, why we need CI/CD, the goals of our process and its six phases.
Who does what in CI/CD? Roles and environments - Part 2
The roles and responsibilities in the CI/CD process, which role owns which phase and the environments a build goes through, with their gates.