-
Notifications
You must be signed in to change notification settings - Fork 113
/
stage-build-samples.yml
107 lines (96 loc) · 5.06 KB
/
stage-build-samples.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
jobs:
- job: PrepareBuildList
displayName: Prepare Build List
timeoutInMinutes: 600
pool:
vmImage: windows-2022
steps:
- checkout: self
clean: true
- powershell: |
# Determine the context of the build (PR or push)
$isPR = "$(Build.Reason)" -eq "PullRequest"
# Normalize the branch names based on context and set the target branch accordingly
if ($isPR) {
$targetBranch = "$(System.PullRequest.TargetBranch)" -replace 'refs/heads/', ''
} else {
$targetBranch = "master"
}
Write-Host "Build context determined: $(if ($isPR) { 'Pull Request targeting ' + $targetBranch } else { 'Push to master' })"
# Initialize a dictionary to keep track of solutions that have changed and need building.
$dict = New-Object 'System.Collections.Generic.Dictionary[String,System.Collections.Generic.Dictionary[String,String]]'
$samples = Get-ChildItem -Path **\*.sln -Recurse | Where-Object { $_.FullName -notmatch "\\ArchivedProjects\\" }
foreach ($sample in $samples) {
$solutionPath = [System.IO.Path]::GetDirectoryName($sample.FullName)
Write-Host "Evaluating $solutionPath"
# Perform a git diff to check for changes in the solution path relative to the target branch.
git diff --quiet HEAD "origin/$targetBranch" -- "$solutionPath"
if ($env:System_PullRequest_PullRequestId -eq '' -or $LASTEXITCODE -ne 0) {
Write-Host "Changes detected, adding $solutionPath to build list"
$item = New-Object 'System.Collections.Generic.Dictionary[String,String]'
$item.Add("solutionPath", $sample.FullName)
$name = $sample.Name.Split(".")[0]
if (!$dict.ContainsKey($name)) {
$dict.Add($name, $item)
}
}
$LASTEXITCODE = 0 # Reset last exit code to ensure accurate detection for each iteration.
}
# Convert the dictionary of changed solutions to JSON and output it for subsequent jobs.
$solutionsJson = $dict | ConvertTo-Json -Compress
Write-Host "JSON of changed solutions: $solutionsJson"
Write-Host "##vso[task.setvariable variable=samplesJson;isOutput=true]$solutionsJson"
name: passJsonOutput
displayName: 'Generate Json of Samples'
- job: BuildSamples
displayName: Build
dependsOn: PrepareBuildList
# Condition to ensure this job only runs if the previous job succeeded and there are changes in the samples solutions.
condition: and(succeeded(), ne(dependencies.PrepareBuildList.outputs['passJsonOutput.samplesJson'], '{}'))
pool:
vmImage: 'windows-2022'
strategy:
# Implementing matrix strategy based on changes detected in previous PrepareBuildList job.
matrix: $[ dependencies.PrepareBuildList.outputs['passJsonOutput.samplesJson'] ]
variables:
UseDotNetNativeToolchain: false
steps:
- checkout: self
clean: true
- template: templates/dotnet-install-windows.yml
# Conditionally apply configurations for builds from the 'canaries' branch.
- ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/canaries') }}:
- template: templates/canary-updater.yml
parameters:
solution: $(solutionPath)
# Build the project and handle build validation.
- powershell: |
Set-PSDebug -Trace 1
Write-Host "Starting build for $(solutionPath)"
# NOTE: Currently, the CI just validates that the sample builds, and we don't publish actual apps.
# In future, if we publish actual apps, it could happen that AndroidAddKeepAlives=false may cause issues.
# So it will be safer to remove it. For now, we set it to false as it makes the build much faster.
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 /p:WindowsAppSDKSelfContained=false /p:WindowsPackageType=None "/bl:$(build.artifactstagingdirectory)\$(Agent.JobName).binlog"
# Locate test projects and execute tests if applicable.
$folderPath = [System.IO.Path]::GetDirectoryName("$(solutionPath)")
$testProject = Get-Item -Path $folderPath\**\*.Tests.csproj -ErrorAction SilentlyContinue
if ($testProject) {
Write-Host "Testing project: $($testProject.FullName)"
& dotnet test $testProject.FullName -c Release --collect "XPlat code coverage" --logger trx --no-build
} else {
Write-Host "No tests found for project: $($folderPath)"
}
# Ensure the build fails on error.
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
# Configure Git to handle long paths and clean the repository to avoid disk space issues.
git config --system core.longpaths true
git clean -fdx
Write-Host "Cleanup complete, preparing for next operations."
displayName: Build Sample
- task: PublishBuildArtifacts@1
condition: always()
retryCountOnTaskFailure: 3
inputs:
PathtoPublish: $(build.artifactstagingdirectory)
ArtifactName: samples
ArtifactType: Container