Skip to content

Commit

Permalink
Work CI-CD
Browse files Browse the repository at this point in the history
- Move build script to file.
- Update build script to use AZDO formatted output.
- Remove private feed from Nuget config.

***NO_CI***
  • Loading branch information
josesimoes committed Feb 7, 2025
1 parent 8642de3 commit f93c9c4
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 122 deletions.
194 changes: 194 additions & 0 deletions .pipeline-assets/build-solutions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
# Copyright (c) .NET Foundation and Contributors
# See LICENSE file in the project root for full license information.

# 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

# setup msbuild
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$msbuild = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath

if ($msbuild) {
$msbuild = join-path $msbuild 'MSBuild\Current\Bin\amd64\MSBuild.exe'
}

# compute authorization header in format "AUTHORIZATION: basic 'encoded token'"
# 'encoded token' is the Base64 of the string "nfbot:personal-token"
$auth = "basic $([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("nfbot:${env:MY_GITHUBTOKEN}"))))"

if($env:System_PullRequest_PullRequestId -ne $null)
{
"" | Write-Host -ForegroundColor Yellow
"**********************" | Write-Host -ForegroundColor Yellow
"* Building from a PR *" | Write-host -ForegroundColor Yellow
"**********************" | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow

# get files changed in PR
$pageCounter = 1

do
{
"##[debug] INFO: iteration $pageCounter" | Write-Host


$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

if($null -eq $commit)
{
$commit = $batch
}
else
{
$commit += $batch
}

$pageCounter++

} until ($batch.Count -eq 0)

# filter removed files
$files = $commit.where{$_.status -ne 'removed'}

}
else
{
"" | Write-Host -ForegroundColor Yellow
"**************************" | Write-Host -ForegroundColor Yellow
"* Building from a commit *" | Write-host -ForegroundColor Yellow
"**************************" | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow

# get files changed in the commit, if this is NOT a PR
$commit = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/commits/$(Build.SourceVersion)" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET

# get files changed in the commit
$pageCounter = 1

do
{
"##[command] Get API file change page: $pageCounter" | Write-Host

$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

if($null -eq $commit)
{
$commit = $batch.files
}
else
{
$commit += $batch.files
}

$pageCounter++

} until ($batch.files.Count -eq 0)

# filter removed files
$files = $commit.where{$_.status -ne 'removed' -and $_.filename -match '(devices)'}
}

# get file names only
$files1 = $files | % {$_.filename} | Where-Object {$_ -match 'samples/*'}

if($null -eq $files1)
{
Write-host "No 'samples' found to build"
exit
}

Write-host "##[group] Files changed:"
$files1 | ForEach-Object { Write-host $_ }
Write-host "##[endgroup]"

# pattern to select samples folder name
$pattern = '(samples\/)(?<folder>[a-zA-Z0-9._]+)(?>\/)'

# filter out the collection
$results = [Regex]::Matches($files1, $pattern)

# get unique folder names
$sampleFolders = $results | Sort-Object | Select-Object | Foreach-Object {$_.Groups["folder"].Value} | Get-Unique

Write-host "Found $($sampleFolders.count) sample(s) to build"

write-host "##[section] Processing samples..."
foreach ($folder in $sampleFolders)
{
"" | Write-Host -ForegroundColor Yellow
"***********************" | Write-Host -ForegroundColor Yellow
"##[group] Processing sample '$folder'..." | Write-Host -ForegroundColor Yellow
"***********************" | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow

try
{
# try to find all solution files
$solutionFiles = Get-ChildItem -Path "samples\$folder\" -Include "*.sln" -Recurse

if($null -eq $solutionFiles)
{
"" | Write-Host -ForegroundColor Red
"*****************************************" | Write-Host -ForegroundColor Red
"##[error] >>>ERROR: Couldn't find any solution files!" | Write-Host -ForegroundColor Red
throw "Couldn't find any solution file inside '$folder'..."
}
else
{
foreach ($sln in $solutionFiles)
{
"" | Write-Host -ForegroundColor Yellow
"##[command] INFO: Building solution: '$sln'" | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow

# need to restore NuGets first
write-host "##[command] Performing nuget restore for '$solutionFile'."
nuget restore $sln
if (-not $?)
{
"" | Write-Host -ForegroundColor Red
"*****************************************" | Write-Host -ForegroundColor Red
"##[error] >>>ERROR: Couldn't restore solution: '$solutionFile'!" | Write-Host -ForegroundColor Red
# set flag
$buildFailed = $true
}

# build solution
write-host "##[command] Running msbuild."
& "$msbuild" "$sln" /verbosity:normal /p:Configuration=Release
if (-not $?) {
"" | Write-Host -ForegroundColor Red
"*****************************************" | Write-Host -ForegroundColor Red
"##[error] >>>ERROR: Failed building solution: '$solutionFile'!" | Write-Host -ForegroundColor Red
# set flag
$buildFailed = $true
}
}
}
}
catch
{
"" | Write-Host -ForegroundColor Red
"*****************************************" | Write-Host -ForegroundColor Red
"##[error] >>>ERROR: build failed, check output <<<" | Write-Host -ForegroundColor Red
"*****************************************" | Write-Host -ForegroundColor Red
"" | Write-Host -ForegroundColor Red

"" | Write-Host -ForegroundColor Red
"Eception was: $_" | Write-Host -ForegroundColor Red
"" | Write-Host -ForegroundColor Red

# set flag
$buildFailed = $true
}

write-host "##[endgroup]"
}

if($buildFailed)
{
"********************************" | Write-Host -ForegroundColor Red
"##[error] Check >>>ERROR<<< messages above" | Write-Host -ForegroundColor Red
"********************************" | Write-Host -ForegroundColor Red

exit 1
}
1 change: 0 additions & 1 deletion NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
<configuration>
<packageSources>
<add key="NuGet" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Azure Artifacts nanoFramework dev" value="https://pkgs.dev.azure.com/nanoframework/feed/_packaging/sandbox/nuget/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
128 changes: 7 additions & 121 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,128 +61,14 @@ steps:
env:
GITHUB_TOKEN: $(GitHubToken)

# only build solutions that need to be build
- powershell: |
# setup msbuild
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$msbuild = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
if ($msbuild) {
$msbuild = join-path $msbuild 'MSBuild\Current\Bin\amd64\MSBuild.exe'
}
# compute authorization header in format "AUTHORIZATION: basic 'encoded token'"
# 'encoded token' is the Base64 of the string "nfbot:personal-token"
$auth = "basic $([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("nfbot:${env:MY_GITHUBTOKEN}"))))"
if($env:System_PullRequest_PullRequestId -ne $null)
{
"" | Write-Host -ForegroundColor Yellow
"**********************" | Write-Host -ForegroundColor Yellow
"* Building from a PR *" | Write-host -ForegroundColor Yellow
"**********************" | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
# get files changed in PR, if this is a PR
# can get max of 100 files using this method (for more requires paging)
$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
# filter removed files
$files = $commit.where{$_.status -ne 'removed'}
}
else
{
"" | Write-Host -ForegroundColor Yellow
"**************************" | Write-Host -ForegroundColor Yellow
"* Building from a commit *" | Write-host -ForegroundColor Yellow
"**************************" | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
# get files changed in the commit, if this is NOT a PR
$commit = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/commits/$(Build.SourceVersion)" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET
# filter removed files
$files = $commit.files.where{$_.status -ne 'removed'}
}
# get file names only
$files1 = $files | % {$_.filename} | Where-Object {$_ -match 'samples/*'}
if($null -eq $files1)
{
Write-host "No 'samples' found to build"
exit
}
Write-host "Files changed:"
$files1 | % { Write-host $_ }
Write-host ""
# pattern to select samples folder name
$pattern = '(samples\/)(?<folder>[a-zA-Z0-9._]+)(?>\/)'
# filter out the collection
$results = [Regex]::Matches($files1, $pattern)
# get unique folder names
$sampleFolders = $results | Sort-Object | Select-Object | Foreach-Object {$_.Groups["folder"].Value} | Get-Unique
Write-host "Found $($sampleFolders.count) sample(s) to build"
foreach ($folder in $sampleFolders)
{
try
{
"" | Write-Host -ForegroundColor Yellow
"***********************" | Write-Host -ForegroundColor Yellow
"Processing sample '$folder'..." | Write-Host -ForegroundColor Yellow
"***********************" | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
# try to find all solution files
$solutionFiles = Get-ChildItem -Path "samples\$folder\" -Include "*.sln" -Recurse
if($null -eq $solutionFiles)
{
"Couldn't find any solution file!" | Write-Host -ForegroundColor Red
throw "Couldn't find any solution file inside '$folder'..."
}
else
{
foreach ($sln in $solutionFiles)
{
"" | Write-Host -ForegroundColor Yellow
"INFO: Building solution: '$sln'" | Write-Host -ForegroundColor Yellow
"" | Write-Host -ForegroundColor Yellow
# need to restore NuGets first
nuget restore $sln
if (-not $?) { throw "Error restoring '$sln'" }
# build solution
& "$msbuild" "$sln" /verbosity:normal /p:Configuration=Release
if (-not $?) { throw "Error building '$sln'" }
}
}
}
catch
{
"" | Write-Host -ForegroundColor Red
"*****************************************" | Write-Host -ForegroundColor Red
">>>ERROR: build failed, check output <<<" | Write-Host -ForegroundColor Red
"*****************************************" | Write-Host -ForegroundColor Red
"" | Write-Host -ForegroundColor Red
"" | Write-Host -ForegroundColor Red
"Eception was: $_" | Write-Host -ForegroundColor Red
"" | Write-Host -ForegroundColor Red
# set flag
$buildFailed = $true
}
}
# only build solutions that were changed
- task: PowerShell@2
displayName: Build solutions
workingDirectory: 'Samples'
inputs:
workingDirectory: 'Samples'
targetType: 'filePath'
filePath: '$(Build.Repository.LocalPath)\Samples\.pipeline-assets\build-solutions.ps1'
failOnStderr: true
env:
MY_GITHUBTOKEN: $(GitHubToken)

Expand Down

0 comments on commit f93c9c4

Please sign in to comment.