-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
buildagent-generation-template.yml
282 lines (255 loc) · 12.1 KB
/
buildagent-generation-template.yml
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
parameters:
- name: image_type
displayName: Build Agent Image
type: string
default: windows2019
values:
- windows2019
- windows2022
- ubuntu2004
- ubuntu2204
- name: agent_pool
displayName: Agent Pool
type: object
default:
name: 'ci-agent-pool'
- name: variable_group_name
displayName: Variable Group
type: string
default: 'Image Generation Variables'
- name: runner_images_version
displayName: Runner-Images Version
type: string
default: release
values:
- alpha
- prerelease
- release
- name: depends_on
displayName: First Stage Depends On
type: object
default: ' '
- name: repository_base_path
displayName: Scripts Path
type: string
default: .
- name: update_type
displayName: 'Update vmss (VM Scale Set) or galleryvm (Gallery VM Image) ?'
type: string
default: galleryvm
values:
- galleryvm
- vmss
stages:
- stage: buildagent_template_vm
displayName: 'Build Agent Template VM'
${{ if ne(parameters.depends_on, ' ') }}:
dependsOn: ${{ parameters.depends_on }}
jobs:
- job: generate_image
displayName: Image Generation (${{ parameters.image_type }})
timeoutInMinutes: '600'
cancelTimeoutInMinutes: '30'
variables:
- group: ${{ parameters.variable_group_name }}
pool:
name: ${{ parameters.agent_pool }}
steps:
- checkout: self
- ${{ if ne(parameters.repository_base_path, '.') }}:
- checkout: ${{ parameters.repository_base_path }}
- task: PowerShell@2
displayName: 'Checkout the GitHub repository'
inputs:
targetType: 'inline'
script: |
$GitHubOwner = 'actions'
$RepoName = 'runner-images'
if (Test-Path ".\$RepoName\") { Remove-Item -Recurse -Force ".\$RepoName\" }
git clone "https://github.com/$GitHubOwner/$RepoName.git"
$runner_images_version = '${{ parameters.runner_images_version }}'
Write-Output "runner images version = $runner_images_version"
if ($runner_images_version -ne 'alpha') {
$os_image_type = '${{ parameters.image_type }}'
Write-Output "os image type = $os_image_type"
switch ($os_image_type) {
'windows2019' { $OSTagPrefix = 'win19' }
'windows2022' { $OSTagPrefix = 'win22' }
'ubuntu2004' { $OSTagPrefix = 'ubuntu20' }
'ubuntu2204' { $OSTagPrefix = 'ubuntu22' }
}
Write-Output "OSTagPrefix = $OSTagPrefix"
$IncludePrerelease = if ($runner_images_version -eq 'prerelease') { $true } else { $false }
Write-Output "IncludePrerelease = $IncludePrerelease"
$latest_release = ${{ parameters.repository_base_path }}/scripts/get-latestgithubrelease.ps1 -OSTagPrefix $OSTagPrefix -GitHubOwner $GitHubOwner -RepoName $RepoName -IncludePrerelease $IncludePrerelease
write-output $latest_release
cd runner-images
git checkout ($latest_release.tag_name)
}
- task: PowerShell@2
name: setVars
displayName: 'Set image template variables'
inputs:
targetType: 'inline'
script: |
if ("${{ parameters.image_type }}" -eq "ubuntu2004") {
Write-Host "##vso[task.setvariable variable=image_readme_name]Ubuntu2004-Readme.md"
$image_template_name = "ubuntu-20.04.pkr.hcl"
}
elseif ("${{ parameters.image_type }}" -eq "ubuntu2204") {
Write-Host "##vso[task.setvariable variable=image_readme_name]Ubuntu2204-Readme.md"
$image_template_name = "ubuntu-22.04.pkr.hcl"
}
elseif ("${{ parameters.image_type }}" -eq "windows2019") {
Write-Host "##vso[task.setvariable variable=image_readme_name]Windows2019-Readme.md"
$image_template_name = "windows-2019.pkr.hcl"
}
elseif ("${{ parameters.image_type }}" -eq "windows2022") {
Write-Host "##vso[task.setvariable variable=image_readme_name]Windows2022-Readme.md"
$image_template_name = "windows-2022.pkr.hcl"
}
$ImageType = "${{ parameters.image_type }}"
$TemplateDirectoryName = if ($ImageType.StartsWith("ubuntu")) { "ubuntu/templates" } else { "windows/templates" }
$TemplateDirectoryPath = Join-Path (Join-Path "runner-images" "images") $TemplateDirectoryName | Resolve-Path
$TemplateFileName = $image_template_name
if ($TemplateFileName) {
$TemplatePath = Join-Path $TemplateDirectoryPath $TemplateFileName
} else {
$TemplatePath = Join-Path $TemplateDirectoryPath "$ImageType.pkr.hcl"
if ( -not (Test-Path $TemplatePath) ) {
$TemplatePath = Join-Path $TemplateDirectoryPath "$ImageType.json"
}
}
Write-Host "##vso[task.setvariable variable=TemplateDirectoryPath;]$TemplateDirectoryPath"
Write-Host "##vso[task.setvariable variable=TemplatePath;]$TemplatePath"
$ManagedImageName = "${{ parameters.image_type }}-$(Build.BuildId)"
Write-Host "##vso[task.setvariable variable=ManagedImageName;]$ManagedImageName"
$TempResourceGroupName = "packer-temp-$ManagedImageName"
Write-Host "##vso[task.setvariable variable=TempResourceGroupName;]$TempResourceGroupName"
$GalleryVmImageDefinition = "$ImageType-agentpool-full"
Write-Host "##vso[task.setvariable variable=GalleryVmImageDefinition;isOutput=true]$GalleryVmImageDefinition"
Write-Host "##vso[task.setvariable variable=ManagedImageNameOutput;isOutput=true]$ManagedImageName"
- task: PowerShell@2
displayName: 'Workaround for issue: https://github.com/actions/runner-images/discussions/6251'
inputs:
targetType: 'inline'
script: |
$ImageType = "${{ parameters.image_type }}"
$OsType = if ($ImageType.StartsWith("ubuntu")) { "Linux" } else { "Windows" }
If ($OsType -eq 'Windows') {
Write-Host 'Attempting to modify $(TemplatePath) with workaround'
((Get-Content -path "$(TemplatePath)" -Raw) -replace 'default = {}','default = {"ExcludeMdeAutoProvisioning": "True"}') | Set-Content -Path "$(TemplatePath)"
Write-Host "##[section] Template $(TemplatePath) modified"
} else {
Write-Host "##[section] Patch not applied as $(TemplatePath) is not a Windows template"
}
- task: PowerShell@2
name: buildVm
displayName: "Build VM"
inputs:
targetType: filePath
filePath: ./runner-images/images.CI/linux-and-win/build-image.ps1
arguments: -ClientId $(CLIENT_ID) `
-ClientSecret $(CLIENT_SECRET) `
-TemplatePath $(TemplatePath) `
-ImageName "$(ManagedImageName)" `
-ImageResourceGroupName $(AZURE_RESOURCE_GROUP) `
-TempResourceGroupName "$(TempResourceGroupName)" `
-SubscriptionId $(AZURE_SUBSCRIPTION) `
-TenantId $(AZURE_TENANT) `
-Location $(AZURE_LOCATION) `
-VirtualNetworkName $(BUILD_AGENT_VNET_NAME) `
-VirtualNetworkRG $(BUILD_AGENT_VNET_RESOURCE_GROUP) `
-VirtualNetworkSubnet $(BUILD_AGENT_SUBNET_NAME)
env:
PACKER_LOG: 1
PACKER_LOG_PATH: "$(Agent.TempDirectory)/packer-log.txt"
- task: PowerShell@2
displayName: 'Copy image artifacts to the separate directory'
inputs:
targetType: 'inline'
script: |
$ImageType = "${{ parameters.image_type }}"
$rootDirectoryName = if ($ImageType.StartsWith("ubuntu")) { "ubuntu" } else { "windows" }
$rootDirectoryPath = Join-Path (Join-Path "runner-images" "images") $rootDirectoryName | Resolve-Path
$readmePath = Join-Path $rootDirectoryPath "$(image_readme_name)"
$softwareReportPath = Join-Path $rootDirectoryPath "software-report.json"
Copy-Item -Path $readmePath -Destination "$(Build.ArtifactStagingDirectory)/"
if (Test-Path $softwareReportPath) {
Copy-Item -Path $softwareReportPath -Destination "$(Build.ArtifactStagingDirectory)/"
}
- task: PowerShell@2
displayName: 'Print markdown software report'
inputs:
targetType: 'inline'
script: |
Get-Content -Path "$(Build.ArtifactStagingDirectory)/$(image_readme_name)"
- task: PowerShell@2
displayName: 'Print json software report'
inputs:
targetType: 'inline'
script: |
$softwareReportPath = "$(Build.ArtifactStagingDirectory)/software-report.json"
if (Test-Path $softwareReportPath) {
Get-Content -Path $softwareReportPath
}
- task: PublishBuildArtifacts@1
inputs:
ArtifactName: 'Built_VM_Artifacts'
displayName: Publish Artifacts
- task: PowerShell@2
displayName: 'Print provisioners duration'
inputs:
targetType: 'filePath'
filePath: ./runner-images/images.CI/measure-provisioners-duration.ps1
arguments:
-PackerLogPath "$(Agent.TempDirectory)/packer-log.txt" `
-PrefixToPathTrim "$(TemplateDirectoryPath)" `
-PrintTopNLongest 25
- task: PowerShell@2
displayName: "Clean up resources"
condition: always()
inputs:
targetType: filePath
filePath: ./runner-images/images.CI/linux-and-win/cleanup.ps1
arguments: -TempResourceGroupName "$(TempResourceGroupName)" `
-SubscriptionId $(AZURE_SUBSCRIPTION) `
-ClientId $(CLIENT_ID) `
-ClientSecret $(CLIENT_SECRET) `
-TenantId $(AZURE_TENANT)
- stage: create_gallery_imagevm
displayName: 'Create Gallery Image Version VM'
condition: and(succeeded(), eq('${{ parameters.update_type }}', 'galleryvm'))
dependsOn:
- buildagent_template_vm
jobs:
- template: buildagent-generation-galleryvm.yml
parameters:
variable_group_name: ${{ parameters.variable_group_name }}
agent_pool: ${{ parameters.agent_pool }}
repository_base_path: ${{ parameters.repository_base_path }}
- stage: update_vmss_imagevm
displayName: 'Update VM Scale Set to new Image'
dependsOn:
- buildagent_template_vm
condition: and(succeeded(), eq('${{ parameters.update_type }}', 'vmss'))
variables:
- ${{ if eq(parameters.image_type, 'windows2019') }}:
- name: VmssNames
value: $[variables.VMSS_Windows2019]
- ${{ if eq(parameters.image_type, 'windows2022') }}:
- name: VmssNames
value: $[variables.VMSS_Windows2022]
- ${{ if eq(parameters.image_type, 'ubuntu2004') }}:
- name: VmssNames
value: $[variables.VMSS_Ubuntu2004]
- ${{ if eq(parameters.image_type, 'ubuntu2204') }}:
- name: VmssNames
value: $[variables.VMSS_Ubuntu2204]
jobs:
- template: buildagent-generation-update-vmss.yml
parameters:
variable_group_name: ${{ parameters.variable_group_name }}
agent_pool: ${{ parameters.agent_pool }}
repository_base_path: ${{ parameters.repository_base_path }}
vmss_names: $(VmssNames)