-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildagent-generation-template.yml
228 lines (217 loc) · 8.7 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
parameters:
- name: image_type
displayName: Build Agent Image
type: string
default: ubuntu2204
values:
- windows2019
- windows2022
- ubuntu2004
- ubuntu2204
- name: variable_group
displayName: Variable Group
type: string
default: BuildAgents
- name: agent_pool
displayName: Agent Pool
type: string
default: Azure Pipelines
- name: depends_on
displayName: First Job Depends On
type: object
default: ' '
- name: repository_base_path
displayName: Scripts Path
type: string
default: .
- name: update_type
displayName: 'Create or Update vmss (VM Scale Set) ?'
type: string
default: vmimage
values:
- vmimage
- create vmss
- update vmss
trigger: none
stages:
- stage: buildagent_template_vm
displayName: 'Build Agent Template VM'
jobs:
- job: imagegeneration
displayName: Image Generation (${{ parameters.image_type }})
${{ if ne(parameters.depends_on, ' ') }}:
dependsOn: ${{ parameters.depends_on }}
timeoutInMinutes: '600'
cancelTimeoutInMinutes: '30'
variables:
- group: ${{ parameters.variable_group }}
pool:
name: ${{ parameters.agent_pool }}
steps:
- checkout: self
- ${{ if ne(parameters.repository_base_path, '.') }}:
- checkout: ${{ parameters.repository_base_path }}
- task: PowerShell@2
displayName: Remove Old runner-images Folder
inputs:
targetType: inline
script: if (Test-Path ".\custom-image\") { Remove-Item -Recurse -Force ".\custom-image\" }
- script: |
git clone https://github.com/pratappilaka24/custom-image
displayName: 'Clone the GitHub repository'
- task: PowerShell@2
name: setVars
displayName: 'Set image template variables'
inputs:
targetType: 'inline'
script: |
$ResourcesNamePrefix = "$(Build.BuildId)"
$ImageType = "${{ parameters.image_type }}"
$TemplateDirectoryName = if ($ImageType.StartsWith("ubuntu")) { "linux" } else { "win" }
$OsType = if ($ImageType.StartsWith("ubuntu")) { "Linux" } else { "Windows" }
$TemplateDirectoryPath = Join-Path (Join-Path "custom-image" "images") $TemplateDirectoryName | Resolve-Path
$TemplatePath = Join-Path $TemplateDirectoryPath "$ImageType.pkr.hcl"
if ( -not (Test-Path $TemplatePath) ) {
$TemplatePath = Join-Path $TemplateDirectoryPath "$ImageType.json"
}
$ImageReadmeNameConcat = "${{ parameters.image_type }}-Readme.md"
$ImageReadmeName = $ImageReadmeNameConcat.Substring(0,1).ToUpper()+$ImageReadmeNameConcat.Substring(1)
Write-Host "##vso[task.setvariable variable=TemplateDirectoryPath;]$TemplateDirectoryPath"
Write-Host "##vso[task.setvariable variable=TemplatePath;]$TemplatePath"
Write-Host "##vso[task.setvariable variable=OsType;isOutput=true;]$OsType"
Write-Host "##vso[task.setvariable variable=ImageType;isOutput=true;]$ImageType"
Write-Host "##vso[task.setvariable variable=ResourcesNamePrefix;isOutput=true;]$ResourcesNamePrefix"
Write-Host "##vso[task.setvariable variable=ImageReadmeName;isOutput=true;]$ImageReadmeName"
- task: PowerShell@2
displayName: 'Workaround for issue: https://github.com/actions/runner-images/discussions/6251'
inputs:
targetType: 'inline'
script: |
If ('$(setVars.OsType)' -eq 'Windows') {
Write-Host 'Attempting to modify $(TemplatePath) with workaround'
((Get-Content -path "$(TemplatePath)" -Raw) -replace '"type": "azure-arm",','"type": "azure-arm","azure_tags": { "ExcludeMdeAutoProvisioning": "True" },') | Set-Content -Path "$(TemplatePath)"
Write-Host "$(TemplatePath) modified"
}
- task: PowerShell@2
name: buildVm
displayName: 'Build VM'
inputs:
targetType: filePath
filePath: ${{ parameters.repository_base_path }}/scripts/build-image.ps1
arguments: -ResourcesNamePrefix $(setVars.ResourcesNamePrefix) `
-ClientId $(CLIENT_ID) `
-ClientSecret $(CLIENT_SECRET) `
-TemplatePath $(TemplatePath) `
-ResourceGroup $(AZURE_RESOURCE_GROUP) `
-StorageAccount $(AZURE_STORAGE_ACCOUNT) `
-SubscriptionId $(AZURE_SUBSCRIPTION) `
-TenantId $(AZURE_TENANT) `
-VirtualNetworkName $(BUILD_AGENT_VNET_NAME) `
-VirtualNetworkRG $(BUILD_AGENT_VNET_RESOURCE_GROUP) `
-VirtualNetworkSubnet $(BUILD_AGENT_SUBNET_NAME)
-Location $(AZURE_LOCATION)
env:
PACKER_LOG: 1
PACKER_LOG_PATH: $(Build.ArtifactStagingDirectory)/packer-log.txt
- task: PowerShell@2
displayName: 'Output Readme file content'
inputs:
targetType: 'inline'
script: |
Get-Content -Path (Join-Path "$(TemplateDirectoryPath)" "$(setVars.ImageReadmeName)")
- task: PowerShell@2
displayName: 'Print provisioners duration'
inputs:
targetType: 'filePath'
filePath: ${{ parameters.repository_base_path }}/scripts/measure-provisioners-duration.ps1
arguments: -PackerLogPath "$(Build.ArtifactStagingDirectory)/packer-log.txt" `
-PrefixToPathTrim "$(TemplateDirectoryPath)" `
-PrintTopNLongest 25
- task: PowerShell@2
displayName: 'Clean up resources'
condition: always()
inputs:
targetType: filePath
filePath: ${{ parameters.repository_base_path }}/scripts/cleanup.ps1
arguments: -ResourcesNamePrefix $(setVars.ResourcesNamePrefix) `
-ClientId $(CLIENT_ID) `
-ClientSecret $(CLIENT_SECRET) `
-Image $(setVars.ImageType) `
-SubscriptionId $(AZURE_SUBSCRIPTION) `
-TenantId $(AZURE_TENANT) `
-StorageAccount $(AZURE_STORAGE_ACCOUNT)
- stage: convert_template_to_managedvm
displayName: 'Convert Template to Managed VM'
dependsOn: buildagent_template_vm
jobs:
- template: buildagent-generation-managedvm.yml
parameters:
variable_group: ${{ parameters.variable_group }}
agent_pool: ${{ parameters.agent_pool }}
repository_base_path: ${{ parameters.repository_base_path }}
- stage: create_gallery_imagevm
displayName: 'Create Gallery Image Version VM'
condition: succeeded()
dependsOn:
- buildagent_template_vm
- convert_template_to_managedvm
jobs:
- template: buildagent-generation-galleryvm.yml
parameters:
variable_group: ${{ parameters.variable_group }}
agent_pool: ${{ parameters.agent_pool }}
repository_base_path: ${{ parameters.repository_base_path }}
- stage: create_vmss_imagevm
displayName: 'Create VM Scale Set for new Image'
dependsOn:
- buildagent_template_vm
- convert_template_to_managedvm
- create_gallery_imagevm
condition: and(succeeded(), eq('${{ parameters.update_type }}', 'create 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-create-vmss.yml
parameters:
variable_group: ${{ parameters.variable_group }}
agent_pool: ${{ parameters.agent_pool }}
repository_base_path: ${{ parameters.repository_base_path }}
vmss_names: $(VmssNames)
- stage: update_vmss_imagevm
displayName: 'Update VM Scale Set to new Image'
dependsOn:
- buildagent_template_vm
- convert_template_to_managedvm
- create_gallery_imagevm
condition: and(succeeded(), eq('${{ parameters.update_type }}', 'update 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: ${{ parameters.variable_group }}
agent_pool: ${{ parameters.agent_pool }}
repository_base_path: ${{ parameters.repository_base_path }}
vmss_names: $(VmssNames)