-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
azure-pipelines.yml
235 lines (195 loc) · 7.57 KB
/
azure-pipelines.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Copyright (c) .NET Foundation and Contributors
# See LICENSE file in the project root for full license information.
trigger:
branches:
include:
- main
paths:
exclude:
- .gitignore
- CODE_OF_CONDUCT.md
- LICENSE.md
- README.md
- README.zh-cn.md
- NuGet.Config
- .github/*
# PR always trigger build
pr:
autoCancel: true
# add nf-tools repo to resources (for Azure Pipelines templates)
resources:
repositories:
- repository: templates
type: github
name: nanoframework/nf-tools
endpoint: nanoframework
pool:
vmImage: 'windows-latest'
variables:
DOTNET_NOLOGO: true
steps:
# need this here in order to persist GitHub credentials
# only need the latest commit
- checkout: self
- checkout: templates
fetchDepth: 1
- script: |
git config --global user.email '[email protected]'
git config --global user.name 'nfbot'
git config --global core.autocrlf true
displayName: Setup git identity
- task: UseDotNet@2
displayName: Install .NET SDK
inputs:
packageType: sdk
version: 6.x
performMultiLevelLookup: true
- template: azure-pipelines-templates/install-nuget.yml@templates
- task: InstallNanoMSBuildComponents@1
displayName: Install nanoFramework MSBuild components
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
}
}
displayName: Build solutions
workingDirectory: 'Samples'
env:
MY_GITHUBTOKEN: $(GitHubToken)
# Adjust the readmes
- task: DotNetCoreCLI@2
displayName: Adjust READMEs
condition: >-
and(
succeeded(),
eq(variables['System.PullRequest.PullRequestId'], '')
)
inputs:
command: custom
custom: run
projects: '**/device-listing.csproj'
workingDirectory: 'Samples'
- task: PowerShell@2
condition: >-
and(
succeeded(),
eq(variables['System.PullRequest.PullRequestId'], '')
)
displayName: Push READMEs to GitHub
continueOnError: true
inputs:
targetType: 'inline'
workingDirectory: 'Samples'
script: |
git add README.md
git add README.zh-cn.md
git commit -m "Update READMEs" -m"***NO_CI***"
Write-Host "Rebasing..."
git rebase origin/main
Write-Host "Pushing READMEs to $(Build.SourceBranchName)"
# 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:$(GitHubToken)")))"
git -c http.extraheader="AUTHORIZATION: $auth" push origin "HEAD:$(Build.SourceBranchName)"
# report error
- template: azure-pipelines-templates/discord-webhook-task.yml@templates
parameters:
status: 'failure'
webhookUrl: '$(DiscordWebhook)'
message: ''