-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathDetermineProjectsToBuild.Action.ps1
110 lines (95 loc) · 5.55 KB
/
DetermineProjectsToBuild.Action.ps1
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
Param(
[Parameter(HelpMessage = "The folder to scan for projects to build", Mandatory = $true)]
[string] $baseFolder,
[Parameter(HelpMessage = "The maximum depth to build the dependency tree", Mandatory = $false)]
[int] $maxBuildDepth = 0,
[Parameter(HelpMessage = "The GitHub token to use to fetch the modified files", Mandatory = $true)]
[string] $token
)
#region Action: Setup
. (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve)
DownloadAndImportBcContainerHelper -baseFolder $baseFolder
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "DetermineProjectsToBuild.psm1" -Resolve) -DisableNameChecking
#endregion
$settings = $env:Settings | ConvertFrom-Json
$targetBranch = $env:GITHUB_REF_NAME
if ($ENV:GITHUB_EVENT_NAME -eq 'pull_request') {
$targetBranch = $env:GITHUB_BASE_REF
}
#region Action: Determine projects to build
Write-Host "$($ENV:GITHUB_EVENT_NAME) on $targetBranch"
$buildAllProjects, $publishSkippedProjects = Get-BuildAllProjectsBasedOnEventAndSettings -ghEventName $ENV:GITHUB_EVENT_NAME -settings $settings
$modifiedFiles = @()
$baselineWorkflowRunId = 0 #default to 0, which means no baseline workflow run ID is set
$baselineWorkflowSHA = ''
if(-not $buildAllProjects) {
Write-Host "::group::Determine Baseline Workflow ID"
$baselineWorkflowRunId,$baselineWorkflowSHA = FindLatestSuccessfulCICDRun -repository $env:GITHUB_REPOSITORY -branch $targetBranch -token $token -retention $settings.incrementalBuilds.retentionDays
Write-Host "::endgroup::"
if (-not ($baselineWorkflowSHA -and $baselineWorkflowRunId -ne '0')) {
OutputNotice -message "Unable to locate a successful CI/CD run for branch $targetBranch, newer than $($settings.incrementalBuilds.retentionDays) days, building all projects"
$buildAllProjects = $true
}
else {
Write-Host "::group::Get Modified Files"
try {
$modifiedFiles = Get-ModifiedFiles -baselineSHA $baselineWorkflowSHA
OutputMessageAndArray -message "Modified files" -arrayOfStrings $modifiedFiles
}
catch {
OutputWarning -message "Failed to calculate modified files since $baselineWorkflowSHA, the Error was $($_.Exception.Message). Building all projects"
$buildAllProjects = $true
}
Write-Host "::endgroup::"
}
}
if (-not $buildAllProjects) {
Write-Host "::group::Determine Incremental Build"
$buildAllProjects = Get-BuildAllProjects -modifiedFiles $modifiedFiles -baseFolder $baseFolder
Write-Host "::endgroup::"
}
# 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
# buildAllProjects is set to true if we are to build all projects
# 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)
Write-Host "::group::Get Projects To Build"
$allProjects, $modifiedProjects, $projectsToBuild, $projectDependencies, $buildOrder = Get-ProjectsToBuild -baseFolder $baseFolder -buildAllProjects ($buildAllProjects -or $publishSkippedProjects) -modifiedFiles $modifiedFiles -maxBuildDepth $maxBuildDepth
if ($buildAllProjects) {
$skippedProjects = @()
}
else {
$skippedProjects = @($allProjects | Where-Object { $_ -notin $modifiedProjects })
}
Write-Host "::endgroup::"
#endregion
#region Action: Output
$skippedProjectsJson = ConvertTo-Json -InputObject $skippedProjects -Depth 99 -Compress
$projectsJson = ConvertTo-Json $projectsToBuild -Depth 99 -Compress
$projectDependenciesJson = ConvertTo-Json $projectDependencies -Depth 99 -Compress
$buildOrderJson = ConvertTo-Json $buildOrder -Depth 99 -Compress
$additionalDataForTelemetry = [System.Collections.Generic.Dictionary[[System.String], [System.String]]]::new()
$additionalDataForTelemetry.Add("Mode", $settings.incrementalBuilds.Mode)
$additionalDataForTelemetry.Add("Event", $ENV:GITHUB_EVENT_NAME)
$additionalDataForTelemetry.Add("Projects", $allProjects.Count)
$additionalDataForTelemetry.Add("ModifiedProjects", $modifiedProjects.Count)
$additionalDataForTelemetry.Add("ProjectsToBuild", $projectsToBuild.Count)
Trace-Information -Message "Incremental builds (projects)" -AdditionalData $additionalDataForTelemetry
# Add annotation for last known good build
if ($baselineWorkflowRunId) {
Write-Host "::notice::Last known good build: https://github.com/$($env:GITHUB_REPOSITORY)/actions/runs/$baselineWorkflowRunId"
}
# Set output variables
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "SkippedProjectsJson=$skippedProjectsJson"
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "ProjectsJson=$projectsJson"
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "ProjectDependenciesJson=$projectDependenciesJson"
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "BuildOrderJson=$buildOrderJson"
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "BuildAllProjects=$([int] $buildAllProjects)"
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "BaselineWorkflowRunId=$baselineWorkflowRunId"
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "BaselineWorkflowSHA=$baselineWorkflowSHA"
Write-Host "SkippedProjectsJson=$skippedProjectsJson"
Write-Host "ProjectsJson=$projectsJson"
Write-Host "ProjectDependenciesJson=$projectDependenciesJson"
Write-Host "BuildOrderJson=$buildOrderJson"
Write-Host "BuildAllProjects=$buildAllProjects"
Write-Host "BaselineWorkflowRunId=$baselineWorkflowRunId"
Write-Host "BaselineWorkflowSHA=$baselineWorkflowSHA"
#endregion