Skip to content

Commit 0e080a3

Browse files
authored
Extended debug logging (#1729)
1 parent b763a6b commit 0e080a3

File tree

10 files changed

+244
-64
lines changed

10 files changed

+244
-64
lines changed

Actions/.Modules/DebugLogHelper.psm1

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
2+
if ($env:GITHUB_ACTIONS -eq "true") {
3+
$runningLocal = $false
4+
} else {
5+
$runningLocal = $true
6+
}
7+
8+
# Colors
9+
$colorCodeRed = '31'
10+
$colorCodeGreen = '32'
11+
$colorCodeYellow = '33'
12+
$colorCodeBlue = '34'
13+
$colorCodeMagenta = '35'
14+
$colorCodeCyan = '36'
15+
16+
<#
17+
.SYNOPSIS
18+
Writes debug information about the function call and its parameters if extended debug logging is enabled or running locally.
19+
.DESCRIPTION
20+
Writes debug information about the function call and its parameters to the console if extended debug logging is enabled or running locally.
21+
Automatically retrieves the caller's name and arguments from the call stack.
22+
#>
23+
function OutputDebugFunctionCall {
24+
try {
25+
$caller = (Get-PSCallStack)[1]
26+
$callerName = $caller.Command
27+
$callerParameters = $caller.InvocationInfo.BoundParameters
28+
29+
OutputDebug "Function '$callerName' called with parameters:"
30+
if ($callerParameters.Count -eq 0) {
31+
OutputDebug "None"
32+
}
33+
foreach ($key in $callerParameters.Keys) {
34+
$val = $callerParameters[$key]
35+
OutputDebug "-$($key): $val"
36+
}
37+
} catch {
38+
OutputDebug "Unable to retrieve function information from call stack."
39+
}
40+
}
41+
42+
<#
43+
.SYNOPSIS
44+
Starts a console log group.
45+
.DESCRIPTION
46+
Starts a console log group. All subsequent log messages will be grouped under this message until OutputGroupEnd is called.
47+
If running locally, it writes a simple message to the console. If running in GitHub Actions, it uses the `::group::` command to create a collapsible group in the log.
48+
.PARAMETER Message
49+
Name/Title of the group.
50+
#>
51+
function OutputGroupStart {
52+
param (
53+
[Parameter(Mandatory = $true)]
54+
[string] $Message
55+
)
56+
57+
if ($runningLocal) {
58+
Write-Host "==== Group start: $Message ===="
59+
} else {
60+
Write-Host "::group::$Message"
61+
}
62+
}
63+
64+
<#
65+
.SYNOPSIS
66+
Ends a console log group.
67+
.DESCRIPTION
68+
Ends a console log group started with OutputGroupStart. All subsequent log messages will be outside of this group.
69+
#>
70+
function OutputGroupEnd {
71+
if ($runningLocal) {
72+
Write-Host "==== Group end ===="
73+
} else {
74+
Write-Host "::endgroup::"
75+
}
76+
}
77+
78+
<#
79+
.SYNOPSIS
80+
Writes to console with optional color.
81+
.DESCRIPTION
82+
Writes a message to the console with an optional color. If no color is specified, the message is written in the default console color.
83+
.PARAMETER Message
84+
Message to be written to console.
85+
.PARAMETER Color
86+
Optional color for the message. Valid values are 'Red', 'Green', 'Yellow', 'Blue', 'Magenta', 'Cyan'.
87+
#>
88+
function OutputColor {
89+
param (
90+
[Parameter(Mandatory = $true)]
91+
[string] $Message,
92+
[Parameter(Mandatory = $false)]
93+
[ValidateSet('Red', 'Green', 'Yellow', 'Blue', 'Magenta', 'Cyan')]
94+
[string] $Color
95+
)
96+
97+
if ($Color) {
98+
$colorCode = 0
99+
switch ($Color) {
100+
'Red' { $colorCode = $colorCodeRed }
101+
'Green' { $colorCode = $colorCodeGreen }
102+
'Yellow' { $colorCode = $colorCodeYellow }
103+
'Blue' { $colorCode = $colorCodeBlue }
104+
'Magenta' { $colorCode = $colorCodeMagenta }
105+
'Cyan' { $colorCode = $colorCodeCyan }
106+
}
107+
# char 27 is the escape character for ANSI codes which works in both PS 5 and 7.
108+
Write-Host "$([char] 27)[${colorCode}m$Message$([char] 27)[0m"
109+
} else {
110+
Write-Host $Message
111+
}
112+
}
113+
114+
<#
115+
.SYNOPSIS
116+
Write an error message to the console.
117+
.DESCRIPTION
118+
Writes an error message to the console. Throws an exception if running locally, otherwise formats the message for GitHub Actions.
119+
.PARAMETER Message
120+
Message to be written to console.
121+
#>
122+
function OutputError {
123+
Param(
124+
[string] $message
125+
)
126+
127+
if ($runningLocal) {
128+
throw $message
129+
}
130+
else {
131+
Write-Host "::Error::$($message.Replace("`r",'').Replace("`n",' '))"
132+
$host.SetShouldExit(1)
133+
}
134+
}
135+
136+
<#
137+
.SYNOPSIS
138+
Write a warning message to the console.
139+
.DESCRIPTION
140+
Writes a warning message to the console. Uses Write-Warning if running locally, otherwise formats the message for GitHub Actions.
141+
.PARAMETER Message
142+
Message to be written to console.
143+
#>
144+
function OutputWarning {
145+
Param(
146+
[string] $message
147+
)
148+
149+
if ($runningLocal) {
150+
Write-Warning $message
151+
}
152+
else {
153+
Write-Host "::Warning::$message"
154+
}
155+
}
156+
157+
<#
158+
.SYNOPSIS
159+
Write a notice message to the console.
160+
.DESCRIPTION
161+
Writes a notice message to the console. Uses regular Write-Host if running locally, otherwise formats the message for GitHub Actions.
162+
.PARAMETER Message
163+
Message to be written to console.
164+
#>
165+
function OutputNotice {
166+
Param(
167+
[string] $message
168+
)
169+
170+
if ($runningLocal) {
171+
Write-Host $message
172+
}
173+
else {
174+
Write-Host "::Notice::$message"
175+
}
176+
}
177+
178+
<#
179+
.SYNOPSIS
180+
Mask a value in the log.
181+
.DESCRIPTION
182+
Masks a value in the log to prevent sensitive information from being displayed. If running locally, it writes the masked value to the console.
183+
.PARAMETER Value
184+
The value to be masked in the log.
185+
#>
186+
function MaskValueInLog {
187+
Param(
188+
[string] $value
189+
)
190+
191+
if (!$runningLocal) {
192+
Write-Host "`r::add-mask::$value"
193+
}
194+
}
195+
196+
<#
197+
.SYNOPSIS
198+
Write a debug message to the console.
199+
.DESCRIPTION
200+
Writes a debug message to the console. Uses Write-Debug if running locally, otherwise formats the message for GitHub Actions.
201+
.PARAMETER Message
202+
Message to be written to console.
203+
#>
204+
function OutputDebug {
205+
Param(
206+
[string] $message
207+
)
208+
209+
if ($runningLocal) {
210+
Write-Debug $message
211+
}
212+
else {
213+
Write-Host "::Debug::[AL-Go]$message"
214+
}
215+
}
216+
217+
Export-ModuleMember -Function OutputColor, OutputDebugFunctionCall, OutputGroupStart, OutputGroupEnd, OutputError, OutputWarning, OutputNotice, MaskValueInLog, OutputDebug
218+
Export-ModuleMember -Variable debugLoggingEnabled

Actions/AL-Go-Helper.ps1

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Param(
44

55
$gitHubHelperPath = Join-Path $PSScriptRoot 'Github-Helper.psm1'
66
$readSettingsModule = Join-Path $PSScriptRoot '.Modules/ReadSettings.psm1'
7+
$debugLoggingModule = Join-Path $PSScriptRoot '.Modules/DebugLogHelper.psm1'
78
if (Test-Path $gitHubHelperPath) {
89
Import-Module $gitHubHelperPath
910
# If we are adding more dependencies here, then localDevEnv and cloudDevEnv needs to be updated
@@ -13,6 +14,10 @@ if (Test-Path $readSettingsModule) {
1314
Import-Module $readSettingsModule
1415
}
1516

17+
if (Test-Path $debugLoggingModule) {
18+
Import-Module $debugLoggingModule
19+
}
20+
1621
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
1722

1823
$ALGoFolderName = '.AL-Go'
@@ -22,7 +27,6 @@ $RepoSettingsFile = Join-Path '.github' 'AL-Go-Settings.json'
2227
$defaultCICDPushBranches = @( 'main', 'release/*', 'feature/*' )
2328
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'defaultCICDPullRequestBranches', Justification = 'False positive.')]
2429
$defaultCICDPullRequestBranches = @( 'main' )
25-
$runningLocal = $local.IsPresent
2630
$defaultBcContainerHelperVersion = "preview" # Must be double quotes. Will be replaced by BcContainerHelperVersion if necessary in the deploy step - ex. "https://github.com/organization/navcontainerhelper/archive/refs/heads/branch.zip"
2731
$notSecretProperties = @("Scopes","TenantId","BlobName","ContainerName","StorageAccountName","ServerUrl","ppUserName","GitHubAppClientId","EnvironmentName")
2832

@@ -183,69 +187,6 @@ function ConvertTo-HashTable() {
183187
}
184188
}
185189

186-
function OutputError {
187-
Param(
188-
[string] $message
189-
)
190-
191-
if ($runningLocal) {
192-
throw $message
193-
}
194-
else {
195-
Write-Host "::Error::$($message.Replace("`r",'').Replace("`n",' '))"
196-
$host.SetShouldExit(1)
197-
}
198-
}
199-
200-
function OutputWarning {
201-
Param(
202-
[string] $message
203-
)
204-
205-
if ($runningLocal) {
206-
Write-Host -ForegroundColor Yellow "WARNING: $message"
207-
}
208-
else {
209-
Write-Host "::Warning::$message"
210-
}
211-
}
212-
213-
function OutputNotice {
214-
Param(
215-
[string] $message
216-
)
217-
218-
if ($runningLocal) {
219-
Write-Host $message
220-
}
221-
else {
222-
Write-Host "::Notice::$message"
223-
}
224-
}
225-
226-
function MaskValueInLog {
227-
Param(
228-
[string] $value
229-
)
230-
231-
if (!$runningLocal) {
232-
Write-Host "`r::add-mask::$value"
233-
}
234-
}
235-
236-
function OutputDebug {
237-
Param(
238-
[string] $message
239-
)
240-
241-
if ($runningLocal) {
242-
Write-Host $message
243-
}
244-
else {
245-
Write-Host "::Debug::$message"
246-
}
247-
}
248-
249190
function GetUniqueFolderName {
250191
Param(
251192
[string] $baseFolder,

Actions/DetermineDeploymentEnvironments/DetermineDeploymentEnvironments.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
[ValidateSet('CD','Publish','All')]
66
[string] $type
77
)
8+
OutputDebugFunctionCall
89

910
function IsGitHubPagesAvailable() {
11+
OutputDebugFunctionCall
1012
$headers = GetHeaders -token $env:GITHUB_TOKEN
1113
$url = "$($ENV:GITHUB_API_URL)/repos/$($ENV:GITHUB_REPOSITORY)/pages"
14+
OutputDebug "Url: $url"
1215
try {
1316
Write-Host "Requesting GitHub Pages settings from GitHub"
1417
$ghPages = (InvokeWebRequest -Headers $headers -Uri $url).Content | ConvertFrom-Json
@@ -20,8 +23,10 @@ function IsGitHubPagesAvailable() {
2023
}
2124

2225
function GetGitHubEnvironments() {
26+
OutputDebugFunctionCall
2327
$headers = GetHeaders -token $env:GITHUB_TOKEN
2428
$url = "$($ENV:GITHUB_API_URL)/repos/$($ENV:GITHUB_REPOSITORY)/environments"
29+
OutputDebug "Url: $url"
2530
try {
2631
Write-Host "Requesting environments from GitHub"
2732
$ghEnvironments = @(((InvokeWebRequest -Headers $headers -Uri $url).Content | ConvertFrom-Json).environments)
@@ -34,6 +39,7 @@ function GetGitHubEnvironments() {
3439
}
3540

3641
function Get-BranchesFromPolicy($ghEnvironment) {
42+
OutputDebugFunctionCall
3743
if ($ghEnvironment) {
3844
# Environment is defined in GitHub - check protection rules
3945
$headers = GetHeaders -token $env:GITHUB_TOKEN

Actions/Invoke-AlGoAction.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $progressPreference = "SilentlyContinue"
1414
Set-StrictMode -Version 2.0
1515

1616
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "TelemetryHelper.psm1" -Resolve)
17+
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath ".Modules/DebugLogHelper.psm1" -Resolve)
1718

1819
try {
1920
$startTime = Get-Date

RELEASENOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ AL-Go now offers a dataexplorer dashboard to get started with AL-Go telemetry. A
99
- Issue 1722 Check if apps are already installed on a higher version before deploying
1010
- Issue 1774 Increment Version Number with +0.1 can increment some version numbers twice
1111

12+
### Additional debug logging functionality
13+
14+
We have improved how logging is handled in AL-Go, and now make better use of GitHub built-in extended debug logging functionality. Extended debug logging can be enabled when re-running actions by clicking the 'Enable debug logging' checkbox in the pop-up window. This can be done both for jobs that failed and jobs that succeeded, but did not produce the correct result.
15+
1216
### Add custom jobs to AL-Go workflows
1317

1418
It is now possible to add custom jobs to AL-Go workflows. The Custom Job needs to be named `CustomJob<something>` and should be placed after all other jobs in the .yaml file. The order of which jobs are executed is determined by the Needs statements. Your custom job will be executed after all jobs specified in the Needs clause in your job and if you need the job to be executed before other jobs, you should add the job name in the Needs clause of that job. See [https://aka.ms/algosettings#customjobs](https://aka.ms/algosettings#customjobs) for details.

Templates/AppSource App/.AL-Go/cloudDevEnv.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ $tmpFolder = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().To
5353
New-Item -Path $tmpFolder -ItemType Directory -Force | Out-Null
5454
$GitHubHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Github-Helper.psm1' -folder $tmpFolder -notifyAuthenticatedAttempt
5555
$ReadSettingsModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/.Modules/ReadSettings.psm1' -folder $tmpFolder
56+
$debugLoggingModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/.Modules/DebugLogHelper.psm1' -folder $tmpFolder
5657
$ALGoHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/AL-Go-Helper.ps1' -folder $tmpFolder
5758
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/settings.schema.json' -folder $tmpFolder | Out-Null
5859
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Packages.json' -folder $tmpFolder | Out-Null
5960

6061
Import-Module $GitHubHelperPath
6162
Import-Module $ReadSettingsModule
63+
Import-Module $debugLoggingModule
6264
. $ALGoHelperPath -local
6365

6466
$baseFolder = GetBaseFolder -folder $PSScriptRoot

Templates/AppSource App/.AL-Go/localDevEnv.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ $tmpFolder = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().To
5757
New-Item -Path $tmpFolder -ItemType Directory -Force | Out-Null
5858
$GitHubHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Github-Helper.psm1' -folder $tmpFolder -notifyAuthenticatedAttempt
5959
$ReadSettingsModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/.Modules/ReadSettings.psm1' -folder $tmpFolder
60+
$debugLoggingModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/.Modules/DebugLogHelper.psm1' -folder $tmpFolder
6061
$ALGoHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/AL-Go-Helper.ps1' -folder $tmpFolder
6162
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/settings.schema.json' -folder $tmpFolder | Out-Null
6263
DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/main/Packages.json' -folder $tmpFolder | Out-Null
6364

6465
Import-Module $GitHubHelperPath
6566
Import-Module $ReadSettingsModule
67+
Import-Module $debugLoggingModule
6668
. $ALGoHelperPath -local
6769

6870
$baseFolder = GetBaseFolder -folder $PSScriptRoot

0 commit comments

Comments
 (0)