-
Notifications
You must be signed in to change notification settings - Fork 106
294 lines (248 loc) · 12.5 KB
/
aks_build_container_images.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
283
284
285
286
287
288
289
290
291
292
# This workflow creates the ACR images for the different supported SQL Server versions.
name: Build MSSQL Images
on:
# schedule:
# # * is a special character in YAML so you have to quote this string
# # Run every sunday at 6:20 AM
# - cron: '20 6 * * 0'
workflow_dispatch:
inputs:
projectName:
description: 'Project Name'
required: true
default: 'test_20210224'
machineName:
description: 'Machine Name, cannot be more than 15 characters long, be entirely numeric, or contain the following characters: ` ~ ! @ # $ % ^ & * ( ) = + _ [ ] { } \ | ; : . singlequote " , < > / ?.'
required: true
default: 'w2019c1'
msSqlVersions:
description: 'names of the k8s deployment files in JSON array format (e.g. ["windows-2008r2","windows-2012","windows-2014","windows-2016","windows-2017","windows-2019"])'
required: true
default: '["windows-2014","windows-2016","windows-2017","windows-2019"]'
debugVM:
description: 'set to "true" to retain the VM for debugging purposes'
required: false
default: 'false'
jobs:
# 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖
create-vm:
name: Create The VM
runs-on: windows-latest
# if: ${{ false }}
env:
PROJECT_NAME: ${{ github.event.inputs.projectName }}
MACHINE_NAME: ${{ github.event.inputs.machineName }}
MS_SQL_VERSIONS: ${{ github.event.inputs.msSqlVersions }}
AZ_SERVICE_PRINCIPAL_CREDENTIALS: ${{ secrets[format('AZ_SP_CRED_{0}', github.event.inputs.projectName)] }}
steps:
- name: Checkout self
uses: actions/checkout@v2
with:
path: cicd
- name: create variables
id: create-vars
shell: pwsh
run: |
function Get-MD5HashOfString($string) {
$stringAsStream = [System.IO.MemoryStream]::new();
$writer = [System.IO.StreamWriter]::new($stringAsStream);
$writer.write($string);
$writer.Flush();
$stringAsStream.Position = 0;
$hashedString = (Get-FileHash -InputStream $stringAsStream).Hash;
return [String]$hashedString;
}
$projectNameHash = (Get-MD5HashOfString($env:PROJECT_NAME)).Substring(0,10);
$machineName = "$env:MACHINE_NAME";
$machineRgName = "rg_$machineName";
$azSecretsManagerName = "sm-" + $projectNameHash;
$containerRegistryURL = "crn" + $projectNameHash + ".azurecr.io";
$repoURL = "${{ github.SERVER_URL }}/${{ github.REPOSITORY }}.git";
$commitId = "${{ github.SHA }}" ;
$msSqlVersionForMatrix = '{"windows-version":'+$env:MS_SQL_VERSIONS+'}';
Write-Host "✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ";
Write-Host "";
Write-Host ("projectName value: {0}" -f $env:PROJECT_NAME);
Write-Host ("machineName value: {0}" -f $machineName);
Write-Host ("machineRgName value: {0}" -f $machineRgName);
Write-Host ("msSqlVersionForMatrix: {0}" -f "$msSqlVersionForMatrix");
Write-Host ("azSecretsManagerName: {0}" -f "$azSecretsManagerName");
Write-Host ("containerRegistryURL: {0}" -f "$containerRegistryURL");
Write-Host ("repoURL: {0}" -f "$repoURL");
Write-Host ("commitId: {0}" -f "$commitId");
Write-Host "";
Write-Host "✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ";
Write-Host ('::set-output name=azsecretsmanagername::'+$azSecretsManagerName);
Write-Host ('::set-output name=containerregistryurl::'+$containerRegistryURL);
Write-Host ('::set-output name=repoURL::'+$repoURL);
Write-Host ('::set-output name=machineName::'+$machineName);
Write-Host ('::set-output name=machineRgName::'+$machineRgName);
Write-Host ('::set-output name=commitId::'+$commitId);
Write-Host ("::set-output name=msSqlVersionForMatrix::{0}" -f "$msSqlVersionForMatrix");
- name: decode az sp cred
id: cred-decode
shell: pwsh
run: |
$decodedCreds = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("${{ env.AZ_SERVICE_PRINCIPAL_CREDENTIALS}}"))
Write-Host ('::set-output name=az_sp_creds::'+$decodedCreds);
# documentation: https://github.com/azure/login#configure-azure-credentials
- name: login via az module
uses: azure/login@v1
with:
creds: ${{ steps.cred-decode.outputs.az_sp_creds }}
enable-AzPSSession: true
# documentation: https://github.com/Azure/get-keyvault-secrets
- name: get azure secrets
id: azure-secrets
uses: Azure/[email protected]
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true' # Note that this task can be replaced with a similar pattern as setting the namespace to the env variables (above), but is also not secure.
with:
keyvault: ${{ steps.create-vars.outputs.azsecretsmanagername }}
secrets: 'azResourceGroupName' # comma separated list of secret keys that need to be fetched from the Key Vault
- name: run azure powershell script
id: buildMachine-ps
uses: azure/powershell@v1
with:
azpsversion: 'latest'
errorActionPreference: 'continue'
inlineScript: |
$Parameters = @{
projectName = "$env:PROJECT_NAME";
azSecretsManagerName = "${{ steps.create-vars.outputs.azsecretsmanagername }}";
azResourceGroupName = "${{ steps.azure-secrets.outputs.azResourceGroupName }}";
machineRgName = "${{ steps.create-vars.outputs.machineRgName }}";
repoURL = "${{ steps.create-vars.outputs.repoURL }}";
commitId = "${{ steps.create-vars.outputs.commitId }}";
machineName = "${{ steps.create-vars.outputs.machineName }}";
debugOn = $true;
};
./cicd/envSetup/createContainerBuildMachine.ps1 @Parameters;
outputs:
mssqlversion: ${{ steps.create-vars.outputs.msSqlVersionForMatrix }}
containerregistryurl: ${{ steps.create-vars.outputs.containerregistryurl }}
azsecretsmanagername: ${{ steps.create-vars.outputs.azSecretsManagerName }}
machinergname: ${{ steps.create-vars.outputs.machineRgName }}
machinename: ${{ steps.create-vars.outputs.machineName }}
# 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖
create-images:
name: create and publish the ACR images
needs: create-vm
runs-on: windows-latest
env:
AZ_CONTAINER_REGISTRY_URL: ${{ needs.create-vm.outputs.containerregistryurl }}
AZ_SECRETS_MANAGER_NAME: ${{ needs.create-vm.outputs.azsecretsmanagername }}
MACHINE_NAME: ${{ needs.create-vm.outputs.machinename }}
MACHINE_RG_NAME: ${{ needs.create-vm.outputs.machinergname }}
AZ_SERVICE_PRINCIPAL_CREDENTIALS: ${{ secrets[format('AZ_SP_CRED_{0}', github.event.inputs.projectName)] }}
strategy:
matrix: ${{fromJson(needs.create-vm.outputs.mssqlversion)}}
max-parallel: 1
steps:
- name: Checkout self
uses: actions/checkout@v2
with:
path: cicd
- name: build and push docker image
shell: pwsh
run: |
$mssqlversion = '${{ matrix.mssqlversion }}';
Write-Host "✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ";
Write-Host "";
Write-Host ("mssqlversion: {0}" -f "$mssqlversion");
Write-Host ("azContainerRegistryURL: {0}" -f "$env:AZ_CONTAINER_REGISTRY_URL");
Write-Host ("azSecretsManagerName: {0}" -f "$env:AZ_SECRETS_MANAGER_NAME");
Write-Host ("machineName: {0}" -f "$env:MACHINE_NAME");
Write-Host ("machineRGName: {0}" -f "$env:MACHINE_RG_NAME");
Write-Host "";
Write-Host "✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ";
- name: decode az sp cred
id: cred-decode
shell: pwsh
run: |
$decodedCreds = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("${{ env.AZ_SERVICE_PRINCIPAL_CREDENTIALS}}"));
Write-Host ('::set-output name=az_sp_creds::'+$decodedCreds);
# documentation: https://github.com/azure/login#configure-azure-credentials
- name: login via az module
uses: azure/login@v1
with:
creds: ${{ steps.cred-decode.outputs.az_sp_creds }}
enable-AzPSSession: true
- name: build and push image script
id: build-and-push-image
uses: azure/powershell@v1
with:
azpsversion: 'latest'
errorActionPreference: 'continue'
inlineScript: |
$builImageScriptPath = 'cicd\envSetup\buildAndPushImage.ps1';
Invoke-AzVMRunCommand -ResourceGroupName $env:MACHINE_RG_NAME -VMName $env:MACHINE_NAME -CommandId 'RunPowerShellScript' -ScriptPath $builImageScriptPath -Parameter @{mssqlVersion = "${{ matrix.mssqlversion }}"; acrURL = "$env:AZ_CONTAINER_REGISTRY_URL"; azSpCrBase64 = "${{ env.AZ_SERVICE_PRINCIPAL_CREDENTIALS}}"; debugOnString = "$debugOn"}
# 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖
prune-untagged-images:
name: prune images
needs: [create-vm, create-images]
if: ${{ always() }}
runs-on: windows-latest
env:
AZ_CONTAINER_REGISTRY_URL: ${{ needs.create-vm.outputs.containerregistryurl }}
AZ_SERVICE_PRINCIPAL_CREDENTIALS: ${{ secrets[format('AZ_SP_CRED_{0}', github.event.inputs.projectName)] }}
steps:
- name: decode az sp cred
id: cred-decode
shell: pwsh
run: |
$decodedCreds = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("${{ env.AZ_SERVICE_PRINCIPAL_CREDENTIALS}}"))
Write-Host ('::set-output name=az_sp_creds::'+$decodedCreds);
# documentation: https://github.com/azure/login#configure-azure-credentials
- name: login via az module
uses: azure/login@v1
with:
creds: ${{ steps.cred-decode.outputs.az_sp_creds }}
enable-AzPSSession: true
- name: drop image script
id: dropBuildMachine-ps
uses: azure/powershell@v1
with:
azpsversion: 'latest'
errorActionPreference: 'continue'
inlineScript: |
# Documented here: https://docs.microsoft.com/en-us/azure/container-registry/container-registry-delete#delete-all-untagged-images
$registry = "$env:AZ_CONTAINER_REGISTRY_URL";
$repository = "windows-mssql"; # https://github.com/distribution/distribution/blob/main/docs/spec/api.md#overview, must adhere to: [a-z0-9]+(?:[._-][a-z0-9]+)*
az acr repository show-manifests --name $registry --repository $repository --query "[?tags[0]==null].digest" -o tsv | %{ az acr repository delete --name $registry --image $repository@$_ --yes }
# 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖 💖
drop-vm:
name: drop the VM
needs: [create-vm, create-images]
if: ${{ always() }}
runs-on: windows-latest
env:
AZ_SERVICE_PRINCIPAL_CREDENTIALS: ${{ secrets[format('AZ_SP_CRED_{0}', github.event.inputs.projectName)] }}
MACHINE_RG_NAME: ${{ needs.create-vm.outputs.machinergname }}
steps:
- name: check debug VM
shell: pwsh
if: ${{ github.event.inputs.debugVM == 'true' }}
run: |
Write-Host ("::error::✨ ✨ ✨TURN OFF THE VM WHEN YOU ARE DONE!✨ ✨ ✨");
exit 1;
- name: decode az sp cred
id: cred-decode
shell: pwsh
run: |
$decodedCreds = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("${{ env.AZ_SERVICE_PRINCIPAL_CREDENTIALS}}"))
Write-Host ('::set-output name=az_sp_creds::'+$decodedCreds);
# documentation: https://github.com/azure/login#configure-azure-credentials
- name: login via az module
uses: azure/login@v1
with:
creds: ${{ steps.cred-decode.outputs.az_sp_creds }}
enable-AzPSSession: true
- name: drop image build machine resource group script
id: dropBuildMachine-ps
uses: azure/powershell@v1
with:
azpsversion: 'latest'
errorActionPreference: 'continue'
inlineScript: |
Remove-AzResourceGroup -Name "$env:MACHINE_RG_NAME" -Force ;