Skip to content

Commit ea64263

Browse files
[windows] Change packer templates from json to hcl (#8999)
1 parent 126c302 commit ea64263

File tree

9 files changed

+919
-735
lines changed

9 files changed

+919
-735
lines changed

docs/create-image-and-azure-resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ Where:
217217
- `InstallPassword` - password for the user used to install software (Windows only);
218218
- `Location` - location where resources will be created (e.g., "East US");
219219
- `ImageName` and `ImageResourceGroupName` - name of the resource group where the managed image will be stored;
220-
- `TemplatePath` - path to the Packer template file (e.g., "images/windows/templates/windows-2022.json").
220+
- `TemplatePath` - path to the Packer template file (e.g., "images/windows/templates/windows-2022.pkr.hcl").
221221

222222
### Required variables
223223

helpers/GenerateResourcesAndImage.ps1

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Function Get-PackerTemplatePath {
1919
switch ($ImageType) {
2020
# Note: Double Join-Path is required to support PowerShell 5.1
2121
([ImageType]::Windows2019) {
22-
$relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2019.json"
22+
$relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2019.pkr.hcl"
2323
}
2424
([ImageType]::Windows2022) {
25-
$relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2022.json"
25+
$relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2022.pkr.hcl"
2626
}
2727
([ImageType]::Ubuntu2004) {
2828
$relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "ubuntu-20.04.json"
@@ -155,7 +155,7 @@ Function GenerateResourcesAndImage {
155155
if ($Force -and $ReuseResourceGroup) {
156156
throw "Force and ReuseResourceGroup cannot be used together."
157157
}
158-
158+
159159
Show-LatestCommit -ErrorAction SilentlyContinue
160160

161161
# Validate packer is installed
@@ -229,6 +229,13 @@ Function GenerateResourcesAndImage {
229229

230230
$InstallPassword = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper()
231231

232+
Write-Host "Downloading packer plugins..."
233+
& $PackerBinary init $TemplatePath
234+
235+
if ($LastExitCode -ne 0) {
236+
throw "Packer plugins download failed."
237+
}
238+
232239
Write-Host "Validating packer template..."
233240
& $PackerBinary validate `
234241
"-var=client_id=fake" `
@@ -242,7 +249,7 @@ Function GenerateResourcesAndImage {
242249
"-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" `
243250
"-var=azure_tags=$($TagsJson)" `
244251
$TemplatePath
245-
252+
246253
if ($LastExitCode -ne 0) {
247254
throw "Packer template validation failed."
248255
}
@@ -290,7 +297,7 @@ Function GenerateResourcesAndImage {
290297
# Resource group already exists, ask the user what to do
291298
$title = "Resource group '$ResourceGroupName' already exists"
292299
$message = "Do you want to delete the resource group and all resources in it?"
293-
300+
294301
$options = @(
295302
[System.Management.Automation.Host.ChoiceDescription]::new("&Yes", "Delete the resource group and all resources in it."),
296303
[System.Management.Automation.Host.ChoiceDescription]::new("&No", "Keep the resource group and continue."),
@@ -346,7 +353,7 @@ Function GenerateResourcesAndImage {
346353
if ($LastExitCode -ne 0) {
347354
throw "Failed to create service principal '$ServicePrincipalName'."
348355
}
349-
356+
350357
$ServicePrincipalAppId = $ServicePrincipal.appId
351358
$ServicePrincipalPassword = $ServicePrincipal.password
352359
$TenantId = $ServicePrincipal.tenant
@@ -383,7 +390,7 @@ Function GenerateResourcesAndImage {
383390
Write-Error $_
384391
} finally {
385392
Write-Verbose "`nCleaning up..."
386-
393+
387394
# Remove ADServicePrincipal and ADApplication
388395
if ($ADCleanupRequired) {
389396
Write-Host "Removing ADServicePrincipal..."

images.CI/linux-and-win/azure-pipelines/windows2019.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ jobs:
1818
parameters:
1919
image_type: windows2019
2020
image_readme_name: Windows2019-Readme.md
21-
image_template_name: windows-2019.json
21+
image_template_name: windows-2019.pkr.hcl

images.CI/linux-and-win/azure-pipelines/windows2022.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ jobs:
1818
parameters:
1919
image_type: windows2022
2020
image_readme_name: Windows2022-Readme.md
21-
image_template_name: windows-2022.json
21+
image_template_name: windows-2022.pkr.hcl

images.CI/linux-and-win/build-image.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ if (-not (Test-Path $TemplatePath))
2222
$ImageTemplateName = [io.path]::GetFileName($TemplatePath).Split(".")[0]
2323
$InstallPassword = [System.GUID]::NewGuid().ToString().ToUpper()
2424

25-
packer validate -syntax-only $TemplatePath
26-
2725
$SensitiveData = @(
2826
'OSType',
2927
'StorageAccountLocation',
@@ -37,6 +35,12 @@ $SensitiveData = @(
3735
Write-Host "Show Packer Version"
3836
packer --version
3937

38+
Write-Host "Download packer plugins"
39+
packer init $TemplatePath
40+
41+
Write-Host "Validate packer template"
42+
packer validate -syntax-only $TemplatePath
43+
4044
Write-Host "Build $ImageTemplateName VM"
4145
packer build -var "client_id=$ClientId" `
4246
-var "client_secret=$ClientSecret" `

0 commit comments

Comments
 (0)