Windows 10 Image Series – Part 3 – Shared Image Gallery

In this series I am going to show you how build a Windows 10 Image via Azure Pipelines and DevOps without 3rd party tooling, welcome to part 3!

Part 3 is about the Shared Image Gallery. This is a service within Microsoft Azure to publish images and we are publishing our managed image in the gallery for distribution.

Prerequisites

Part 1 & 2 of this series needs to be completed. These are the links:

Windows 10 Image Series – Part 1 (Creating the Windows VM Pipeline)

Windows 10 Image Series – Part 2 (Artifacts & Application Installation)

Therefore, the prerequisites from part 1 are also required for this part. So:

Firstly, I am assuming that you have knowledge of Azure DevOps. These are the parts that already need be setup:

In addition, if you don’t have knowledge about Azure DevOps and still want to follow this series please let me know. I might write a blog about the preparing Azure DevOps.

Checkout/Skip to other parts:

0. Windows 10 Image Series – Part 0 (Preparing Azure/Azure DevOps)

1. Windows 10 Image Series – Part 1 (Creating the Windows VM Pipeline)

2. Windows 10 Image Series – Part 2 (Artifacts & Application Installation)

3.1 Windows 10 Image Series – Part 3.1 (Create test VM from Shared Image Gallery)

4. Windows 10 Image Series – Part 4 (SessionHost Deployment from Image)

5. Windows 10 Image Series – Part 5 (Convert the Image Build pipeline to YAML)

6. Windows 10 Image Series – Part 6 (Deploy Sessionhosts with Bicep and YAML)

Creating the Shared Image Gallery

There are 2 options to create the shared image gallery. Powershell and GUI based. I am showing the Powershell option.

Firstly, log on to Azure via Powershell with this command:

Connect-AzAccount

After that, make sure you select the correct subscription:

Select-AzSubscription "SubscriptionID"

Now it is time to create the Shared Image Gallery. In the example we create a new resource group for the Shared Image Gallery

$resourceGroup = New-AzResourceGroup `
   -Name 'RG_WE_SharedImageGallery' `
   -Location 'West Europe'	

As a result, this is the resource group:

Windows 10 Image Series – Part 3 - Shared Image Gallery Resource Group creation.

After that, run the following code to create the Shared Image Gallery:

$gallery = New-AzGallery `
   -GalleryName 'WVDImageGallery' `
   -ResourceGroupName $resourceGroup.ResourceGroupName `
   -Location $resourceGroup.Location `
   -Description 'Shared Image Gallery for nielskok.tech'

It takes a minut or so but this is the Shared Image Gallery:

Windows 10 Image Series – Part 3 - Shared Image Gallery creation.

Creating Shared Image Gallery Image Definition

We continue in our same Powershell session to create the Shared Image Gallery Definition. In addition, the image definition is needed for us to upload our image in the Shared Image Gallery.

Use this code to create the image definition:

$imageDefinition = New-AzGalleryImageDefinition `
   -GalleryName $gallery.Name `
   -ResourceGroupName $gallery.ResourceGroupName `
   -Location $gallery.Location `
   -Name 'Windows10WVDImage' `
   -OsState generalized `
   -OsType Windows `
   -Publisher 'NielsKok.tech' `
   -Offer 'Windows10' `
   -Sku '21h1'

As a result, this image definition is now available in the Shared Image Gallery:

Windows 10 Image Series – Part 3 - Image Definition

Add the Shared Image Gallery to the Azure DevOps Pipeline

Firstly, we add a new job to the existing pipeline:

Name the job and fill in the parameters:

Furthermore, use this script in the Inline Script section:

## Variables

$location = '$(Location)'
$imageName = '$(ImageName)'
$ImagergName = '$(ImageRGName)'
$SharedImageGalleryName = 'WVDImageGallery'
$SharedImageGalleryRG = 'RG_WE_SharedImageGallery'
$SharedImageGalleryDefinitionName = 'Windows10WVDImage'
$GalleryImageVersionName = '1.0.$(Build.BuildId)'


## Get Managed Image Info

$managedImage = Get-AzImage `
   -ImageName $imageName `
   -ResourceGroupName $ImagergName

## Get Image Definition Info

$ImageDefinition = Get-AzGalleryImageDefinition `
    -ResourceGroupName $SharedImageGalleryRG `
    -GalleryName $SharedImageGalleryName `
    -GalleryImageDefinitionName $SharedImageGalleryDefinitionName

## Create Upload Job

$region1 = @{Name='West Europe';ReplicaCount=2}
$targetRegions = @($region1)
$job = $imageVersion = New-AzGalleryImageVersion `
      -GalleryImageDefinitionName $imageDefinition.Name `
      -GalleryImageVersionName $GalleryImageVersionName `
      -GalleryName $SharedImageGalleryName `
      -ResourceGroupName $imageDefinition.ResourceGroupName `
      -Location $imageDefinition.Location `
      -TargetRegion $targetRegions  `
      -SourceImageId $managedImage.Id.ToString() `
      -PublishingProfileEndOfLifeDate '2021-12-31' `
      -asJob

## Wait for upload to complete
$Count = 1

do {
    #Starting Count
    $Count
    $Count++

    Write-Host "Shared Image Gallery Upload not yet completed, Starting Sleep for 60 Seconds"
    Start-Sleep 60

    if ($Count -ge 75) { 
        Write-Host "Shared Image Gallery Upload FAILED"
        Break
    }
} while ($job.State -eq "Running")

if ($job.State -eq "Completed") {
    Write-Host "Shared Image Gallery Upload completed"
}

After that, run the pipeline.

When the Azure CLI task for the Shared Image Gallery upload runs the task will report every minute about the upload status:

(The task runs for about 12 – 15 minutes)

Furthermore, you can see a new version in the Shared Image Gallery:

After a while (12 – 15 minutes) the job has finished:

This was Windows 10 Image Series – Part 3, check out the other parts:

0. Windows 10 Image Series – Part 0 (Preparing Azure/Azure DevOps)

1. Windows 10 Image Series – Part 1 (Creating the Windows VM Pipeline)

2. Windows 10 Image Series – Part 2 (Artifacts & Application Installation)

3.1 Windows 10 Image Series – Part 3.1 (Create test VM from Shared Image Gallery)

4. Windows 10 Image Series – Part 4 (SessionHost Deployment from Image)

5. Windows 10 Image Series – Part 5 (Convert the Image Build pipeline to YAML)

6. Windows 10 Image Series – Part 6 (Deploy Sessionhosts with Bicep and YAML)

References

Microsoft Doc about Shared Image Gallery

9 thoughts on “Windows 10 Image Series – Part 3 – Shared Image Gallery”

Leave a Comment