I have added below code in provisoner section of packer , which will install vistul studio build tools in target machine and prepares the image.
You can see it is running since 8 hours, and also observed in previous attempt it did not install any VS build tools in target machine but it worked locally
packer {
required_plugins {
azure = {
version = ">= 1.0.0"
source = "github.com/hashicorp/azure"
}
}
}
source "azure-arm" "autogenerated_1" {
azure_tags = {
Environment = "Development"
Owner = "Shyam"
}
use_azure_cli_auth = true
build_resource_group_name = "cg"
image_publisher = "MicrosoftWindowsServer"
image_offer = "WindowsServer"
image_sku = "2019-Datacenter"
shared_image_gallery_destination {
subscription = "93048f2d-2d0f-44cc-b12c-4df25a3c5ff5"
resource_group = "cg"
gallery_name = "gallery1"
image_name = "webserver"
image_version = "1.0.11"
replication_regions = ["eastus2"]
storage_account_type = "Standard_LRS"
}
os_type = "Windows"
subscription_id = "XXXXX"
tenant_id = "XXXX"
vm_size = "Standard_D16lds_v5"
communicator = "winrm"
winrm_insecure = true
winrm_timeout = "60m"
winrm_use_ssl = true
winrm_username = "packer"
}
build {
sources = ["source.azure-arm.autogenerated_1"]
provisioner "powershell" {
inline = [
"Write-Host 'Installing Visual Studio Build Tools...' -ForegroundColor Cyan",
"cd $env:USERPROFILE",
"$exePath = \"$env:TEMP\\vs_buildtools.exe\"",
"# Download the installer",
"Invoke-WebRequest -Uri 'https://aka.ms/vs/17/release/vs_BuildTools.exe' -UseBasicParsing -OutFile $exePath",
"Write-Host 'Starting installation with selected components...' -ForegroundColor Cyan",
"# Install the specified workloads",
"$arguments = @('--installPath', \"$env:USERPROFILE\\vs_BuildTools2022\", '--quiet', '--norestart', '--nocache', '--wait', '--noUpdateInstaller', '--noWeb', '--add', 'Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools', '--add', 'Microsoft.VisualStudio.Workload.WebBuildTools', '--add', 'Microsoft.VisualStudio.Workload.AzureBuildTools', '--add', 'Microsoft.VisualStudio.Workload.DataBuildTools', '--includeRecommended')",
"Start-Process -FilePath $exePath -ArgumentList $arguments -Wait",
"# Set the environment variable for the path",
"[Environment]::SetEnvironmentVariable('Path', \"$([Environment]::GetEnvironmentVariable('Path', 'Machine'));$env:USERPROFILE\\vs_BuildTools2022\", 'Machine')",
"Write-Host 'Installation complete.' -ForegroundColor Green"
]
}
provisioner "powershell" {
inline = [
"while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
"while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
"& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
"while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }"
]
}
}
Screenshot of the message;