1
+ jobs :
2
+ - job : PrepareBuildList
3
+ displayName : Prepare Build List
4
+ timeoutInMinutes : 600
5
+ pool :
6
+ vmImage : windows-2022
7
+ steps :
8
+ - checkout : self
9
+ clean : true
10
+ - powershell : |
11
+ # Determine the context of the build (PR or push)
12
+ $isPR = "$(Build.Reason)" -eq "PullRequest"
13
+
14
+ # Normalize the branch names based on context and set the target branch accordingly
15
+ if ($isPR) {
16
+ $targetBranch = "$(System.PullRequest.TargetBranch)" -replace 'refs/heads/', ''
17
+ } else {
18
+ $targetBranch = "master"
19
+ }
20
+ Write-Host "Build context determined: $(if ($isPR) { 'Pull Request targeting ' + $targetBranch } else { 'Push to master' })"
21
+
22
+ # Initialize a dictionary to keep track of solutions that have changed and need building.
23
+ $dict = New-Object 'System.Collections.Generic.Dictionary[String,System.Collections.Generic.Dictionary[String,String]]'
24
+ $samples = Get-ChildItem -Path **\*.sln -Recurse | Where-Object { $_.FullName -notmatch "\\ArchivedProjects\\" }
25
+ foreach ($sample in $samples) {
26
+ $solutionPath = [System.IO.Path]::GetDirectoryName($sample.FullName)
27
+ Write-Host "Evaluating $solutionPath"
28
+
29
+ # Perform a git diff to check for changes in the solution path relative to the target branch.
30
+ git diff --quiet HEAD "origin/$targetBranch" -- "$solutionPath"
31
+ if ($env:System_PullRequest_PullRequestId -eq '' -or $LASTEXITCODE -ne 0) {
32
+ Write-Host "Changes detected, adding $solutionPath to build list"
33
+ $item = New-Object 'System.Collections.Generic.Dictionary[String,String]'
34
+ $item.Add("solutionPath", $sample.FullName)
35
+ $name = $sample.Name.Split(".")[0]
36
+ if (!$dict.ContainsKey($name)) {
37
+ $dict.Add($name, $item)
38
+ }
39
+ }
40
+ $LASTEXITCODE = 0 # Reset last exit code to ensure accurate detection for each iteration.
41
+ }
42
+
43
+ # Convert the dictionary of changed solutions to JSON and output it for subsequent jobs.
44
+ $solutionsJson = $dict | ConvertTo-Json -Compress
45
+ Write-Host "JSON of changed solutions: $solutionsJson"
46
+ Write-Host "##vso[task.setvariable variable=samplesJson;isOutput=true]$solutionsJson"
47
+ name: passJsonOutput
48
+ displayName: 'Generate Json of Samples'
49
+
50
+ - job : BuildSamples
51
+ displayName : Build
52
+ dependsOn : PrepareBuildList
53
+ # Condition to ensure this job only runs if the previous job succeeded and there are changes in the samples solutions.
54
+ condition : and(succeeded(), ne(dependencies.PrepareBuildList.outputs['passJsonOutput.samplesJson'], '{}'))
55
+ pool :
56
+ vmImage : ' windows-2022'
57
+ strategy :
58
+ # Implementing matrix strategy based on changes detected in previous PrepareBuildList job.
59
+ matrix : $[ dependencies.PrepareBuildList.outputs['passJsonOutput.samplesJson'] ]
60
+ variables :
61
+ UseDotNetNativeToolchain : false
62
+ steps :
63
+ - checkout : self
64
+ clean : true
65
+ - template : templates/dotnet-install-windows.yml
66
+
67
+ # Conditionally apply configurations for builds from the 'canaries' branch.
68
+ - ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/canaries') }} :
69
+ - template : templates/canary-updater.yml
70
+ parameters :
71
+ solution : $(solutionPath)
72
+
73
+ # Build the project and handle build validation.
74
+ - powershell : |
75
+ Set-PSDebug -Trace 1
76
+ Write-Host "Starting build for $(solutionPath)"
77
+ # NOTE: Currently, the CI just validates that the sample builds, and we don't publish actual apps.
78
+ # In future, if we publish actual apps, it could happen that AndroidAddKeepAlives=false may cause issues.
79
+ # So it will be safer to remove it. For now, we set it to false as it makes the build much faster.
80
+ dotnet build $(solutionPath) /p:AndroidAddKeepAlives=false /p:Configuration=Release /p:WasmShellMonoRuntimeExecutionMode=Interpreter /p:PublishTrimmed=false /p:WasmShellILLinkerEnabled=false /p:EnableCoreMrtTooling=false /p:RunAOTCompilation=false /p:MtouchUseLlvm=false "/bl:$(build.artifactstagingdirectory)\$(Agent.JobName).binlog"
81
+
82
+ # Locate test projects and execute tests if applicable.
83
+ $folderPath = [System.IO.Path]::GetDirectoryName("$(solutionPath)")
84
+ $testProject = Get-Item -Path $folderPath\**\*.Tests.csproj -ErrorAction SilentlyContinue
85
+ if ($testProject) {
86
+ Write-Host "Testing project: $($testProject.FullName)"
87
+ & dotnet test $testProject.FullName -c Release --collect "XPlat code coverage" --logger trx --no-build
88
+ } else {
89
+ Write-Host "No tests found for project: $($folderPath)"
90
+ }
91
+
92
+ # Ensure the build fails on error.
93
+ if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
94
+
95
+ # Configure Git to handle long paths and clean the repository to avoid disk space issues.
96
+ git config --system core.longpaths true
97
+ git clean -fdx
98
+ Write-Host "Cleanup complete, preparing for next operations."
99
+ displayName: Build Sample
100
+
101
+ - task : PublishBuildArtifacts@1
102
+ condition : always()
103
+ retryCountOnTaskFailure : 3
104
+ inputs :
105
+ PathtoPublish : $(build.artifactstagingdirectory)
106
+ ArtifactName : samples
107
+ ArtifactType : Container
0 commit comments