forked from nanoframework/nanoFirmwareFlasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure-pipelines.yml
340 lines (282 loc) · 11.6 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
trigger:
branches:
include: [main, develop, "release-*" ]
paths:
exclude: [README.md, LICENSE.md, NuGet.Config, .github_changelog_generator, .gitignore]
tags:
include: ["v*"]
# no pr config, we want to trigger builds for all PRs on all branches
# add nf-tools repo to resources (for Azure Pipelines templates)
resources:
repositories:
- repository: templates
type: github
name: nanoframework/nf-tools
endpoint: nanoframework
jobs:
##############################
- job: Check_Build_Options
pool:
vmImage: 'windows-2019'
steps:
- checkout: self
# get commint message
- powershell: |
if($env:StartReleaseCandidate -like "true")
{
# this is a release prep so NO build
echo "##vso[task.setvariable variable=SKIP_BUILD;isOutput=true]true"
Write-Host "Release preparation, skipping build."
}
name: BuildOptions
displayName: Evaluate build options
- task: DotNetCoreCLI@2
inputs:
command: custom
custom: tool
arguments: install -g nbgv
condition: or( eq( variables['StartReleaseCandidate'], true ), ne(variables['system.pullrequest.isfork'], true) )
displayName: Install NBGV tool
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
# 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)"))))"
cd "$env:Agent_TempDirectory" > $null
Write-Host "Cloning from: $env:BUILD_REPOSITORY_URI"
git clone $env:BUILD_REPOSITORY_URI repo
cd repo > $null
git config --global gc.auto 0
git config --global user.name nfbot
git config --global user.email [email protected]
git config --global core.autocrlf true
Write-Host "Checkout develop branch..."
git checkout --quiet develop > $null
# prepare release and capture output
Write-Host "Prepare release with NBGV..."
$release = nbgv prepare-release
Write-Host "Prepare commit..."
# get commit message for the merge
$commitMessage = git log -1 --pretty=%B
# amend commit message to skip build
git commit --amend -m "$commitMessage" -m "***NO_CI***" > $null
Write-Host "Pushing changes to GitHub..."
# push all changes to github
git -c http.extraheader="AUTHORIZATION: $auth" push --quiet --all origin
# get release branch name
$branch = $release.Split(' ')[0]
Write-Host "Prepare PR..."
# start PR for release
$prRequestBody = @{title="Release $branch";body="";head="$branch";base="main"} | ConvertTo-Json
$githubApiEndpoint = "https://api.github.com/repos/$env:BUILD_REPOSITORY_NAME/pulls"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$headers = @{}
$headers.Add("Authorization","$auth")
$headers.Add("Accept","application/vnd.github.symmetra-preview+json")
try
{
$result = Invoke-RestMethod -Method Post -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer -Uri $githubApiEndpoint -Header $headers -ContentType "application/json" -Body $prRequestBody
'Started PR for new release...' | Write-Host -NoNewline
'OK' | Write-Host -ForegroundColor Green
}
catch
{
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
"Error starting PR: $responseBody" | Write-Host -ForegroundColor Red
}
workingDirectory: $(Agent.TempDirectory)
ignoreLASTEXITCODE: true
condition: eq( variables['StartReleaseCandidate'], true )
displayName: NBGV prepare release
###########################################################
# build tool
- job: Build_tool
condition: ne( dependencies.Check_Build_Options.outputs['BuildOptions.SKIP_BUILD'], true )
dependsOn:
- Check_Build_Options
pool:
vmImage: 'windows-2019'
variables:
DOTNET_NOLOGO: true
buildPlatform: 'x64'
buildConfiguration: 'Release'
solution: 'nanoFirmwareFlasher.sln'
steps:
# need this here in order to persist GitHub credentials
- checkout: self
persistCredentials: true
- script: |
git config --global user.email "[email protected]"
git config --global user.name "nfbot"
displayName: Setup git identity
- template: azure-pipelines-templates/install-nuget.yml@templates
- task: UseDotNet@2
displayName: Install .NET SDK
inputs:
packageType: sdk
version: 5.0.300
- task: DotNetCoreCLI@2
displayName: Restore NuGet packages
inputs:
command: restore
verbosityRestore: minimal
projects: nanoFirmwareFlasher.sln
feedsToUse: config
nugetConfigPath: NuGet.Config
- script: dotnet build -c $(BuildConfiguration) /p:PublicRelease=true --no-restore /t:build,pack
displayName: Build NuGet package
- script: dotnet pack --runtime win-x64 -c $(BuildConfiguration) -p:PublicRelease=true -p:PackGlobalTool=true --no-restore
displayName: Build .NET Core Tool NuGet package
- task: PowerShell@2
condition: succeeded()
displayName: Get NuGet build number
inputs:
targetType: 'inline'
script: |
$MyNuGetVersion = $env:NBGV_NuGetPackageVersion -replace "\-g$env:NBGV_GitCommitIdShort", ""
# replace preview with alpha if this is a PR build
if($env:System_PullRequest_PullRequestId -ne $null)
{
$MyNuGetVersion = $MyNuGetVersion -replace "preview", "alpha"
}
Write-Host "NuGet build number is $MyNuGetVersion"
Write-Host "$("##vso[task.setvariable variable=MY_NUGET_VERSION]")$MyNuGetVersion"
# update could build number (only possible if this is not a PR from a fork)
- task: PowerShell@2
condition: and( succeeded(), ne(variables['system.pullrequest.isfork'], true) )
displayName: Update cloud build number
inputs:
targetType: 'inline'
script: Write-Host "$("##vso[build.updatebuildnumber]")$env:NBGV_NuGetPackageVersion"
- powershell: |
# get subject and commit message for commit
$commitMessage = git log --format='%B' -1
# need to flatten message by removing new lines
$commitMessage = $commitMessage -replace "`r`n", " "
if($commitMessage -like "***PUBLISH_RELEASE***")
{
# set variable
Write-Host "$("##vso[task.setvariable variable=RELEASE_DRAFT]")false"
Write-Host "Release draft: FALSE"
}
else
{
# set variable
Write-Host "$("##vso[task.setvariable variable=RELEASE_DRAFT]")true"
Write-Host "Release draft: TRUE"
}
displayName: set release draft var
- task: CopyFiles@1
condition: succeeded()
displayName: Collecting deployable artifacts
inputs:
sourceFolder: $(Agent.BuildDirectory)
Contents: |
**\*.nupkg
TargetFolder: '$(Build.ArtifactStagingDirectory)'
flattenFolders: true
- task: DotNetCoreCLI@2
displayName: Install SignTool tool
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], '') )
inputs:
command: custom
custom: tool
arguments: install --tool-path . SignClient
- pwsh: |
.\SignClient "Sign" `
--baseDirectory "$(Build.ArtifactStagingDirectory)" `
--input "**/*.nupkg" `
--config "$(Build.Repository.LocalPath)\config\SignClient.json" `
--filelist "$(Build.Repository.LocalPath)\config\filelist.txt" `
--user "$(SignClientUser)" `
--secret '$(SignClientSecret)' `
--name ".NET nanoFramework firmware flasher" `
--description ".NET nanoFramework firmware flasher" `
--descriptionUrl "https://github.com/$env:Build_Repository_Name"
displayName: Sign packages
continueOnError: true
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], '') )
# publish artifacts (only possible if this is not a PR originated on a fork)
- task: PublishBuildArtifacts@1
condition: and( succeeded(), ne(variables['system.pullrequest.isfork'], true) )
displayName: Publish deployables artifacts
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: deployables
ArtifactType: Container
# push NuGet class lib package to NuGet (happens on all builds except PRs)
- task: NuGetCommand@2
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], ''), not( startsWith(variables['Build.SourceBranch'], 'refs/tags/v') ) )
displayName: Push nanoff NuGet package to NuGet
continueOnError: true
inputs:
command: push
feedsToUse: select
nuGetFeedType: external
allowPackageConflicts: true
packagesToPush: '$(Build.ArtifactStagingDirectory)\nanoff.$(NBGV_NuGetPackageVersion).nupkg'
publishFeedCredentials: 'NuGet-nanoFirmwareFlasher'
# push NuGet class lib package to NuGet (happens on all builds except PRs)
- task: NuGetCommand@2
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], ''), not( startsWith(variables['Build.SourceBranch'], 'refs/tags/v') ) )
displayName: Push library NuGet package to NuGet
continueOnError: true
inputs:
command: push
feedsToUse: select
nuGetFeedType: external
allowPackageConflicts: true
packagesToPush: '$(Build.ArtifactStagingDirectory)\nanoFramework.Tools.FirmwareFlasher.$(NBGV_NuGetPackageVersion).nupkg'
publishFeedCredentials: 'NuGet-nanoFirmwareFlasher'
# create or update GitHub release
- task: GithubRelease@1
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], ''), not( startsWith(variables['Build.SourceBranch'], 'refs/tags/v') ) )
displayName: Create/Update GitHub PREVIEW release
inputs:
gitHubConnection: 'github.com_nano-$(System.TeamProject)'
tagSource: userSpecifiedTag
tag: v$(MY_NUGET_VERSION)
title: 'nano firmware flasher v$(MY_NUGET_VERSION)'
releaseNotesSource: inline
releaseNotesInline: 'add description here'
assets: ''
isPreRelease: true
addChangeLog: false
# create or update GitHub release
- task: GithubRelease@1
condition: and( succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'), not(contains(variables['Build.SourceBranch'], 'preview') ) )
displayName: Create/Update GitHub stable release
inputs:
gitHubConnection: 'github.com_nano-$(System.TeamProject)'
tagSource: userSpecifiedTag
tag: v$(MY_NUGET_VERSION)
title: 'nano firmware flasher v$(MY_NUGET_VERSION)'
releaseNotesSource: inline
releaseNotesInline: 'add description here'
assets: ''
isPreRelease: false
addChangeLog: false
action: edit
##################################
# report build failure to Discord
- job: Report_Build_Failure
dependsOn:
- Check_Build_Options
- Build_tool
condition: or( failed('Check_Build_Options'), failed('Build_tool') )
pool:
vmImage: 'windows-2019'
steps:
- checkout: self
# step from template @ nf-tools repo
- template: azure-pipelines-templates/discord-webhook.yml@templates
parameters:
status: 'failure'
webhookUrl: '$(DiscordWebhook)'
message: ''