-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRunPipeline.ps1
475 lines (434 loc) · 21.1 KB
/
RunPipeline.ps1
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
Param(
[Parameter(HelpMessage = "The GitHub token running the action", Mandatory = $false)]
[string] $token,
[Parameter(HelpMessage = "ArtifactUrl to use for the build", Mandatory = $false)]
[string] $artifact = "",
[Parameter(HelpMessage = "Project folder", Mandatory = $false)]
[string] $project = "",
[Parameter(HelpMessage = "Specifies a mode to use for the build steps", Mandatory = $false)]
[string] $buildMode = 'Default',
[Parameter(HelpMessage = "A JSON-formatted list of apps to install", Mandatory = $false)]
[string] $installAppsJson = '[]',
[Parameter(HelpMessage = "A JSON-formatted list of test apps to install", Mandatory = $false)]
[string] $installTestAppsJson = '[]'
)
$containerBaseFolder = $null
$projectPath = $null
try {
. (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve)
Import-Module (Join-Path $PSScriptRoot '..\TelemetryHelper.psm1' -Resolve)
DownloadAndImportBcContainerHelper
if ($isWindows) {
# Pull docker image in the background
$genericImageName = Get-BestGenericImageName
Start-Job -ScriptBlock {
docker pull --quiet $using:genericImageName
} | Out-Null
}
$containerName = GetContainerName($project)
$ap = "$ENV:GITHUB_ACTION_PATH".Split('\')
$branch = $ap[$ap.Count-2]
$owner = $ap[$ap.Count-4]
if ($owner -ne "microsoft") {
$verstr = "dev"
}
else {
$verstr = $branch
}
$runAlPipelineParams = @{
"sourceRepositoryUrl" = "$ENV:GITHUB_SERVER_URL/$ENV:GITHUB_REPOSITORY"
"sourceCommit" = $ENV:GITHUB_SHA
"buildBy" = "AL-Go for GitHub,$verstr"
"buildUrl" = "$ENV:GITHUB_SERVER_URL/$ENV:GITHUB_REPOSITORY/actions/runs/$ENV:GITHUB_RUN_ID"
}
if ($project -eq ".") { $project = "" }
$baseFolder = $ENV:GITHUB_WORKSPACE
if ($bcContainerHelperConfig.useVolumes -and $bcContainerHelperConfig.hostHelperFolder -eq "HostHelperFolder") {
$allVolumes = "{$(((docker volume ls --format "'{{.Name}}': '{{.Mountpoint}}'") -join ",").Replace('\','\\').Replace("'",'"'))}" | ConvertFrom-Json | ConvertTo-HashTable
$containerBaseFolder = Join-Path $allVolumes.hostHelperFolder $containerName
if (Test-Path $containerBaseFolder) {
Remove-Item -Path $containerBaseFolder -Recurse -Force
}
Write-Host "Creating temp folder"
New-Item -Path $containerBaseFolder -ItemType Directory | Out-Null
Copy-Item -Path $ENV:GITHUB_WORKSPACE -Destination $containerBaseFolder -Recurse -Force
$baseFolder = Join-Path $containerBaseFolder (Get-Item -Path $ENV:GITHUB_WORKSPACE).BaseName
}
$projectPath = Join-Path $baseFolder $project
$sharedFolder = ""
if ($project) {
$sharedFolder = $baseFolder
}
$workflowName = "$env:GITHUB_WORKFLOW".Trim()
Write-Host "use settings and secrets"
$settings = $env:Settings | ConvertFrom-Json | ConvertTo-HashTable
# ENV:Secrets is not set when running Pull_Request trigger
if ($env:Secrets) {
$secrets = $env:Secrets | ConvertFrom-Json | ConvertTo-HashTable
}
else {
$secrets = @{}
}
$appBuild = $settings.appBuild
$appRevision = $settings.appRevision
'licenseFileUrl','codeSignCertificateUrl','*codeSignCertificatePassword','keyVaultCertificateUrl','*keyVaultCertificatePassword','keyVaultClientId','gitHubPackagesContext','applicationInsightsConnectionString' | ForEach-Object {
# Secrets might not be read during Pull Request runs
if ($secrets.Keys -contains $_) {
$value = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($secrets."$_"))
}
else {
$value = ""
}
# Secrets preceded by an asterisk are returned encrypted.
# Variable name should not include the asterisk
Set-Variable -Name $_.TrimStart('*') -Value $value
}
$analyzeRepoParams = @{}
if ($artifact) {
# Avoid checking the artifact setting in AnalyzeRepo if we have an artifactUrl
$settings.artifact = $artifact
$gitHubHostedRunner = $settings.gitHubRunner -like "windows-*" -or $settings.gitHubRunner -like "ubuntu-*"
if ($gitHubHostedRunner -and $settings.useCompilerFolder) {
# If we are running GitHub hosted agents and UseCompilerFolder is set (and we have an artifactUrl), we need to set the artifactCachePath
$runAlPipelineParams += @{
"artifactCachePath" = Join-Path $ENV:GITHUB_WORKSPACE ".artifactcache"
}
$analyzeRepoParams += @{
"doNotCheckArtifactSetting" = $true
}
}
}
$settings = AnalyzeRepo -settings $settings -baseFolder $baseFolder -project $project @analyzeRepoParams
$settings = CheckAppDependencyProbingPaths -settings $settings -token $token -baseFolder $baseFolder -project $project
if ((-not $settings.appFolders) -and (-not $settings.testFolders) -and (-not $settings.bcptTestFolders)) {
Write-Host "Repository is empty, exiting"
exit
}
if ($bcContainerHelperConfig.ContainsKey('TrustedNuGetFeeds')) {
Write-Host "Reading TrustedNuGetFeeds"
foreach($trustedNuGetFeed in $bcContainerHelperConfig.TrustedNuGetFeeds) {
if ($trustedNuGetFeed.PSObject.Properties.Name -eq 'Token') {
if ($trustedNuGetFeed.Token -ne '') {
OutputWarning -message "Auth token for NuGet feed is defined in settings. This is not recommended. Use a secret instead and specify the secret name in the AuthTokenSecret property"
}
}
else {
$trustedNuGetFeed | Add-Member -MemberType NoteProperty -Name 'Token' -Value ''
}
if ($trustedNuGetFeed.PSObject.Properties.Name -eq 'AuthTokenSecret' -and $trustedNuGetFeed.AuthTokenSecret) {
$authTokenSecret = $trustedNuGetFeed.AuthTokenSecret
if ($secrets.Keys -notcontains $authTokenSecret) {
OutputWarning -message "Secret $authTokenSecret needed for trusted NuGetFeeds cannot be found"
}
else {
$trustedNuGetFeed.Token = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($secrets."$authTokenSecret"))
}
}
}
}
else {
$bcContainerHelperConfig.TrustedNuGetFeeds = @()
}
if ($settings.trustMicrosoftNuGetFeeds) {
$bcContainerHelperConfig.TrustedNuGetFeeds += @([PSCustomObject]@{
"url" = "https://dynamicssmb2.pkgs.visualstudio.com/DynamicsBCPublicFeeds/_packaging/AppSourceSymbols/nuget/v3/index.json"
"token" = ''
})
}
$installApps = $settings.installApps
$installTestApps = $settings.installTestApps
$installApps += $installAppsJson | ConvertFrom-Json
$installTestApps += $installTestAppsJson | ConvertFrom-Json
# Analyze app.json version dependencies before launching pipeline
# Analyze InstallApps and InstallTestApps before launching pipeline
# Check if codeSignCertificateUrl+Password is used (and defined)
if (!$settings.doNotSignApps -and $codeSignCertificateUrl -and $codeSignCertificatePassword -and !$settings.keyVaultCodesignCertificateName) {
OutputWarning -message "Using the legacy CodeSignCertificateUrl and CodeSignCertificatePassword parameters. Consider using the new Azure Keyvault signing instead. Go to https://aka.ms/ALGoSettings#keyVaultCodesignCertificateName to find out more"
$runAlPipelineParams += @{
"CodeSignCertPfxFile" = $codeSignCertificateUrl
"CodeSignCertPfxPassword" = ConvertTo-SecureString -string $codeSignCertificatePassword
}
}
if ($applicationInsightsConnectionString) {
$runAlPipelineParams += @{
"applicationInsightsConnectionString" = $applicationInsightsConnectionString
}
}
if ($keyVaultCertificateUrl -and $keyVaultCertificatePassword -and $keyVaultClientId) {
$runAlPipelineParams += @{
"KeyVaultCertPfxFile" = $keyVaultCertificateUrl
"keyVaultCertPfxPassword" = ConvertTo-SecureString -string $keyVaultCertificatePassword
"keyVaultClientId" = $keyVaultClientId
}
}
$previousApps = @()
if (!$settings.skipUpgrade) {
Write-Host "::group::Locating previous release"
try {
$latestRelease = GetLatestRelease -token $token -api_url $ENV:GITHUB_API_URL -repository $ENV:GITHUB_REPOSITORY -ref $ENV:GITHUB_REF_NAME
if ($latestRelease) {
Write-Host "Using $($latestRelease.name) (tag $($latestRelease.tag_name)) as previous release"
$artifactsFolder = Join-Path $baseFolder "artifacts"
New-Item $artifactsFolder -ItemType Directory | Out-Null
DownloadRelease -token $token -projects $project -api_url $ENV:GITHUB_API_URL -repository $ENV:GITHUB_REPOSITORY -release $latestRelease -path $artifactsFolder -mask "Apps"
$previousApps += @(Get-ChildItem -Path $artifactsFolder | ForEach-Object { $_.FullName })
}
else {
OutputWarning -message "No previous release found"
}
}
catch {
OutputError -message "Error trying to locate previous release. Error was $($_.Exception.Message)"
exit
}
Write-Host "::endgroup::"
}
$additionalCountries = $settings.additionalCountries
$imageName = ""
if (-not $gitHubHostedRunner) {
$imageName = $settings.cacheImageName
if ($imageName) {
Write-Host "::group::Flush ContainerHelper Cache"
Flush-ContainerHelperCache -cache 'all,exitedcontainers' -keepdays $settings.cacheKeepDays
Write-Host "::endgroup::"
}
}
$authContext = $null
$environmentName = ""
$CreateRuntimePackages = $false
if ($settings.versioningStrategy -eq -1) {
$artifactVersion = [Version]$settings.artifact.Split('/')[4]
$runAlPipelineParams += @{
"appVersion" = "$($artifactVersion.Major).$($artifactVersion.Minor)"
}
$appBuild = $artifactVersion.Build
$appRevision = $artifactVersion.Revision
}
elseif (($settings.versioningStrategy -band 16) -eq 16) {
$runAlPipelineParams += @{
"appVersion" = $settings.repoVersion
}
}
$buildArtifactFolder = Join-Path $projectPath ".buildartifacts"
New-Item $buildArtifactFolder -ItemType Directory | Out-Null
$allTestResults = "testresults*.xml"
$testResultsFile = Join-Path $projectPath "TestResults.xml"
$testResultsFiles = Join-Path $projectPath $allTestResults
if (Test-Path $testResultsFiles) {
Remove-Item $testResultsFiles -Force
}
$buildOutputFile = Join-Path $projectPath "BuildOutput.txt"
$containerEventLogFile = Join-Path $projectPath "ContainerEventLog.evtx"
Add-Content -Encoding UTF8 -Path $env:GITHUB_ENV -Value "containerName=$containerName"
Set-Location $projectPath
$runAlPipelineOverrides | ForEach-Object {
$scriptName = $_
$scriptPath = Join-Path $ALGoFolderName "$ScriptName.ps1"
if (Test-Path -Path $scriptPath -Type Leaf) {
Write-Host "Add override for $scriptName"
Trace-Information -Message "Using override for $scriptName"
$runAlPipelineParams += @{
"$scriptName" = (Get-Command $scriptPath | Select-Object -ExpandProperty ScriptBlock)
}
}
}
if ($runAlPipelineParams.Keys -notcontains 'RemoveBcContainer') {
$runAlPipelineParams += @{
"RemoveBcContainer" = {
Param([Hashtable]$parameters)
Remove-BcContainerSession -containerName $parameters.ContainerName -killPsSessionProcess
Remove-BcContainer @parameters
}
}
}
if ($runAlPipelineParams.Keys -notcontains 'ImportTestDataInBcContainer') {
if (($settings.configPackages) -or ($settings.Keys | Where-Object { $_ -like 'configPackages.*' })) {
Write-Host "Adding Import Test Data override"
Write-Host "Configured config packages:"
$settings.Keys | Where-Object { $_ -like 'configPackages*' } | ForEach-Object {
Write-Host "- $($_):"
$settings."$_" | ForEach-Object {
Write-Host " - $_"
}
}
$runAlPipelineParams += @{
"ImportTestDataInBcContainer" = {
Param([Hashtable]$parameters)
$country = Get-BcContainerCountry -containerOrImageName $parameters.containerName
$prop = "configPackages.$country"
if ($settings.Keys -notcontains $prop) {
$prop = "configPackages"
}
if ($settings."$prop") {
Write-Host "Importing config packages from $prop"
$settings."$prop" | ForEach-Object {
$configPackage = $_.Split(',')[0].Replace('{COUNTRY}',$country)
$packageId = $_.Split(',')[1]
UploadImportAndApply-ConfigPackageInBcContainer `
-containerName $parameters.containerName `
-companyName $settings.companyName `
-Credential $parameters.credential `
-Tenant $parameters.tenant `
-ConfigPackage $configPackage `
-PackageId $packageId
}
}
}
}
}
}
if ((($bcContainerHelperConfig.ContainsKey('TrustedNuGetFeeds') -and ($bcContainerHelperConfig.TrustedNuGetFeeds.Count -gt 0)) -or ($gitHubPackagesContext)) -and ($runAlPipelineParams.Keys -notcontains 'InstallMissingDependencies')) {
if ($githubPackagesContext) {
$gitHubPackagesCredential = $gitHubPackagesContext | ConvertFrom-Json
}
else {
$gitHubPackagesCredential = [PSCustomObject]@{ "serverUrl" = ''; "token" = '' }
}
$runAlPipelineParams += @{
"InstallMissingDependencies" = {
Param([Hashtable]$parameters)
$parameters.missingDependencies | ForEach-Object {
$appid = $_.Split(':')[0]
$appName = $_.Split(':')[1]
$version = $appName.SubString($appName.LastIndexOf('_')+1)
$version = [System.Version]$version.SubString(0,$version.Length-4)
$publishParams = @{
"nuGetServerUrl" = $gitHubPackagesCredential.serverUrl
"nuGetToken" = $gitHubPackagesCredential.token
"packageName" = $appId
"version" = $version
}
if ($parameters.ContainsKey('CopyInstalledAppsToFolder')) {
$publishParams += @{
"CopyInstalledAppsToFolder" = $parameters.CopyInstalledAppsToFolder
}
}
if ($parameters.ContainsKey('containerName')) {
Publish-BcNuGetPackageToContainer -containerName $parameters.containerName -tenant $parameters.tenant -skipVerification -appSymbolsFolder $parameters.appSymbolsFolder @publishParams -ErrorAction SilentlyContinue
}
else {
Download-BcNuGetPackageToFolder -folder $parameters.appSymbolsFolder @publishParams | Out-Null
}
}
}
}
}
"enableTaskScheduler",
"assignPremiumPlan",
"doNotBuildTests",
"doNotRunTests",
"doNotRunBcptTests",
"doNotRunPageScriptingTests",
"doNotPublishApps",
"installTestRunner",
"installTestFramework",
"installTestLibraries",
"installPerformanceToolkit",
"enableCodeCop",
"enableAppSourceCop",
"enablePerTenantExtensionCop",
"enableUICop",
"enableCodeAnalyzersOnTestApps",
"useCompilerFolder" | ForEach-Object {
if ($settings."$_") { $runAlPipelineParams += @{ "$_" = $true } }
}
switch($buildMode){
'Clean' {
$preprocessorsymbols = $settings.cleanModePreprocessorSymbols
if (!$preprocessorsymbols) {
throw "No cleanModePreprocessorSymbols defined in settings.json for this project. Please add the preprocessor symbols to use when building in clean mode or disable CLEAN mode."
}
if ($runAlPipelineParams.Keys -notcontains 'preprocessorsymbols') {
$runAlPipelineParams["preprocessorsymbols"] = @()
}
Write-Host "Adding Preprocessor symbols: $preprocessorsymbols"
$runAlPipelineParams["preprocessorsymbols"] += $preprocessorsymbols
}
'Translated' {
if ($runAlPipelineParams.Keys -notcontains 'features') {
$runAlPipelineParams["features"] = @()
}
$runAlPipelineParams["features"] += "translationfile"
}
}
Write-Host "Invoke Run-AlPipeline with buildmode $buildMode"
Run-AlPipeline @runAlPipelineParams `
-accept_insiderEula `
-pipelinename $workflowName `
-containerName $containerName `
-imageName $imageName `
-bcAuthContext $authContext `
-environment $environmentName `
-artifact $settings.artifact.replace('{INSIDERSASTOKEN}','') `
-vsixFile $settings.vsixFile `
-companyName $settings.companyName `
-memoryLimit $settings.memoryLimit `
-baseFolder $projectPath `
-sharedFolder $sharedFolder `
-licenseFile $licenseFileUrl `
-installApps $installApps `
-installTestApps $installTestApps `
-installOnlyReferencedApps:$settings.installOnlyReferencedApps `
-generateDependencyArtifact `
-updateDependencies:$settings.updateDependencies `
-previousApps $previousApps `
-appFolders $settings.appFolders `
-testFolders $settings.testFolders `
-bcptTestFolders $settings.bcptTestFolders `
-pageScriptingTests $settings.pageScriptingTests `
-restoreDatabases $settings.restoreDatabases `
-buildOutputFile $buildOutputFile `
-containerEventLogFile $containerEventLogFile `
-testResultsFile $testResultsFile `
-testResultsFormat 'JUnit' `
-customCodeCops $settings.customCodeCops `
-gitHubActions `
-failOn $settings.failOn `
-treatTestFailuresAsWarnings:$settings.treatTestFailuresAsWarnings `
-rulesetFile $settings.rulesetFile `
-enableExternalRulesets:$settings.enableExternalRulesets `
-appSourceCopMandatoryAffixes $settings.appSourceCopMandatoryAffixes `
-additionalCountries $additionalCountries `
-obsoleteTagMinAllowedMajorMinor $settings.obsoleteTagMinAllowedMajorMinor `
-buildArtifactFolder $buildArtifactFolder `
-pageScriptingTestResultsFile (Join-Path $buildArtifactFolder 'PageScriptingTestResults.xml') `
-pageScriptingTestResultsFolder (Join-Path $buildArtifactFolder 'PageScriptingTestResultDetails') `
-CreateRuntimePackages:$CreateRuntimePackages `
-appBuild $appBuild -appRevision $appRevision `
-uninstallRemovedApps
if ($containerBaseFolder) {
Write-Host "Copy artifacts and build output back from build container"
$destFolder = Join-Path $ENV:GITHUB_WORKSPACE $project
Copy-Item -Path (Join-Path $projectPath ".buildartifacts") -Destination $destFolder -Recurse -Force
Copy-Item -Path (Join-Path $projectPath ".output") -Destination $destFolder -Recurse -Force
Copy-Item -Path (Join-Path $projectPath "testResults*.xml") -Destination $destFolder
Copy-Item -Path (Join-Path $projectPath "bcptTestResults*.json") -Destination $destFolder
Copy-Item -Path $buildOutputFile -Destination $destFolder -Force -ErrorAction SilentlyContinue
Copy-Item -Path $containerEventLogFile -Destination $destFolder -Force -ErrorAction SilentlyContinue
}
}
catch {
throw
}
finally {
try {
if (Test-BcContainer -containerName $containerName) {
Write-Host "Get Event Log from container"
$eventlogFile = Get-BcContainerEventLog -containerName $containerName -doNotOpen
Copy-Item -Path $eventLogFile -Destination $containerEventLogFile
if ($project) {
# Copy event log to project folder if multiproject
$destFolder = Join-Path $ENV:GITHUB_WORKSPACE $project
Copy-Item -Path $containerEventLogFile -Destination $destFolder
}
}
}
catch {
Write-Host "Error getting event log from container: $($_.Exception.Message)"
}
if ($containerBaseFolder -and (Test-Path $containerBaseFolder) -and $projectPath -and (Test-Path $projectPath)) {
Write-Host "Removing temp folder"
Remove-Item -Path (Join-Path $projectPath '*') -Recurse -Force
Write-Host "Done"
}
}