Skip to content

Commit 61a0a8a

Browse files
authored
Implement update dependents (#99)
1 parent e9260dc commit 61a0a8a

File tree

2 files changed

+256
-19
lines changed

2 files changed

+256
-19
lines changed

azure-pipelines.yml

Lines changed: 139 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,50 @@ jobs:
3030

3131
- checkout: self
3232

33-
# get commint message
33+
# get commit message
3434
- powershell: |
3535
36-
if($env:StartReleaseCandidate -like "true")
36+
# default to false
37+
$update = $false
38+
39+
if($env:System_PullRequest_PullRequestId -ne $null)
3740
{
38-
# this is a release prep so NO build
39-
echo "##vso[task.setvariable variable=SKIP_BUILD;isOutput=true]true"
40-
41-
Write-Host "Release preparation, skipping build."
41+
# PR build, nothing interesting in commit message
42+
Write-Host "Build from PR"
4243
}
43-
44+
else
45+
{
46+
if($env:StartReleaseCandidate -like "true")
47+
{
48+
# this is a release prep so NO build
49+
echo "##vso[task.setvariable variable=SKIP_BUILD;isOutput=true]true"
50+
51+
Write-Host "Release preparation, skipping build."
52+
}
53+
else
54+
{
55+
# build NOT from PR
56+
Write-Host "Build NOT from PR, commit ID: $env:Build_SourceVersion"
57+
58+
# get PR associated with commit
59+
$prUrl = "https://api.github.com/repos/$env:Build_Repository_Name/commits/$env:Build_SourceVersion/pulls"
60+
$commit = Invoke-RestMethod -Uri $prUrl -ContentType "application/json" -Headers @{"Accept"="application/vnd.github.groot-preview+json"} -Method GET
61+
62+
if($commit -ne $null)
63+
{
64+
# there is a PR, check labels
65+
$updateDependents = $commit.labels | where {$_.Name -eq 'CI: Update Dependents'}
66+
if($updateDependents -ne $null)
67+
{
68+
$update = $true
69+
}
70+
}
71+
}
72+
}
73+
74+
# set variable to foward to jobs
75+
echo "##vso[task.setvariable variable=RUN_UPDATE_DEPENDENTS]$update"
76+
4477
name: BuildOptions
4578
displayName: Evaluate build options
4679
@@ -130,7 +163,7 @@ jobs:
130163
###########################################################
131164
# build tool
132165
- job: Build_tool
133-
condition: ne( dependencies.Check_Build_Options.outputs['BuildOptions.SKIP_BUILD'], true )
166+
condition: ne(dependencies.Check_Build_Options.outputs['BuildOptions.SKIP_BUILD'], true)
134167
dependsOn:
135168
- Check_Build_Options
136169

@@ -158,12 +191,27 @@ jobs:
158191

159192
- task: UseDotNet@2
160193
displayName: Install .NET SDK
194+
condition: eq(variables['UPDATE_DEPENDENTS'], 'false')
161195
inputs:
162196
packageType: sdk
163197
version: 6.x
164198

199+
- task: DotNetCoreCLI@2
200+
displayName: Install NBGV tool
201+
condition: ne(variables['system.pullrequest.isfork'], true)
202+
inputs:
203+
command: custom
204+
custom: tool
205+
arguments: install -g nbgv
206+
207+
# only required when updating dependents
208+
- script: nbgv cloud
209+
condition: eq(variables['UPDATE_DEPENDENTS'], 'true')
210+
displayName: Set Could Version
211+
165212
- task: DotNetCoreCLI@2
166213
displayName: Restore NuGet packages
214+
condition: eq(variables['UPDATE_DEPENDENTS'], 'false')
167215
inputs:
168216
command: restore
169217
verbosityRestore: minimal
@@ -173,12 +221,18 @@ jobs:
173221

174222
- script: dotnet build -c $(BuildConfiguration) /p:PublicRelease=true --no-restore /t:build,pack
175223
displayName: Build NuGet package
224+
condition: eq(variables['UPDATE_DEPENDENTS'], 'false')
176225

177226
- script: dotnet pack --runtime win-x64 -c $(BuildConfiguration) -p:PublicRelease=true -p:PackGlobalTool=true --no-restore
178227
displayName: Build .NET Core Tool NuGet package
228+
condition: eq(variables['UPDATE_DEPENDENTS'], 'false')
179229

180230
- task: PowerShell@2
181-
condition: succeeded()
231+
condition: >-
232+
and(
233+
succeeded(),
234+
eq(variables['UPDATE_DEPENDENTS'], 'false')
235+
)
182236
displayName: Get NuGet build number
183237
inputs:
184238
targetType: 'inline'
@@ -197,7 +251,12 @@ jobs:
197251
198252
# update could build number (only possible if this is not a PR from a fork)
199253
- task: PowerShell@2
200-
condition: and( succeeded(), ne(variables['system.pullrequest.isfork'], true) )
254+
condition: >-
255+
and(
256+
succeeded(),
257+
ne(variables['system.pullrequest.isfork'], true),
258+
eq(variables['UPDATE_DEPENDENTS'], 'false')
259+
)
201260
displayName: Update cloud build number
202261
inputs:
203262
targetType: 'inline'
@@ -224,9 +283,14 @@ jobs:
224283
}
225284
226285
displayName: set release draft var
286+
condition: eq(variables['UPDATE_DEPENDENTS'], 'false')
227287
228288
- task: CopyFiles@1
229-
condition: succeeded()
289+
condition: >-
290+
and(
291+
succeeded(),
292+
eq(variables['UPDATE_DEPENDENTS'], 'false')
293+
)
230294
displayName: Collecting deployable artifacts
231295
inputs:
232296
sourceFolder: $(Agent.BuildDirectory)
@@ -237,7 +301,12 @@ jobs:
237301

238302
- task: DotNetCoreCLI@2
239303
displayName: Install SignTool tool
240-
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], '') )
304+
condition: >-
305+
and(
306+
succeeded(),
307+
eq(variables['System.PullRequest.PullRequestId'], ''),
308+
eq(variables['UPDATE_DEPENDENTS'], 'false')
309+
)
241310
inputs:
242311
command: custom
243312
custom: tool
@@ -256,11 +325,21 @@ jobs:
256325
--descriptionUrl "https://github.com/$env:Build_Repository_Name"
257326
displayName: Sign packages
258327
continueOnError: true
259-
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], '') )
328+
condition: >-
329+
and(
330+
succeeded(),
331+
eq(variables['System.PullRequest.PullRequestId'], ''),
332+
eq(variables['UPDATE_DEPENDENTS'], 'false')
333+
)
260334
261335
# publish artifacts (only possible if this is not a PR originated on a fork)
262336
- task: PublishBuildArtifacts@1
263-
condition: and( succeeded(), ne(variables['system.pullrequest.isfork'], true) )
337+
condition: >-
338+
and(
339+
succeeded(),
340+
ne(variables['system.pullrequest.isfork'], true),
341+
eq(variables['UPDATE_DEPENDENTS'], 'false')
342+
)
264343
displayName: Publish deployables artifacts
265344
inputs:
266345
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
@@ -269,7 +348,13 @@ jobs:
269348

270349
# push NuGet class lib package to NuGet (happens on all builds except PRs)
271350
- task: NuGetCommand@2
272-
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], ''), not( startsWith(variables['Build.SourceBranch'], 'refs/tags/v') ) )
351+
condition: >-
352+
and(
353+
succeeded(),
354+
eq(variables['System.PullRequest.PullRequestId'], ''),
355+
not(startsWith(variables['Build.SourceBranch'], 'refs/tags/v')),
356+
eq(variables['UPDATE_DEPENDENTS'], 'false')
357+
)
273358
displayName: Push nanoff NuGet package to NuGet
274359
continueOnError: true
275360
inputs:
@@ -282,7 +367,13 @@ jobs:
282367

283368
# push NuGet class lib package to NuGet (happens on all builds except PRs)
284369
- task: NuGetCommand@2
285-
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], ''), not( startsWith(variables['Build.SourceBranch'], 'refs/tags/v') ) )
370+
condition: >-
371+
and(
372+
succeeded(),
373+
eq(variables['System.PullRequest.PullRequestId'], ''),
374+
not(startsWith(variables['Build.SourceBranch'], 'refs/tags/v')),
375+
eq(variables['UPDATE_DEPENDENTS'], 'false')
376+
)
286377
displayName: Push library NuGet package to NuGet
287378
continueOnError: true
288379
inputs:
@@ -292,10 +383,33 @@ jobs:
292383
allowPackageConflicts: true
293384
packagesToPush: '$(Build.ArtifactStagingDirectory)\nanoFramework.Tools.FirmwareFlasher.$(NBGV_NuGetPackageVersion).nupkg'
294385
publishFeedCredentials: 'NuGet-nanoFirmwareFlasher'
295-
386+
387+
# update dependencies
388+
- task: PowerShell@2
389+
condition: >-
390+
and(
391+
or(
392+
eq(variables['UPDATE_DEPENDENTS'], 'true'),
393+
eq(variables['RUN_UPDATE_DEPENDENTS'], 'true')
394+
),
395+
not(startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
396+
)
397+
displayName: Update dependent tools
398+
inputs:
399+
targetType: filePath
400+
filePath: azure-pipelines/update-dependencies.ps1
401+
env:
402+
MY_GITHUB_TOKEN: $(GitHubToken)
403+
296404
# create or update GitHub release
297405
- task: GithubRelease@1
298-
condition: and( succeeded(), eq(variables['System.PullRequest.PullRequestId'], ''), not( startsWith(variables['Build.SourceBranch'], 'refs/tags/v') ) )
406+
condition: >-
407+
and(
408+
succeeded(),
409+
eq(variables['System.PullRequest.PullRequestId'], ''),
410+
not(startsWith(variables['Build.SourceBranch'], 'refs/tags/v')),
411+
eq(variables['UPDATE_DEPENDENTS'], 'false')
412+
)
299413
displayName: Create/Update GitHub PREVIEW release
300414
inputs:
301415
gitHubConnection: 'github.com_nano-$(System.TeamProject)'
@@ -310,7 +424,13 @@ jobs:
310424

311425
# create or update GitHub release
312426
- task: GithubRelease@1
313-
condition: and( succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'), not(contains(variables['Build.SourceBranch'], 'preview') ) )
427+
condition: >-
428+
and(
429+
succeeded(),
430+
startsWith(variables['Build.SourceBranch'], 'refs/tags/v'),
431+
not(contains(variables['Build.SourceBranch'], 'preview')),
432+
eq(variables['UPDATE_DEPENDENTS'], 'false')
433+
)
314434
displayName: Create/Update GitHub stable release
315435
inputs:
316436
gitHubConnection: 'github.com_nano-$(System.TeamProject)'
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"Updating dependency at nf-VS Code Extension" | Write-Host
2+
3+
# compute authorization header in format "AUTHORIZATION: basic 'encoded token'"
4+
# 'encoded token' is the Base64 of the string "nfbot:personal-token"
5+
$auth = "basic $([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("nfbot:$env:MY_GITHUB_TOKEN"))))"
6+
7+
# because it can take sometime for the package to become available on the NuGet providers
8+
# need to hang in here for 1 minute (1 * 60)
9+
"Waiting 1 minute to let package process flow in Azure Artifacts feed..." | Write-Host
10+
Start-Sleep -Seconds 60
11+
12+
# init/reset these
13+
$commitMessage = ""
14+
$prTitle = ""
15+
$newBranchName = "develop-nfbot/update-dependencies/" + [guid]::NewGuid().ToString()
16+
$packageTargetVersion = $env:NBGV_NuGetPackageVersion
17+
18+
# working directory is agent temp directory
19+
Write-Debug "Changing working directory to $env:Agent_TempDirectory"
20+
Set-Location "$env:Agent_TempDirectory" | Out-Null
21+
22+
$repoName = 'nf-VSCodeExtension'
23+
24+
# clone repo and checkout develop branch
25+
Write-Debug "Init and featch $repoName repo"
26+
27+
28+
git clone --depth 1 https://github.com/nanoframework/$repoName repo
29+
Set-Location repo | Out-Null
30+
git config --global gc.auto 0
31+
git config --global user.name nfbot
32+
git config --global user.email nanoframework@outlook.com
33+
git config --global core.autocrlf true
34+
35+
Write-Host "Checkout develop branch..."
36+
git checkout --quiet develop | Out-Null
37+
38+
####################
39+
# VS Code extension
40+
41+
Write-Host "Updating nanoFramework.Tools.FirmwareFlasher version in VS Code extension..."
42+
43+
$versionRegex = "nanoFlasherVersion\s=\s\""v\d+.\d+.\d+\"""
44+
$newVersion = "nanoFlasherVersion = ""$packageTargetVersion"""
45+
46+
$buildFileName = 'scripts/build.ps1'
47+
$buildFileContent = Get-Content $buildFileName -Encoding UTF8
48+
49+
attrib $buildFileName -r
50+
$buildFileContent -replace $versionRegex, $newVersion | Out-File $buildFileName -Encoding utf8
51+
52+
#####################
53+
54+
"Bumping nanoFramework.Tools.FirmwareFlasher to $packageTargetVersion." | Write-Host -ForegroundColor Cyan
55+
56+
# build commit message
57+
$commitMessage += "Bumps nanoFramework.Tools.FirmwareFlasher to $packageTargetVersion.`n"
58+
# build PR title
59+
$prTitle = "Bumps nanoFramework.Tools.FirmwareFlasher to $packageTargetVersion"
60+
61+
# need this line so nfbot flags the PR appropriately
62+
$commitMessage += "`n[version update]`n`n"
63+
64+
# better add this warning line
65+
$commitMessage += "### :warning: This is an automated update. Merge only after all tests pass. :warning:`n"
66+
67+
Write-Debug "Git branch"
68+
69+
# create branch to perform updates
70+
git branch $newBranchName
71+
72+
Write-Debug "Checkout branch"
73+
74+
# checkout branch
75+
git checkout $newBranchName
76+
77+
Write-Debug "Add changes"
78+
79+
# commit changes
80+
git add -A > $null
81+
82+
Write-Debug "Commit changed files"
83+
84+
git commit -m "$prTitle ***NO_CI***" -m "$commitMessage" > $null
85+
86+
Write-Debug "Push changes"
87+
88+
git -c http.extraheader="AUTHORIZATION: $auth" push --set-upstream origin $newBranchName > $null
89+
90+
# start PR
91+
# we are hardcoding to 'develop' branch to have a fixed one
92+
# this is very important for tags (which don't have branch information)
93+
# considering that the base branch can be changed at the PR there is no big deal about this
94+
$prRequestBody = @{title="$prTitle";body="$commitMessage";head="$newBranchName";base="develop"} | ConvertTo-Json
95+
$githubApiEndpoint = "https://api.github.com/repos/nanoframework/$repoName/pulls"
96+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
97+
98+
$headers = @{}
99+
$headers.Add("Authorization","$auth")
100+
$headers.Add("Accept","application/vnd.github.symmetra-preview+json")
101+
102+
try
103+
{
104+
$result = Invoke-RestMethod -Method Post -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer -Uri $githubApiEndpoint -Header $headers -ContentType "application/json" -Body $prRequestBody
105+
'Started PR with dependencies update...' | Write-Host -NoNewline
106+
'OK' | Write-Host -ForegroundColor Green
107+
}
108+
catch
109+
{
110+
$result = $_.Exception.Response.GetResponseStream()
111+
$reader = New-Object System.IO.StreamReader($result)
112+
$reader.BaseStream.Position = 0
113+
$reader.DiscardBufferedData()
114+
$responseBody = $reader.ReadToEnd();
115+
116+
throw "Error creating PR: $responseBody"
117+
}

0 commit comments

Comments
 (0)