Streamlining Image Deployment with Packer and Azure DevOps

In the rapidly evolving landscape of modern software deployment, the need for efficient, automated image deployment has become increasingly critical. Leveraging the combined power of HashiCorp Packer and Azure DevOps, organizations can streamline their image creation and deployment processes. This guide is designed to offer practical strategies for harnessing the capabilities of Packer and Azure DevOps, specifically focusing on automating Packer image builds and effectively managing these images through the Azure Image Gallery. By following this guide, developers and DevOps professionals can gain an understanding of how to optimize their image deployment workflows and enhance the efficiency of their software deployment pipelines by pairing Packer with Azure Image Gallery.

What is Packer?

Automated machine images enable the rapid and consistent deployment of complex software systems and configurations across various computing environments. Packer is a powerful, open-source tool designed to simplify the process of creating machine images for multiple platforms in an automated and consistent manner. HashiCorp Packer streamlines the image creation process, enabling users to build, provision, and configure images for various platforms simultaneously. By offering a uniform approach to image creation, Packer helps ensure consistency, reliability, and efficiency in the deployment of software across diverse computing environments, making it a useful tool for modern DevOps and infrastructure management.

Azure Image Gallery

Azure Image Gallery is a robust service offered by Microsoft Azure, designed to simplify the management and deployment of custom virtual machine images in the cloud. It serves as a centralized repository for storing, managing, and sharing customized images, enabling users to streamline the deployment process and enhance overall operational efficiency. With a range of powerful features, the Azure Image Gallery empowers businesses to effortlessly create, manage, and distribute virtual machine images, catering to the dynamic requirements of modern cloud computing environments. Key features of Azure Image Gallery include seamless image versioning, granular access control, and integrated image-sharing capabilities, all of which contribute to a more streamlined and agile cloud image management process.

How to Build and Store an Image Using Packer, Azure DevOps Pipelines, and Azure Image Gallery

This section describes the steps to create a packer image template, build the image utilizing Azure DevOps pipelines, and then upload and store the image using Azure Image Gallery.

Create an Image with Packer

Packer operates through a JSON-based image template, which serves as a blueprint for defining the image. This template includes specifications for builders and provisioners, which are crucial components of Packer’s functionality.

Builders are responsible for creating machines and generating images from a single source configuration. They facilitate the setup of specific platforms or cloud providers, allowing users to create images compatible with various target environments. Examples of Packer builders include those for popular virtualization and cloud platforms such as VMware, VirtualBox, Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP), and Docker.

Provisioners enable the installation and configuration of software within the created images. They help in automating tasks such as installing packages, configuring settings, and preparing the image for its intended use.

To create the JSON image template file, users need to define the required configuration within the template, specifying the builders and provisioners according to their project’s specific requirements. The template file includes details about the source image, builders, provisioners, and post-processing tasks, providing a comprehensive guide for Packer to execute the image creation process accurately and efficiently.

There are countless examples of packer template JSON files available online. One such resource is Microsoft’s repository containing packer images of their runners used as build agents in pipelines available here: https://github.com/actions/runner-images/tree/main/images/win

The example JSON template below utilizes Packer’s built-in azure-arm builder and several variables. It includes one sample provisioner to create an empty directory.

Set Up the Azure DevOps Deployment Pipeline

The following guide includes step-by-step instructions to set up a build and release pipeline using Azure DevOps to build the image from the template file using Packer, and deploy the image to Azure Image Gallery. The tutorial begins with setting up a variable group for use in the pipeline. This offers the advantage of the ability to utilize the same variable set in multiple pipelines, and to easily switch between environments by grouping all of the variables for each environment together. This tutorial shows creating a pipeline using the classic pipelines editor. It is also possible to create a similar pipeline utilizing YAML.

1) Set Up Variables for the Pipeline

  1. Click Pipelines > Library > + Variable Group
  1. Enter a name for the variable group. Enter each variable name and value for variables required by the packer JSON template. Click “Pipeline Permissions” and add the current project to allow the Azure DevOps pipelines to access the variables.
  1. Set up a Service Connection: Create a service connection to your Azure environment. For detailed instructions, see this article
  1. Create a new pipeline: In the Azure DevOps project, click the “New pipeline” button. Azure DevOps provides several templates for common workflows. Click “Use the classic editor to create a pipeline without YAML”.
  1. Select your code repository: Choose the source control system where your code is located. Select the repository that contains the packer json template you want to build with the pipeline.
  1. Choose a template: Microsoft provides several templates to get started. Click “or Start with an Empty Job”

2) Configure the Pipeline

  1. Define the agent pool and specification – In the example below, the agent pool is set to Azure Pipelines which are agents hosted by Microsoft and the agent specification is windows-latest
  1. Specify the source repository – Select the repository containing the packer JSON template file
  1. Add prebuild steps – Click the + icon and add an Azure CLI task, selecting your service connection name in the drop-down box. In the example pipeline below, the first task downloads packer and installs it onto the build agent before the build step.
  1. Build the packer image – Build the packer image using the packer build command and passing in any variables required by the template. Click the + sign and add the “Build machine image” task. Select “User provided” for the Packer Template and specify the location in the repository for the Packer JSON template file. Specify any required variables for your template that will be passed through the pipeline in the Template parameters field. Enter a variable name for the image URL or Name output and for the Azure Resource ID.
  1. Upload the image to the Azure Image Gallery – Upload the image to the Azure Image Gallery using Azure CLI commands. Add an Azure CLI task to the pipeline, specifying the service connection. Utilize the environment variables from the previous step to reference the built image in the az sig image-version create command. Specify any variables needed in the command.
  1. Link the variable group to the pipeline – Click Variables > Variable groups > Link variable group and add the variable group to the pipeline

3) Run the Pipeline

Click “Save and Queue” to run the pipeline!

Wrapping It All Up

The goal of this article is to show one example of utilizing Packer custom JSON templates to build and deploy a machine image utilizing Azure DevOps Pipelines. Packer supports several other builders and features for creating many different types of machine images, based on the platform and OS requirements and beyond. These images can be built and maintained not only with Azure DevOps classic deployment pipelines as shown in this example, but also utilizing a host of other tools if needed. We hope that this guide has helped demonstrate one way to utilize these tools together to simplify the process of defining, creating, and maintaining automated machine images.