Skip to content

Commit 8bdc94c

Browse files
committed
Work CI-CD
- Move build script to file. - Update build script to use AZDO formatted output. - Remove wrong checkout of templates repo. - Remove step installing .NET SDK. - Add cache of nuget packages. - Fix error report step. - Remove private feed from Nuget config. ***NO_CI***
1 parent 8642de3 commit 8bdc94c

File tree

3 files changed

+207
-131
lines changed

3 files changed

+207
-131
lines changed

.pipeline-assets/build-solutions.ps1

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
# Copyright (c) .NET Foundation and Contributors
2+
# See LICENSE file in the project root for full license information.
3+
4+
# This PS checks what binding need to be build in a PR or regular commit and takes care of performing the various checks and build the solution
5+
6+
# setup msbuild
7+
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
8+
$msbuild = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
9+
10+
if ($msbuild) {
11+
$msbuild = join-path $msbuild 'MSBuild\Current\Bin\amd64\MSBuild.exe'
12+
}
13+
14+
# compute authorization header in format "AUTHORIZATION: basic 'encoded token'"
15+
# 'encoded token' is the Base64 of the string "nfbot:personal-token"
16+
$auth = "basic $([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("nfbot:${env:MY_GITHUBTOKEN}"))))"
17+
18+
if($env:System_PullRequest_PullRequestId -ne $null)
19+
{
20+
"" | Write-Host -ForegroundColor Yellow
21+
"**********************" | Write-Host -ForegroundColor Yellow
22+
"* Building from a PR *" | Write-host -ForegroundColor Yellow
23+
"**********************" | Write-Host -ForegroundColor Yellow
24+
"" | Write-Host -ForegroundColor Yellow
25+
26+
# get files changed in PR
27+
$pageCounter = 1
28+
29+
do
30+
{
31+
"##[debug] INFO: iteration $pageCounter" | Write-Host
32+
33+
34+
$batch = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/pulls/$env:System_PullRequest_PullRequestNumber/files?per_page=100&page=$pageCounter" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET
35+
36+
if($null -eq $commit)
37+
{
38+
$commit = $batch
39+
}
40+
else
41+
{
42+
$commit += $batch
43+
}
44+
45+
$pageCounter++
46+
47+
} until ($batch.Count -eq 0)
48+
49+
# filter removed files
50+
$files = $commit.where{$_.status -ne 'removed'}
51+
52+
}
53+
else
54+
{
55+
"" | Write-Host -ForegroundColor Yellow
56+
"**************************" | Write-Host -ForegroundColor Yellow
57+
"* Building from a commit *" | Write-host -ForegroundColor Yellow
58+
"**************************" | Write-Host -ForegroundColor Yellow
59+
"" | Write-Host -ForegroundColor Yellow
60+
61+
# get files changed in the commit, if this is NOT a PR
62+
$commit = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/commits/$(Build.SourceVersion)" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET
63+
64+
# get files changed in the commit
65+
$pageCounter = 1
66+
67+
do
68+
{
69+
"##[command] Get API file change page: $pageCounter" | Write-Host
70+
71+
$batch = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/commits/$env:Build_SourceVersion`?per_page=100&page=$pageCounter" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET
72+
73+
if($null -eq $commit)
74+
{
75+
$commit = $batch.files
76+
}
77+
else
78+
{
79+
$commit += $batch.files
80+
}
81+
82+
$pageCounter++
83+
84+
} until ($batch.files.Count -eq 0)
85+
86+
# filter removed files
87+
$files = $commit.where{$_.status -ne 'removed' -and $_.filename -match '(devices)'}
88+
}
89+
90+
# get file names only
91+
$files1 = $files | % {$_.filename} | Where-Object {$_ -match 'samples/*'}
92+
93+
if($null -eq $files1)
94+
{
95+
Write-host "No 'samples' found to build"
96+
exit
97+
}
98+
99+
Write-host "##[group] Files changed:"
100+
$files1 | ForEach-Object { Write-host $_ }
101+
Write-host "##[endgroup]"
102+
103+
# pattern to select samples folder name
104+
$pattern = '(samples\/)(?<folder>[a-zA-Z0-9._]+)(?>\/)'
105+
106+
# filter out the collection
107+
$results = [Regex]::Matches($files1, $pattern)
108+
109+
# get unique folder names
110+
$sampleFolders = $results | Sort-Object | Select-Object | Foreach-Object {$_.Groups["folder"].Value} | Get-Unique
111+
112+
Write-host "Found $($sampleFolders.count) sample(s) to build"
113+
114+
write-host "##[section] Processing samples..."
115+
116+
foreach ($folder in $sampleFolders)
117+
{
118+
"" | Write-Host -ForegroundColor Yellow
119+
"***********************" | Write-Host -ForegroundColor Yellow
120+
"##[group] Processing sample '$folder'..." | Write-Host -ForegroundColor Yellow
121+
"***********************" | Write-Host -ForegroundColor Yellow
122+
"" | Write-Host -ForegroundColor Yellow
123+
124+
try
125+
{
126+
# try to find all solution files
127+
$solutionFiles = Get-ChildItem -Path "samples\$folder\" -Include "*.sln" -Recurse
128+
129+
if($null -eq $solutionFiles)
130+
{
131+
"" | Write-Host -ForegroundColor Red
132+
"*****************************************" | Write-Host -ForegroundColor Red
133+
"##[error] >>>ERROR: Couldn't find any solution files!" | Write-Host -ForegroundColor Red
134+
throw "Couldn't find any solution file inside '$folder'..."
135+
}
136+
else
137+
{
138+
foreach ($slnFile in $solutionFiles)
139+
{
140+
"" | Write-Host -ForegroundColor Yellow
141+
"##[command] INFO: Building solution: '$slnFile'" | Write-Host -ForegroundColor Yellow
142+
"" | Write-Host -ForegroundColor Yellow
143+
144+
# need to restore NuGets first
145+
write-host "##[command] Performing nuget restore for '$slnFile'."
146+
nuget restore $slnFile
147+
if (-not $?)
148+
{
149+
"" | Write-Host -ForegroundColor Red
150+
"*****************************************" | Write-Host -ForegroundColor Red
151+
"##[error] >>>ERROR: Couldn't restore solution: '$slnFile'!" | Write-Host -ForegroundColor Red
152+
# set flag
153+
$buildFailed = $true
154+
}
155+
156+
# build solution
157+
write-host "##[command] Running msbuild."
158+
& "$msbuild" "$slnFile" /verbosity:normal /p:Configuration=Release
159+
if (-not $?) {
160+
"" | Write-Host -ForegroundColor Red
161+
"*****************************************" | Write-Host -ForegroundColor Red
162+
"##[error] >>>ERROR: Failed building solution: '$slnFile'!" | Write-Host -ForegroundColor Red
163+
# set flag
164+
$buildFailed = $true
165+
}
166+
}
167+
}
168+
}
169+
catch
170+
{
171+
"" | Write-Host -ForegroundColor Red
172+
"*****************************************" | Write-Host -ForegroundColor Red
173+
"##[error] >>>ERROR: build failed, check output <<<" | Write-Host -ForegroundColor Red
174+
"*****************************************" | Write-Host -ForegroundColor Red
175+
"" | Write-Host -ForegroundColor Red
176+
177+
"" | Write-Host -ForegroundColor Red
178+
"Exception was: $_" | Write-Host -ForegroundColor Red
179+
"" | Write-Host -ForegroundColor Red
180+
181+
# set flag
182+
$buildFailed = $true
183+
}
184+
185+
write-host "##[endgroup]"
186+
}
187+
188+
if($buildFailed)
189+
{
190+
"********************************" | Write-Host -ForegroundColor Red
191+
"##[error] Check >>>ERROR<<< messages above" | Write-Host -ForegroundColor Red
192+
"********************************" | Write-Host -ForegroundColor Red
193+
194+
exit 1
195+
}

NuGet.Config

-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
<configuration>
33
<packageSources>
44
<add key="NuGet" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
5-
<add key="Azure Artifacts nanoFramework dev" value="https://pkgs.dev.azure.com/nanoframework/feed/_packaging/sandbox/nuget/v3/index.json" protocolVersion="3" />
65
</packageSources>
76
</configuration>

azure-pipelines.yml

+12-130
Original file line numberDiff line numberDiff line change
@@ -38,151 +38,33 @@ steps:
3838
# need this here in order to persist GitHub credentials
3939
# only need the latest commit
4040
- checkout: self
41-
- checkout: templates
42-
fetchDepth: 1
4341

4442
- script: |
4543
git config --global user.email '[email protected]'
4644
git config --global user.name 'nfbot'
4745
git config --global core.autocrlf true
4846
displayName: Setup git identity
4947

50-
- task: UseDotNet@2
51-
displayName: Install .NET SDK
48+
- task: Cache@2
49+
displayName: Cache NuGet packages
50+
condition: succeeded()
51+
continueOnError: true
5252
inputs:
53-
packageType: sdk
54-
version: 6.x
55-
performMultiLevelLookup: true
56-
57-
- template: azure-pipelines-templates/install-nuget.yml@templates
53+
key: 'nuget | **/packages.config'
54+
path: $(UserProfile)/.nuget/packages
5855

5956
- task: InstallNanoMSBuildComponents@1
6057
displayName: Install nanoFramework MSBuild components
6158
env:
6259
GITHUB_TOKEN: $(GitHubToken)
6360

64-
# only build solutions that need to be build
65-
- powershell: |
66-
# setup msbuild
67-
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
68-
$msbuild = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
69-
70-
if ($msbuild) {
71-
$msbuild = join-path $msbuild 'MSBuild\Current\Bin\amd64\MSBuild.exe'
72-
}
73-
74-
# compute authorization header in format "AUTHORIZATION: basic 'encoded token'"
75-
# 'encoded token' is the Base64 of the string "nfbot:personal-token"
76-
$auth = "basic $([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("nfbot:${env:MY_GITHUBTOKEN}"))))"
77-
78-
if($env:System_PullRequest_PullRequestId -ne $null)
79-
{
80-
"" | Write-Host -ForegroundColor Yellow
81-
"**********************" | Write-Host -ForegroundColor Yellow
82-
"* Building from a PR *" | Write-host -ForegroundColor Yellow
83-
"**********************" | Write-Host -ForegroundColor Yellow
84-
"" | Write-Host -ForegroundColor Yellow
85-
86-
# get files changed in PR, if this is a PR
87-
# can get max of 100 files using this method (for more requires paging)
88-
$commit = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/pulls/$env:System_PullRequest_PullRequestNumber/files?per_page=100" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET
89-
90-
# filter removed files
91-
$files = $commit.where{$_.status -ne 'removed'}
92-
}
93-
else
94-
{
95-
"" | Write-Host -ForegroundColor Yellow
96-
"**************************" | Write-Host -ForegroundColor Yellow
97-
"* Building from a commit *" | Write-host -ForegroundColor Yellow
98-
"**************************" | Write-Host -ForegroundColor Yellow
99-
"" | Write-Host -ForegroundColor Yellow
100-
101-
# get files changed in the commit, if this is NOT a PR
102-
$commit = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/commits/$(Build.SourceVersion)" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET
103-
104-
# filter removed files
105-
$files = $commit.files.where{$_.status -ne 'removed'}
106-
}
107-
108-
# get file names only
109-
$files1 = $files | % {$_.filename} | Where-Object {$_ -match 'samples/*'}
110-
111-
if($null -eq $files1)
112-
{
113-
Write-host "No 'samples' found to build"
114-
exit
115-
}
116-
117-
Write-host "Files changed:"
118-
$files1 | % { Write-host $_ }
119-
Write-host ""
120-
121-
# pattern to select samples folder name
122-
$pattern = '(samples\/)(?<folder>[a-zA-Z0-9._]+)(?>\/)'
123-
124-
# filter out the collection
125-
$results = [Regex]::Matches($files1, $pattern)
126-
127-
# get unique folder names
128-
$sampleFolders = $results | Sort-Object | Select-Object | Foreach-Object {$_.Groups["folder"].Value} | Get-Unique
129-
130-
Write-host "Found $($sampleFolders.count) sample(s) to build"
131-
132-
foreach ($folder in $sampleFolders)
133-
{
134-
try
135-
{
136-
"" | Write-Host -ForegroundColor Yellow
137-
"***********************" | Write-Host -ForegroundColor Yellow
138-
"Processing sample '$folder'..." | Write-Host -ForegroundColor Yellow
139-
"***********************" | Write-Host -ForegroundColor Yellow
140-
"" | Write-Host -ForegroundColor Yellow
141-
142-
# try to find all solution files
143-
$solutionFiles = Get-ChildItem -Path "samples\$folder\" -Include "*.sln" -Recurse
144-
145-
if($null -eq $solutionFiles)
146-
{
147-
"Couldn't find any solution file!" | Write-Host -ForegroundColor Red
148-
throw "Couldn't find any solution file inside '$folder'..."
149-
}
150-
else
151-
{
152-
foreach ($sln in $solutionFiles)
153-
{
154-
"" | Write-Host -ForegroundColor Yellow
155-
"INFO: Building solution: '$sln'" | Write-Host -ForegroundColor Yellow
156-
"" | Write-Host -ForegroundColor Yellow
157-
158-
# need to restore NuGets first
159-
nuget restore $sln
160-
if (-not $?) { throw "Error restoring '$sln'" }
161-
162-
# build solution
163-
& "$msbuild" "$sln" /verbosity:normal /p:Configuration=Release
164-
if (-not $?) { throw "Error building '$sln'" }
165-
}
166-
}
167-
}
168-
catch
169-
{
170-
"" | Write-Host -ForegroundColor Red
171-
"*****************************************" | Write-Host -ForegroundColor Red
172-
">>>ERROR: build failed, check output <<<" | Write-Host -ForegroundColor Red
173-
"*****************************************" | Write-Host -ForegroundColor Red
174-
"" | Write-Host -ForegroundColor Red
175-
176-
"" | Write-Host -ForegroundColor Red
177-
"Eception was: $_" | Write-Host -ForegroundColor Red
178-
"" | Write-Host -ForegroundColor Red
179-
180-
# set flag
181-
$buildFailed = $true
182-
}
183-
}
61+
# only build solutions that were changed
62+
- task: PowerShell@2
18463
displayName: Build solutions
185-
workingDirectory: 'Samples'
64+
inputs:
65+
targetType: 'filePath'
66+
filePath: '$(System.DefaultWorkingDirectory)\.pipeline-assets\build-solutions.ps1'
67+
failOnStderr: true
18668
env:
18769
MY_GITHUBTOKEN: $(GitHubToken)
18870

0 commit comments

Comments
 (0)