Home
%3CLINGO-SUB%20id%3D%22lingo-sub-1056276%22%20slang%3D%22en-US%22%3EEnvironment%20Tracing%20and%20Build%20Annotations%20with%20Azure%20Pipelines%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1056276%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20%3CA%20href%3D%22https%3A%2F%2Fcloud.google.com%2Fblog%2Fproducts%2Fdevops-sre%2Fthe-2019-accelerate-state-of-devops-elite-performance-productivity-and-scaling%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Enumbers%20show%3C%2FA%3E%2C%20using%20continuous%20integration%20and%20continuous%20deployment%20(CI%2FCD)%20solutions%20for%20delivering%20software%20and%20cloud%20infrastructure%20improve%20deployment%20reliability%2C%20recoverability%2C%20and%20agility.%20In%20addition%20to%20these%20great%20benefits%2C%20CI%2FCD%20produces%20many%20byproducts%20that%20are%20also%20useful.%20One%20of%20these%20is%20what%20I%20like%20to%20refer%20to%20as%20environment%20traceability.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20like%20to%20think%20of%20environment%20traceability%20as%20the%20ability%20to%20correlate%20a%20deployed%20application%20or%20cloud%20solution%20to%20the%20individual%20build%20that%20created%20or%20updated%20it.%20This%20can%20be%20achieved%20by%20annotating%20the%20deployed%20objects%20with%20information%20about%20the%20build.%20As%20an%20example%2C%20perhaps%20you%20annotate%20a%20cloud%20object%20with%20a%20tag%20that%20includes%20the%20CI%2FCD%20BuildID.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20these%20annotations%20in%20place%2C%20you%20should%20be%20able%20to%20correlate%20a%20deployment%20to%20the%20build%20that%20created%20it.%20Once%20you%20can%20do%20this%2C%20you%20can%20then%20trace%20source%20control%20back%20to%20the%20code%20or%20infrastructure%20updates%20that%20were%20made%20to%20trigger%20the%20build.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20quick%20blog%20post%2C%20I%20will%20show%20you%20how%20to%20achieve%20environment%20traceability%20when%20using%20Azure%20Pipelines.%20The%20examples%20given%20in%20this%20blog%20post%20are%20related%20to%20Docker%20container%20images%20and%20Kubernetes%2C%20however%2C%20the%20theory%20can%20be%20used%20on%20any%20number%20of%20deployment%20objects.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EBuilt-in%20Example%20(Kubernetes%20Manifest%20Task)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAn%20Azure%20Pipeline%20is%20essentially%20a%20series%20of%20automated%20tasks.%20Many%20tasks%20are%20built%20into%20Azure%20Pipelines%20or%20are%20available%20to%20install%20from%20the%20Visual%20Studio%20Marketplace.%20Some%20Azure%20Pipeline%20tasks%20include%20traceability%20annotations%20out%20of%20the%20box.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdevops%2Fpipelines%2Ftasks%2Fdeploy%2Fkubernetes-manifest%3FWT.mc_id%3DITOpsTalk-blog-nepeters%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EKubernetes%20Manifest%20task%3C%2FA%3E%20deploys%20a%20Kubernetes%20manifest%20file%20to%20a%20Kubernetes%20cluster.%20When%20doing%20so%2C%20the%20objects%20created%20by%20the%20manifest%20file%20are%20automatically%20annotated%20with%20information%20about%20the%20Azure%20Pipeline%20build.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20simple%20Azure%20Pipeline%20that%20uses%20the%20Kubernetes%20Manifest%20task%20to%20deploy%20an%20application%20to%20an%20Azure%20Kubernetes%20Service%20cluster.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Etrigger%3A%0A-%20master%0A%0Apool%3A%0A%20%20vmImage%3A%20'ubuntu-latest'%0A%0Asteps%3A%0A%0A-%20task%3A%20KubernetesManifest%400%0A%20%20inputs%3A%0A%20%20%20%20action%3A%20'deploy'%0A%20%20%20%20kubernetesServiceConnection%3A%20'blog-kubernetes'%0A%20%20%20%20namespace%3A%20'default'%0A%20%20%20%20manifests%3A%20'azure-vote.yml'%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20running%20the%20Pipeline%2C%20notice%20that%20the%20pipeline%20Build%20ID%20can%20be%20seen%20in%20the%20pipeline%20address%20bar.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F160767iE2A0F893C4F0E79E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22buildid.png%22%20title%3D%22buildid.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20a%20result%20of%20running%20this%20pipeline%2C%20two%20pods%20and%20a%20Kubernetes%20service%20have%20been%20created.%20Let's%20pull%20some%20information%20about%20one%20of%20the%20pods%20using%20the%20%3CEM%3Ekubectl%20describe%3C%2FEM%3E%20command.%20In%20the%20following%20example%2C%20I've%20removed%20most%20of%20the%20output%20for%20brevity.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%24%20kubectl%20describe%20pod%20azure-vote-back-5966fd4fd4-rxlld%0A%0AAnnotations%3A%20%20%20%20azure-pipelines%2FjobName%3A%20%22Job%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20azure-pipelines%2Forg%3A%20%3CA%20href%3D%22https%3A%2F%2Fnepeters-devops.visualstudio.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fnepeters-devops.visualstudio.com%2F%3C%2FA%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20azure-pipelines%2Fpipeline%3A%20%22neilpeterson.simple-kubernetes-sample%20(1)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20azure-pipelines%2FpipelineId%3A%20%22109%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20azure-pipelines%2Fproject%3A%20kubernetes-deployment-job%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20azure-pipelines%2Frun%3A%2020191209.5%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20azure-pipelines%2Frunuri%3A%20%3CA%20href%3D%22https%3A%2F%2Fnepeters-devops.visualstudio.com%2Fkubernetes-deployment-job%2F_build%2Fresults%3FbuildId%3D2206%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fnepeters-devops.visualstudio.com%2Fkubernetes-deployment-job%2F_build%2Fresults%3FbuildId%3D2206%3C%2FA%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%20here%20that%20the%20pod%20was%20automatically%20annotated%20to%20include%20information%20about%20the%20build%2C%20including%20the%20BuildID%2C%20which%20can%20be%20matched%20to%20the%20BuildID%20seen%20in%20the%20previous%20screenshot.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ECustom%20Example%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20think%20that%20is%20pretty%20awesome%2C%20however%20what%20if%20you%20are%20not%20using%20Kubernetes%20tasks%20or%20another%20task%20that%20has%20native%20support%20for%20tracing%20annotations.%20How%20can%20we%20replicate%20this%20behavior%20in%20a%20custom%20way%3F%20The%20short%20answer%20is%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdevops%2Fpipelines%2Fbuild%2Fvariables%3FWT.mc_id%3DITOpsTalk-blog-nepeters%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EPredefined%20Azure%20Pipeline's%20variables%3C%2FA%3E.%20Azure%20Pipelines%20include%20a%20set%20of%20internal%20variables%20that%20can%20be%20used%20for%20various%20purposes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20let's%20assume%20we%20are%20using%20a%20generic%20script%20task%20to%20build%20a%20Docker%20container%20image%20and%20that%20we%20want%20to%20give%20that%20image%20a%20version%20that%20matches%20the%20BuildID%20of%20the%20pipeline%20instance%20that%20built%20the%20image.%20For%20this%20purpose%2C%20we%20can%20use%20a%20variable%20named%20%3CEM%3E%24(Build.BuildId)%3C%2FEM%3E%20to%20return%20the%20BuildID.%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20example%20shows%20the%20build%20step%2C%20and%20then%20also%20a%20step%20that%20pushes%20the%20image%20to%20a%20container%20registry.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E-%20task%3A%20CmdLine%402%0A%20%20displayName%3A%20'docker%20build%20(tt-cart)'%0A%20%20inputs%3A%0A%20%20%20%20script%3A%20%7C%0A%20%20%20%20%20%20docker%20build%20-t%20%24(containerRegistryFqdn)%2Fcart.api%3A%24(Build.BuildId)%20.%0A%0A-%20task%3A%20CmdLine%402%0A%20%20displayName%3A%20'docker%20push%20(tt-web)'%0A%20%20inputs%3A%0A%20%20%20%20script%3A%20%7C%0A%20%20%20%20%20%20docker%20push%20%24(containerRegistryFqdn)%2Fcart.api%3A%24(Build.BuildId)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%20here%20that%20a%20container%20image%20is%20created%20with%20a%20version%20that%20is%20equal%20to%20%24(Build.BuidID).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%20some%20other%20steps%20in%20my%20sample%20pipeline%20that%20eventually%20deploy%20this%20container%20onto%20a%20Kubernetes%20cluster%2C%20however%2C%20these%20are%20not%20relevant%20to%20the%20post%20and%20have%20been%20omitted.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20the%20pipeline%20has%20run%2C%20a%20container%20image%20is%20created%20with%20the%20proper%20image%20version%2C%20pushed%20to%20an%20Azure%20Container%20Registry%20and%20then%20deployed%20onto%20a%20Kubernetes%20cluster.%20For%20this%20example%2C%20the%20BuildID%20is%20130.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F160775iF9EB56AA6EFD8479%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22buildid-2.png%22%20title%3D%22buildid-2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUpon%20inspecting%20the%20container%2C%20we%20can%20see%20that%20the%20container%20image%20version%20matches%20the%20latest%20pipeline%20BuildID.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EImage%3A%20ttacrfqkz63c47hsxc.azurecr.io%2Fcart.api%3A130%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EConclusion%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20conclusion%2C%20Predefined%20Azure%20Pipeline%20variables%20can%20be%20used%20to%20annotate%20deployment%20objects%20in%20a%20way%20that%20allows%20us%20to%20trace%20back%20from%20production%20to%20source%20code.%20This%20can%20be%20useful%20in%20understanding%20what%20has%20changed%20in%20our%20environment%2C%20during%20troubleshooting%2C%20and%20post-incident%20review.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1056276%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20%3CA%20href%3D%22https%3A%2F%2Fcloud.google.com%2Fblog%2Fproducts%2Fdevops-sre%2Fthe-2019-accelerate-state-of-devops-elite-performance-productivity-and-scaling%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Enumbers%20show%3C%2FA%3E%2C%20using%20continuous%20integration%20and%20continuous%20deployment%20(CI%2FCD)%20solutions%20for%20delivering%20software%20and%20cloud%20infrastructure%20improve%20deployment%20reliability%2C%20recoverability%2C%20and%20agility.%20In%20addition%20to%20these%20great%20benefits%2C%20CI%2FCD%20produces%20many%20byproducts%20that%20are%20also%20useful.%20One%20of%20these%20is%20what%20I%20like%20to%20refer%20to%20as%20environment%20traceability.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F160785i7E5CC31E2A7B89E4%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22buildid-2.png%22%20title%3D%22buildid-2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1056276%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ENeil%20Peterson%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

The numbers show, using continuous integration and continuous deployment (CI/CD) solutions for delivering software and cloud infrastructure improve deployment reliability, recoverability, and agility. In addition to these great benefits, CI/CD produces many byproducts that are also useful. One of these is what I like to refer to as environment traceability.

 

I like to think of environment traceability as the ability to correlate a deployed application or cloud solution to the individual build that created or updated it. This can be achieved by annotating the deployed objects with information about the build. As an example, perhaps you annotate a cloud object with a tag that includes the CI/CD BuildID.

 

With these annotations in place, you should be able to correlate a deployment to the build that created it. Once you can do this, you can then trace source control back to the code or infrastructure updates that were made to trigger the build.

 

In this quick blog post, I will show you how to achieve environment traceability when using Azure Pipelines. The examples given in this blog post are related to Docker container images and Kubernetes, however, the theory can be used on any number of deployment objects.

 

Built-in Example (Kubernetes Manifest Task)

 

An Azure Pipeline is essentially a series of automated tasks. Many tasks are built into Azure Pipelines or are available to install from the Visual Studio Marketplace. Some Azure Pipeline tasks include traceability annotations out of the box.

 

The Kubernetes Manifest task deploys a Kubernetes manifest file to a Kubernetes cluster. When doing so, the objects created by the manifest file are automatically annotated with information about the Azure Pipeline build.

 

Here is a simple Azure Pipeline that uses the Kubernetes Manifest task to deploy an application to an Azure Kubernetes Service cluster.

 

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:

- task: KubernetesManifest@0
  inputs:
    action: 'deploy'
    kubernetesServiceConnection: 'blog-kubernetes'
    namespace: 'default'
    manifests: 'azure-vote.yml'

 

When running the Pipeline, notice that the pipeline Build ID can be seen in the pipeline address bar.

 

buildid.png

 

As a result of running this pipeline, two pods and a Kubernetes service have been created. Let's pull some information about one of the pods using the kubectl describe command. In the following example, I've removed most of the output for brevity.

 

$ kubectl describe pod azure-vote-back-5966fd4fd4-rxlld

Annotations:    azure-pipelines/jobName: "Job"
                azure-pipelines/org: https://nepeters-devops.visualstudio.com/
                azure-pipelines/pipeline: "neilpeterson.simple-kubernetes-sample (1)"
                azure-pipelines/pipelineId: "109"
                azure-pipelines/project: kubernetes-deployment-job
                azure-pipelines/run: 20191209.5
                azure-pipelines/runuri: https://nepeters-devops.visualstudio.com/kubernetes-deployment-job/_build/results?buildId=2206

 

Note here that the pod was automatically annotated to include information about the build, including the BuildID, which can be matched to the BuildID seen in the previous screenshot.

 

Custom Example

 

I think that is pretty awesome, however what if you are not using Kubernetes tasks or another task that has native support for tracing annotations. How can we replicate this behavior in a custom way? The short answer is Predefined Azure Pipeline's variables. Azure Pipelines include a set of internal variables that can be used for various purposes.

 

For example, let's assume we are using a generic script task to build a Docker container image and that we want to give that image a version that matches the BuildID of the pipeline instance that built the image. For this purpose, we can use a variable named $(Build.BuildId) to return the BuildID.

This example shows the build step, and then also a step that pushes the image to a container registry.

 

- task: CmdLine@2
  displayName: 'docker build (tt-cart)'
  inputs:
    script: |
      docker build -t $(containerRegistryFqdn)/cart.api:$(Build.BuildId) .

- task: CmdLine@2
  displayName: 'docker push (tt-web)'
  inputs:
    script: |
      docker push $(containerRegistryFqdn)/cart.api:$(Build.BuildId)

 

Note here that a container image is created with a version that is equal to $(Build.BuidID).

 

There are some other steps in my sample pipeline that eventually deploy this container onto a Kubernetes cluster, however, these are not relevant to the post and have been omitted.

 

Once the pipeline has run, a container image is created with the proper image version, pushed to an Azure Container Registry and then deployed onto a Kubernetes cluster. For this example, the BuildID is 130.

 

buildid-2.png

 

Upon inspecting the container, we can see that the container image version matches the latest pipeline BuildID.

 

Image: ttacrfqkz63c47hsxc.azurecr.io/cart.api:130

 

Conclusion

 

In conclusion, Predefined Azure Pipeline variables can be used to annotate deployment objects in a way that allows us to trace back from production to source code. This can be useful in understanding what has changed in our environment, during troubleshooting, and post-incident review.