Skip to content

Commit ccebe80

Browse files
committed
Initial changes to allow ComputeGallery for images
1 parent 9f0f006 commit ccebe80

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

helpers/GenerateResourcesAndImage.ps1

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ Function GenerateResourcesAndImage {
9191
The type of image to generate. Valid values are: Windows2022, Windows2025, Windows2025_vs2026, Ubuntu2204, Ubuntu2404.
9292
.PARAMETER ManagedImageName
9393
The name of the managed image to create. The default is "Runner-Image-{{ImageType}}".
94+
.PARAMETER ComputeGalleryName
95+
The name of the compute gallery to create. If specified will override the default behavior of creating a managed image.
96+
Will use the ResourceGroupName parameter as the resource group for the compute gallery.
97+
.PARAMETER ComputeGalleryImageName
98+
The name of the compute gallery image to create. This is required if ComputeGalleryName is specified.
99+
.PARAMETER ComputeGalleryImageVersion
100+
The name of the compute gallery image version to create. This is required if ComputeGalleryName is specified.
94101
.PARAMETER AzureLocation
95102
The Azure location where the Azure resources will be created. For example: "East US"
96103
.PARAMETER ImageGenerationRepositoryRoot
@@ -130,6 +137,12 @@ Function GenerateResourcesAndImage {
130137
[ImageType] $ImageType,
131138
[Parameter(Mandatory = $False)]
132139
[string] $ManagedImageName = "Runner-Image-$($ImageType)",
140+
[Parameter(Mandatory = $False)]
141+
[string] $ComputeGalleryName,
142+
[Parameter(Mandatory = $False)]
143+
[string] $ComputeGalleryImageName,
144+
[Parameter(Mandatory = $False)]
145+
[string] $ComputeGalleryImageVersion,
133146
[Parameter(Mandatory = $True)]
134147
[string] $AzureLocation,
135148
[Parameter(Mandatory = $False)]
@@ -161,6 +174,17 @@ Function GenerateResourcesAndImage {
161174
throw "'packer' binary is not found on PATH."
162175
}
163176

177+
# Validate Compute Gallery parameters
178+
if ($ComputeGalleryName -and -not $ComputeGalleryImageName) {
179+
throw "ComputeGalleryImageName parameter is required when ComputeGalleryName is specified."
180+
}
181+
if ($ComputeGalleryName -and -not $ComputeGalleryImageVersion) {
182+
throw "ComputeGalleryImageVersion parameter is required when ComputeGalleryName is specified."
183+
}
184+
if ($ComputeGalleryImageVersion -and -not ($ComputeGalleryImageVersion -match '^\d+\.\d+\.\d+$')) {
185+
throw "ComputeGalleryImageVersion parameter must be in the format Major.Minor.Patch, for example: 1.0.0."
186+
}
187+
164188
# Get template path
165189
$PackerTemplate = Get-PackerTemplate -RepositoryRoot $ImageGenerationRepositoryRoot -ImageType $ImageType
166190
Write-Debug "Template path: $($PackerTemplate.Path)."
@@ -204,6 +228,25 @@ Function GenerateResourcesAndImage {
204228
}
205229
Write-Debug "Tags JSON: $TagsJson."
206230

231+
# Prepare artifact arguments
232+
if ($ComputeGalleryName) {
233+
Write-Host "Compute gallery '$ComputeGalleryName' will be used to store the resulting image."
234+
$artifactArgs = @(
235+
"-var=gallery_name=$($ComputeGalleryName)",
236+
"-var=gallery_image_name=$($ComputeGalleryImageName)",
237+
"-var=gallery_image_version=$($ComputeGalleryImageVersion)",
238+
"-var=gallery_resource_group_name=$($ResourceGroupName)"
239+
)
240+
}
241+
else {
242+
Write-Host "A managed image with name '$ManagedImageName' will be created to store the resulting image."
243+
$artifactArgs = @(
244+
"-var=managed_image_name=$($ManagedImageName)",
245+
"-var=managed_image_resource_group_name=$($ResourceGroupName)"
246+
)
247+
}
248+
249+
207250
$InstallPassword = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper()
208251

209252
Write-Host "Downloading packer plugins..."
@@ -222,8 +265,7 @@ Function GenerateResourcesAndImage {
222265
"-var=tenant_id=fake" `
223266
"-var=location=$($AzureLocation)" `
224267
"-var=image_os=$($PackerTemplate.ImageOS)" `
225-
"-var=managed_image_name=$($ManagedImageName)" `
226-
"-var=managed_image_resource_group_name=$($ResourceGroupName)" `
268+
@artifactArgs `
227269
"-var=install_password=$($InstallPassword)" `
228270
"-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" `
229271
"-var=azure_tags=$($TagsJson)" `
@@ -257,6 +299,17 @@ Function GenerateResourcesAndImage {
257299
throw "Resource group '$ResourceGroupName' does not exist."
258300
}
259301

302+
# Check compute gallery existence
303+
if ($ComputeGalleryName) {
304+
$ComputeGalleryExists = [System.Convert]::ToBoolean((az sig show --resource-group $ResourceGroupName --gallery-name $ComputeGalleryName --name $ComputeGalleryImageName --query "id" --output tsv 2>$null));
305+
if ($ComputeGalleryExists) {
306+
Write-Verbose "Compute gallery '$ComputeGalleryName' already exists in resource group '$ResourceGroupName'."
307+
}
308+
else {
309+
throw "Compute gallery '$ComputeGalleryName' does not exist in resource group '$ResourceGroupName'."
310+
}
311+
}
312+
260313
# Create service principal
261314
if ([string]::IsNullOrEmpty($AzureClientId)) {
262315
Write-Host "Creating service principal for packer..."
@@ -292,8 +345,7 @@ Function GenerateResourcesAndImage {
292345
-var "tenant_id=$($TenantId)" `
293346
-var "location=$($AzureLocation)" `
294347
-var "image_os=$($PackerTemplate.ImageOS)" `
295-
-var "managed_image_name=$($ManagedImageName)" `
296-
-var "managed_image_resource_group_name=$($ResourceGroupName)" `
348+
@artifactArgs `
297349
-var "install_password=$($InstallPassword)" `
298350
-var "allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" `
299351
-var "azure_tags=$($TagsJson)" `

0 commit comments

Comments
 (0)