forked from jenkins-infra/packer-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-jenkins-agent-windows.pkr.hcl
78 lines (73 loc) · 3.51 KB
/
build-jenkins-agent-windows.pkr.hcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
build {
source "amazon-ebs.base" {
name = "windows"
communicator = "winrm"
user_data_file = "./provisioning/setupWinRM.ps1"
winrm_insecure = true
winrm_timeout = "20m"
winrm_use_ssl = true
winrm_username = local.windows_winrm_user[var.image_type]
}
source "azure-arm.base" {
name = "windows"
communicator = "winrm"
# List available offers and publishers with the command `az vm image list --output table`
image_offer = "WindowsServer"
image_publisher = "MicrosoftWindowsServer"
# List available SKUs with the command `az vm image list-skus --offer WindowsServer --location eastus --publisher MicrosoftWindowsServer --output table`
image_sku = "${var.agent_os_version}-datacenter-core-smalldisk-g2"
vm_size = local.azure_vm_size
os_type = "Windows"
os_disk_size_gb = local.windows_disk_size_gb
winrm_insecure = true
winrm_timeout = "20m"
winrm_use_ssl = true
winrm_username = local.windows_winrm_user[var.image_type]
}
## Why repeating? https://github.com/rgl/packer-plugin-windows-update/issues/90#issuecomment-842569865
# Note that restarts are only done when required by windows updates
provisioner "windows-update" { pause_before = "1m" }
provisioner "windows-update" { pause_before = "1m" }
provisioner "windows-update" { pause_before = "1m" }
provisioner "file" {
pause_before = "1m"
source = "./provisioning/addSSHPubKey.ps1"
destination = "C:/"
}
provisioner "powershell" {
environment_vars = local.provisioning_env_vars
elevated_user = local.windows_winrm_user[var.image_type]
elevated_password = build.Password
script = "./provisioning/windows-provision.ps1"
}
# Recommended (and sometimes required) before running deprovisioning (sysprep or AWS scripts)
# ref. https:#www.packer.io/docs/builders/azure/arm#windows
provisioner "windows-restart" {
max_retries = 3
}
# This provisioner must be the last for Azure builds, after reboots
provisioner "powershell" {
only = ["azure-arm.windows"]
elevated_user = local.windows_winrm_user[var.image_type]
elevated_password = build.Password
inline = [
"& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit /mode:vm",
"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 } }"
]
}
provisioner "file" {
only = ["amazon-ebs.windows"]
source = "./provisioning/EC2-LaunchConfig.json"
destination = "C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Config\\LaunchConfig.json"
}
# This provisioner must be the last for AWS EBS builds, after reboots
provisioner "powershell" {
only = ["amazon-ebs.windows"]
elevated_user = local.windows_winrm_user[var.image_type]
elevated_password = build.Password
# Ref. https:#docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html#user-data-scripts-subsequent
inline = [
"if($env:AGENT_OS_VERSION = '2019') { C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Scripts\\SendWindowsIsReady.ps1 -Schedule; C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Scripts\\InitializeInstance.ps1 -Schedule; C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Scripts\\SysprepInstance.ps1 -NoShutdown;};"
]
}
}