-
Notifications
You must be signed in to change notification settings - Fork 1.3k
134 lines (105 loc) · 4.98 KB
/
build-pr.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
name: Build Changed C# Projects for PR
on:
pull_request:
paths-ignore:
- '**.md'
jobs:
build:
strategy:
fail-fast: false
matrix:
os: [windows-latest, macos-14]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
# Remove when .NET 8 is default on hosted runners
- name: Install .NET 8
uses: actions/[email protected]
with:
dotnet-version: 8.0
- name: Install .NET MAUI Workload
run: dotnet workload install maui
# if: runner.os == 'macOS' # reenable when .NET 8 is default on hosted runners
- name: Select Xcode Version
run: sudo xcode-select -s /Applications/Xcode_16.app
if: runner.os == 'macOS'
- name: Find and build changed projects
run: |
$failedProjectCount=0
# Get the list of changed files
$changedFiles = git diff --name-only -r --diff-filter=d HEAD^1 HEAD
$excluded_projects_file="./eng/excluded_projects_" + "${{ runner.os }}".ToLower() + ".txt"
$excluded_projects=@()
$processedProjects=@()
$jobSummaryFile=$env:GITHUB_STEP_SUMMARY
if (Test-Path $excluded_projects_file) {
$excluded_projects = Get-Content -Path $excluded_projects_file | Where-Object { $_ -notmatch "^\s*#" -and $_ -match "\S" }
}
Write-Output "# .NET MAUI Sample Apps Build Status (${{ runner.os }})" | Out-File -FilePath $jobSummaryFile -Append
Write-Output "Only projects that have changes are built." | Out-File -FilePath $jobSummaryFile -Append
Write-Output "| Project | Build Status |" | Out-File -FilePath $jobSummaryFile -Append
Write-Output "|---|---|" | Out-File -FilePath $jobSummaryFile -Append
# Determine the corresponding project for each changed file
foreach ($file in $changedFiles) {
$projectToBuild=""
# Check if the file is a .csproj file
if ($file -like '*.csproj') {
$projectToBuild = $file
} else {
$currentFolder = (Get-Item -LiteralPath (Resolve-Path -Path "$file")).Directory
while ($currentFolder -ne $null -and '' -ne $currentFolder) {
$csprojFiles = Get-ChildItem -Path $currentFolder -Filter '*.csproj' -File
if ($csprojFiles.Count -gt 0) {
break
}
$currentFolder = Split-Path -Parent $currentFolder
}
if ($csprojFiles.Count -gt 0) {
$projectToBuild = $csprojFiles[0].FullName
}
}
if (-not [string]::IsNullOrEmpty($projectToBuild)) {
$projectToBuild = (Resolve-Path -Path $projectToBuild -Relative).Replace("\", "/")
# Only proceed when this project has not been built yet
if (-not ($processedProjects -contains $projectToBuild)) {
Write-Output "::notice:: $projectToBuild is not in processed builds yet"
$processedProjects += $projectToBuild
Write-Output "::notice:: Added $projectToBuild to processed builds"
if ($excluded_projects -contains $projectToBuild) {
Write-Output "::notice:: Skipping build for excluded project: $projectToBuild"
Write-Output "| $projectToBuild | Skipped |" | Out-File -FilePath $jobSummaryFile -Append
$skippedProjectCount++
}
else {
Write-Output "::group:: Building $projectToBuild"
dotnet build $projectToBuild
if ($LASTEXITCODE -gt 0) {
Write-Output "::error:: Build failed for $projectToBuild"
Write-Output "| $projectToBuild | :x: |" | Out-File -FilePath $jobSummaryFile -Append
$failedProjectCount++
}
else {
Write-Output "Build succeeded for $projectToBuild"
Write-Output "| $projectToBuild | :white_check_mark: |" | Out-File -FilePath $jobSummaryFile -Append
}
$proj_dir = [System.IO.Path]::GetDirectoryName($projectToBuild)
Write-Output "Cleaning up bin & obj in $proj_dir"
Get-ChildItem -Path $proj_dir -Directory -Recurse -Include bin,obj | Remove-Item -Recurse -Force
Write-Output "::endgroup::"
}
}
}
else {
Write-Output "::warning:: Found no csproj for file $file"
}
}
if ($failedProjectCount -gt 0) {
Write-Output "" | Out-File -FilePath $jobSummaryFile -Append
Write-Output "# Failed builds: $failedProjectCount" | Out-File -FilePath $jobSummaryFile -Append
Write-Output "# Skipped builds: $skippedProjectCount" | Out-File -FilePath $jobSummaryFile -Append
exit $failedProjectCount
}
shell: powershell