Skip to content

Commit 9c62ec6

Browse files
authored
Fix incremental build problems (#1488)
It can be hard to discover the reason why a full build was triggered deep inside the log. This PR adds a notice to the build when a full build is triggered, with the reason behind it. Fixes #1487 Additionally, a Pull Request, which should trigger a full build (due to files changed) wouldn't trigger the change due to a parameter error. Thirdly - in some cases, we would attempt to do a git diff with an empty sha. Lastly - skip the build cleanup step if no build step was ever run (saves ~10 seconds) --------- Co-authored-by: freddydk <[email protected]>
1 parent cdc3108 commit 9c62ec6

File tree

6 files changed

+71
-33
lines changed

6 files changed

+71
-33
lines changed

Actions/DetermineProjectsToBuild/DetermineProjectsToBuild.Action.ps1

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,47 @@ if ($ENV:GITHUB_EVENT_NAME -eq 'pull_request') {
2121
$targetBranch = $env:GITHUB_BASE_REF
2222
}
2323

24+
#region Action: Determine projects to build
2425
Write-Host "$($ENV:GITHUB_EVENT_NAME) on $targetBranch"
2526
$buildAllProjects, $publishSkippedProjects = Get-BuildAllProjectsBasedOnEventAndSettings -ghEventName $ENV:GITHUB_EVENT_NAME -settings $settings
2627

28+
$modifiedFiles = @()
2729
$baselineWorkflowRunId = 0 #default to 0, which means no baseline workflow run ID is set
2830
$baselineWorkflowSHA = ''
2931
if(-not $buildAllProjects) {
3032
Write-Host "::group::Determine Baseline Workflow ID"
3133
$baselineWorkflowRunId,$baselineWorkflowSHA = FindLatestSuccessfulCICDRun -repository $env:GITHUB_REPOSITORY -branch $targetBranch -token $token -retention $settings.incrementalBuilds.retentionDays
3234
Write-Host "::endgroup::"
33-
}
3435

35-
#region Action: Determine projects to build
36-
Write-Host "::group::Get Modified Files"
37-
try {
38-
$modifiedFiles = @(Get-ModifiedFiles -baselineSHA $baselineWorkflowSHA)
39-
OutputMessageAndArray -message "Modified files" -arrayOfStrings $modifiedFiles
40-
}
41-
catch {
42-
Write-Host "Failed to calculate modified files, building all projects"
43-
$buildAllProjects = $true
44-
$modifiedFiles = @()
36+
if (-not ($baselineWorkflowSHA -and $baselineWorkflowRunId -ne '0')) {
37+
OutputNotice -message "Unable to locate a successful CI/CD run for branch $targetBranch, newer than $($settings.incrementalBuilds.retentionDays) days, building all projects"
38+
$buildAllProjects = $true
39+
}
40+
else {
41+
Write-Host "::group::Get Modified Files"
42+
try {
43+
$modifiedFiles = Get-ModifiedFiles -baselineSHA $baselineWorkflowSHA
44+
OutputMessageAndArray -message "Modified files" -arrayOfStrings $modifiedFiles
45+
}
46+
catch {
47+
OutputWarning -message "Failed to calculate modified files since $baselineWorkflowSHA, the Error was $($_.Exception.Message). Building all projects"
48+
$buildAllProjects = $true
49+
}
50+
Write-Host "::endgroup::"
51+
}
4552
}
46-
Write-Host "::endgroup::"
4753

48-
Write-Host "::group::Determine Incremental Build"
49-
$buildAllProjects = $buildAllProjects -or (Get-BuildAllProjects -modifiedFiles $modifiedFiles -baseFolder $baseFolder)
50-
Write-Host "::endgroup::"
54+
if (-not $buildAllProjects) {
55+
Write-Host "::group::Determine Incremental Build"
56+
$buildAllProjects = Get-BuildAllProjects -modifiedFiles $modifiedFiles -baseFolder $baseFolder
57+
Write-Host "::endgroup::"
58+
}
5159

5260
# If we are to publish artifacts for skipped projects later, we include the full project list and in the build step, just avoid building the skipped projects
61+
# buildAllProjects is set to true if we are to build all projects
62+
# publishSkippedProjects is set to true if we are to publish artifacts for skipped projects (meaning we are still going through the build process for all projects, just not building)
5363
Write-Host "::group::Get Projects To Build"
54-
$allProjects, $modifiedProjects, $projectsToBuild, $projectDependencies, $buildOrder = Get-ProjectsToBuild -baseFolder $baseFolder -buildAllProjects $publishSkippedProjects -modifiedFiles $modifiedFiles -maxBuildDepth $maxBuildDepth
64+
$allProjects, $modifiedProjects, $projectsToBuild, $projectDependencies, $buildOrder = Get-ProjectsToBuild -baseFolder $baseFolder -buildAllProjects ($buildAllProjects -or $publishSkippedProjects) -modifiedFiles $modifiedFiles -maxBuildDepth $maxBuildDepth
5565
if ($buildAllProjects) {
5666
$skippedProjects = @()
5767
}

Actions/DetermineProjectsToBuild/DetermineProjectsToBuild.psm1

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,24 +282,24 @@ function Get-BuildAllProjectsBasedOnEventAndSettings {
282282
.Description
283283
Determines whether a full build is required.
284284
A full build is required if:
285-
- No files were modified
286285
- The modified files contain a file that matches one of the fullBuildPatterns
287286
#>
288-
function Get-BuildAllProjects {
287+
function IsFullBuildRequired {
289288
param(
290289
[Parameter(HelpMessage = "The base folder", Mandatory = $true)]
291290
[string] $baseFolder,
292291
[Parameter(HelpMessage = "The modified files", Mandatory = $false)]
293292
[string[]] $modifiedFiles = @(),
294293
[Parameter(HelpMessage = "Full build patterns", Mandatory = $false)]
295-
[string[]] $fullBuildPatterns = @()
294+
[string[]] $fullBuildPatterns = @(),
295+
[string] $noticeMessage = ''
296296
)
297297

298298
$settings = $env:Settings | ConvertFrom-Json
299299

300300
if (!$modifiedFiles) {
301-
Write-Host "No files modified, building everything"
302-
return $true
301+
Write-Host "No modified files"
302+
return $false
303303
}
304304

305305
$fullBuildPatterns += @(Join-Path '.github' '*.json')
@@ -315,25 +315,49 @@ function Get-BuildAllProjects {
315315
$fullBuildFolder = Join-Path $baseFolder $fullBuildFolder
316316

317317
if ($modifiedFiles -like $fullBuildFolder) {
318-
Write-Host "Changes to $fullBuildFolder, building everything"
318+
if ($noticeMessage) {
319+
Write-Host "::notice::Changes to $fullBuildFolder detected, $noticeMessage"
320+
}
321+
else {
322+
Write-Host "Changes to $fullBuildFolder detected"
323+
}
319324
return $true
320325
}
321326
}
322-
323-
Write-Host "No changes to fullBuildPatterns, not building everything"
324-
327+
Write-Host "No changes to full build patterns detected"
325328
return $false
326329
}
327330

328331
<#
329332
.Synopsis
330-
Determines whether all apps in a project should be built
333+
Determines whether all projects in a repository should be built
331334
.Outputs
332335
A boolean indicating whether a full build is required.
333336
.Description
334337
Determines whether a full build is required.
335338
A full build is required if:
336-
- Get-BuildAllProjects returns true
339+
- The modified files contain a file that matches one of the fullBuildPatterns
340+
#>
341+
function Get-BuildAllProjects {
342+
param(
343+
[Parameter(HelpMessage = "The base folder", Mandatory = $true)]
344+
[string] $baseFolder,
345+
[Parameter(HelpMessage = "The modified files", Mandatory = $false)]
346+
[string[]] $modifiedFiles = @()
347+
)
348+
349+
return (IsFullBuildRequired -baseFolder $baseFolder -modifiedFiles $modifiedFiles -noticeMessage "building everything")
350+
}
351+
352+
<#
353+
.Synopsis
354+
Determines whether all apps in a project should be built
355+
.Outputs
356+
A boolean indicating whether all apps in a project should be built.
357+
.Description
358+
Determines whether all apps in a project should be built.
359+
All apps should be built if:
360+
- The modified files contain a file that matches one of the fullBuildPatterns
337361
- The .AL-Go/settings.json file has been modified
338362
#>
339363
function Get-BuildAllApps {
@@ -346,13 +370,17 @@ function Get-BuildAllApps {
346370
[string[]] $modifiedFiles = @()
347371
)
348372

373+
if (IsFullBuildRequired -baseFolder $baseFolder -modifiedFiles $modifiedFiles) {
374+
# Notice already given in Initialize
375+
return $true
376+
}
349377
if ($project) {
350378
$ALGoSettingsFile = @(Join-Path $project '.AL-Go/settings.json')
351379
}
352380
else {
353381
$ALGoSettingsFile = @('.AL-Go/settings.json')
354382
}
355-
return (Get-BuildAllProjects -baseFolder $baseFolder -modifiedFiles $modifiedFiles -fullBuildPatterns @($ALGoSettingsFile))
383+
return (IsFullBuildRequired -baseFolder $baseFolder -modifiedFiles $modifiedFiles -fullBuildPatterns @($ALGoSettingsFile) -noticeMessage "building all apps")
356384
}
357385

358386
<#

Actions/RunPipeline/RunPipeline.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ try {
136136
$buildAll = Get-BuildAllApps -baseFolder $baseFolder -project $project -modifiedFiles $modifiedFiles
137137
}
138138
catch {
139-
OutputWarning -message "Failed to calculate modified files since $baselineWorkflowSHA, building all apps"
139+
OutputNotice -message "Failed to calculate modified files since $baselineWorkflowSHA, building all apps"
140140
$buildAll = $true
141141
}
142142
if (!$buildAll) {

Templates/AppSource App/.github/workflows/_BuildALGoProject.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ jobs:
280280
project: ${{ inputs.project }}
281281

282282
- name: Cleanup
283-
if: always()
283+
if: always() && steps.DetermineBuildProject.outputs.BuildIt == 'True'
284284
uses: microsoft/AL-Go-Actions/PipelineCleanup@main
285285
with:
286286
shell: ${{ inputs.shell }}

Templates/Per Tenant Extension/.github/workflows/_BuildALGoProject.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ jobs:
280280
project: ${{ inputs.project }}
281281

282282
- name: Cleanup
283-
if: always()
283+
if: always() && steps.DetermineBuildProject.outputs.BuildIt == 'True'
284284
uses: microsoft/AL-Go-Actions/PipelineCleanup@main
285285
with:
286286
shell: ${{ inputs.shell }}

Tests/DetermineProjectsToBuild.Test.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,13 @@ Describe "Get-BuildAllProjects" {
762762
$baseFolder = (New-Item -ItemType Directory -Path (Join-Path $([System.IO.Path]::GetTempPath()) $([System.IO.Path]::GetRandomFileName()))).FullName
763763
}
764764

765-
It ('returns true if there are no modified filed') {
765+
It ('returns false if there are no modified files') {
766766
# Add AL-Go settings
767767
$alGoSettings = @{ fullBuildPatterns = @() }
768768
$env:Settings = ConvertTo-Json $alGoSettings -Depth 99 -Compress
769769

770770
$buildAllProjects = Get-BuildAllProjects -baseFolder $baseFolder
771-
$buildAllProjects | Should -Be $true
771+
$buildAllProjects | Should -Be $false
772772
}
773773

774774
It ('returns true if any of the modified files matches any of the patterns in fullBuildPatterns setting') {

0 commit comments

Comments
 (0)