-
Notifications
You must be signed in to change notification settings - Fork 17
/
build-jenkins-agent-windows.pkr.hcl
150 lines (132 loc) · 5.76 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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-g2"
image_version = try(local.images_versions["azure"]["windows"][var.agent_os_version][var.architecture], "N/A")
os_type = "Windows"
os_disk_size_gb = local.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" }
# Installing Docker requires a restart: this first call to the installation script will prepare requirements
provisioner "powershell" {
pause_before = "1m"
environment_vars = local.provisioning_env_vars
elevated_user = local.windows_winrm_user[var.image_type]
elevated_password = build.Password
inline = [
"Invoke-WebRequest -UseBasicParsing 'https://raw.githubusercontent.com/microsoft/Windows-Containers/Main/helpful_tools/Install-DockerCE/install-docker-ce.ps1' -o install-docker-ce.ps1",
"install-docker-ce.ps1 -DockerVersion $env:DOCKER_VERSION -NoRestart -Verbose",
]
}
# Required for loading Windows Container Feature
provisioner "windows-restart" {}
# Install Docker-CE with Container feature loaded
provisioner "powershell" {
pause_before = "1m"
environment_vars = local.provisioning_env_vars
elevated_user = local.windows_winrm_user[var.image_type]
elevated_password = build.Password
inline = [
"Invoke-WebRequest -UseBasicParsing 'https://raw.githubusercontent.com/microsoft/Windows-Containers/Main/helpful_tools/Install-DockerCE/install-docker-ce.ps1' -o install-docker-ce.ps1",
"install-docker-ce.ps1 -DockerVersion $env:DOCKER_VERSION -Verbose",
]
}
provisioner "file" {
# Previous provisioner might restart
pause_before = "1m"
source = "./provisioning/addSSHPubKey.ps1"
destination = "C:/"
}
provisioner "file" {
source = "./provisioning/visualstudio.vsconfig"
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)
# ref. https:#www.packer.io/docs/builders/azure/arm#windows
provisioner "windows-restart" {
max_retries = 3
# Previous provisioner might restart
pause_before = "1m"
}
provisioner "file" {
source = "./tests/goss-windows-2019.yaml"
destination = "C:/goss-windows-2019.yaml"
}
provisioner "file" {
source = "./tests/goss-windows-2022.yaml"
destination = "C:/goss-windows-2022.yaml"
}
provisioner "file" {
source = "./tests/goss-windows.yaml"
destination = "C:/goss-windows.yaml"
}
provisioner "file" {
source = "./tests/goss-common.yaml"
destination = "C:/goss-common.yaml"
}
provisioner "breakpoint" {
note = "Enable this breakpoint to pause before trying to run goss tests"
disable = true
}
provisioner "powershell" {
environment_vars = local.provisioning_env_vars
inline = [
"$ErrorActionPreference = 'Stop'",
"goss --version",
"goss --use-alpha=1 --gossfile C:/goss-windows-${var.agent_os_version}.yaml --loglevel DEBUG validate --max-concurrent=1",
"goss --use-alpha=1 --gossfile C:/goss-windows.yaml --loglevel DEBUG validate --max-concurrent=1",
"goss --use-alpha=1 --gossfile C:/goss-common.yaml --loglevel DEBUG validate --max-concurrent=1",
"Remove-Item -Force C:/goss-windows.yaml",
"Remove-Item -Force C:/goss-common.yaml",
"Remove-Item -Force C:/visualstudio.vsconfig",
]
}
# 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 } }"
]
}
# 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
inline = [
"& 'C:/Program Files/Amazon/EC2Launch/ec2launch' reset --block",
"& 'C:/Program Files/Amazon/EC2Launch/ec2launch' sysprep --block",
]
}
}