Skip to content

Commit 4433eef

Browse files
Rework inputs handling of update workflow for trigger 'workflow_call' (#2154)
### ❔What, Why & How #### What The `workflow_call` inputs in the update workflow are handled incorrectly. They must be accessed as `inputs.*`, not `github.event.inputs.*`. Also, `github.event_name` will always reflect the trigger of the *calling* workflow, not the reusable workflow itself. In my tests of the original contribution that added the trigger to the update workflow, my use case tests passed by coincidence because the parent workflow had an input named `directCommit` itself and all other inputs used their default values. #### Why When the update workflow is invoked as a reusable workflow, it should use the values defined in its own inputs. These should be treated the same as when the workflow is triggered by `workflow_dispatch` (for example, when determining branches). #### How - Modified the action `GetWorkflowMultiRunBranches`: - Added a new optional input `workflowEventName` - Default: `github.event_name` - Purpose: override the event name used to determine which branches to include - Event `workflow_call` resuls in the same output as event `workflow_dispatch` - Modified the update workflow: - Replaced all uses of `github.event.inputs.*` with `inputs.*` - Added a new required workflow_call input `caller` (string): - Marks that the update workflow was called from another workflow - Added a new environment variable `WorkflowEventName`: - Set to `github.ref_name` or to `workflow_call` when the `caller` input is present - Passed to input `workflowEventName` of action `GetWorkflowMultiRunBranches` - Used to determine the commit options Related to original discussion #1855 and PR #2031 ### ✅ Checklist - [x] Add tests (E2E, unit tests) - [x] Update RELEASENOTES.md
1 parent 646025c commit 4433eef

8 files changed

Lines changed: 135 additions & 20 deletions

File tree

Actions/GetWorkflowMultiRunBranches/GetWorkflowMultiRunBranches.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
param(
2+
[Parameter(Mandatory = $false, HelpMessage = "The GitHub event name that triggered the workflow.")]
3+
[string] $workflowEventName = $env:GITHUB_EVENT_NAME,
24
[Parameter(Mandatory = $false, HelpMessage = "Comma-separated value of branch name patterns to include if they exist. If not specified, only the current branch is returned. Wildcards are supported.")]
35
[string] $includeBranches
46
)
57

68
$gitHubHelperPath = Join-Path $PSScriptRoot '../Github-Helper.psm1' -Resolve
79
Import-Module $gitHubHelperPath -DisableNameChecking
810

9-
switch ($env:GITHUB_EVENT_NAME) {
11+
switch ($workflowEventName) {
1012
'schedule' {
1113
Write-Host "Event is schedule: getting branches from settings"
1214
$settings = ConvertFrom-Json $env:Settings
@@ -20,8 +22,8 @@ switch ($env:GITHUB_EVENT_NAME) {
2022
$branchPatterns = @()
2123
}
2224
}
23-
'workflow_dispatch' {
24-
Write-Host "Event is workflow_dispatch: getting branches from input"
25+
{ $_ -in 'workflow_dispatch', 'workflow_call' } {
26+
Write-Host "Event is $($_): getting branches from input"
2527
$branchPatterns = @($includeBranches.Split(',') | ForEach-Object { $_.Trim() })
2628
}
2729
}

Actions/GetWorkflowMultiRunBranches/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ If the workflow is run on a schedule, the branches are determined based on the `
1717
| Name | Required | Description | Default value |
1818
| :-- | :-: | :-- | :-- |
1919
| shell | false | The shell (powershell or pwsh) in which the PowerShell script in this action should run | powershell |
20+
| workflowEventName | false | The GitHub event name that triggered the workflow. *(override for reusable workflows)* | github.event_name |
2021
| includeBranches | false | Comma-separated value of branch name patterns to include if they exist. If not specified, only the current branch is returned. Wildcards are supported. |''|
2122

2223
## OUTPUT

Actions/GetWorkflowMultiRunBranches/action.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ inputs:
55
description: Shell in which you want to run the action (powershell or pwsh)
66
required: false
77
default: powershell
8+
workflowEventName:
9+
description: The GitHub event name that triggered the workflow.
10+
required: false
11+
default: ${{ github.event_name }}
812
includeBranches:
913
description: Comma-separated value of branch name patterns to include if they exist. If not specified, only the current branch is returned. Wildcards are supported.
1014
required: false
@@ -20,10 +24,11 @@ runs:
2024
shell: ${{ inputs.shell }}
2125
id: GetWorkflowMultiRunBranches
2226
env:
27+
_workflowEventName: ${{ inputs.workflowEventName }}
2328
_includeBranches: ${{ inputs.includeBranches }}
2429
run: |
2530
${{ github.action_path }}/../Invoke-AlGoAction.ps1 -ActionName "GetWorkflowMultiRunBranches" -Action {
26-
${{ github.action_path }}/GetWorkflowMultiRunBranches.ps1 -includeBranches $env:_includeBranches
31+
${{ github.action_path }}/GetWorkflowMultiRunBranches.ps1 -workflowEventName $env:_workflowEventName -includeBranches $env:_includeBranches
2732
}
2833
branding:
2934
icon: terminal

RELEASENOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The new setting `postponeProjectInBuildOrder` allows you to delay long running j
5050
- Issue 2084 Multiple artifacts failure if you re-run failed jobs after flaky tests
5151
- Issue 2085 Projects that doesn't contain both Apps and TestApps are wrongly seen as not built.
5252
- Issue 2086 Postpone jobs, which doesn't have any dependents to the end of the build order.
53+
- Rework input handling of workflow 'Update AL-Go System Files' for trigger 'workflow_call'
5354

5455
### New Settings
5556

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ on:
2121
default: ''
2222
workflow_call:
2323
inputs:
24+
caller:
25+
description: Name of the calling workflow (use github.workflow as value when calling)
26+
type: string
27+
required: true
2428
templateUrl:
2529
description: Template Repository URL (current is {TEMPLATEURL})
2630
type: string
@@ -50,6 +54,7 @@ defaults:
5054
shell: powershell
5155

5256
env:
57+
WorkflowEventName: ${{ inputs.caller && 'workflow_call' || github.event_name }}
5358
ALGoOrgSettings: ${{ vars.ALGoOrgSettings }}
5459
ALGoRepoSettings: ${{ vars.ALGoRepoSettings }}
5560

@@ -76,12 +81,13 @@ jobs:
7681
uses: microsoft/AL-Go-Actions/GetWorkflowMultiRunBranches@main
7782
with:
7883
shell: powershell
79-
includeBranches: ${{ github.event.inputs.includeBranches }}
84+
workflowEventName: ${{ env.WorkflowEventName }}
85+
includeBranches: ${{ inputs.includeBranches }}
8086

8187
- name: Determine Template URL
8288
id: DetermineTemplateUrl
8389
env:
84-
TemplateUrlAsInput: '${{ github.event.inputs.templateUrl }}'
90+
TemplateUrlAsInput: '${{ inputs.templateUrl }}'
8591
run: |
8692
$templateUrl = $env:templateUrl # Available from ReadSettings step
8793
if ($ENV:TemplateUrlAsInput) {
@@ -133,12 +139,12 @@ jobs:
133139

134140
- name: Calculate Commit Options
135141
env:
136-
directCommit: '${{ github.event.inputs.directCommit }}'
137-
downloadLatest: '${{ github.event.inputs.downloadLatest }}'
142+
directCommit: '${{ inputs.directCommit }}'
143+
downloadLatest: '${{ inputs.downloadLatest }}'
138144
run: |
139145
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
140-
if('${{ github.event_name }}' -in 'workflow_dispatch', 'workflow_call') {
141-
Write-Host "Using inputs from ${{ github.event_name }} event"
146+
if($env:WorkflowEventName -in 'workflow_dispatch', 'workflow_call') {
147+
Write-Host "Using inputs from $($env:WorkflowEventName) event"
142148
$directCommit = $env:directCommit
143149
$downloadLatest = $env:downloadLatest
144150
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ on:
2121
default: ''
2222
workflow_call:
2323
inputs:
24+
caller:
25+
description: Name of the calling workflow (use github.workflow as value when calling)
26+
type: string
27+
required: true
2428
templateUrl:
2529
description: Template Repository URL (current is {TEMPLATEURL})
2630
type: string
@@ -50,6 +54,7 @@ defaults:
5054
shell: powershell
5155

5256
env:
57+
WorkflowEventName: ${{ inputs.caller && 'workflow_call' || github.event_name }}
5358
ALGoOrgSettings: ${{ vars.ALGoOrgSettings }}
5459
ALGoRepoSettings: ${{ vars.ALGoRepoSettings }}
5560

@@ -76,12 +81,13 @@ jobs:
7681
uses: microsoft/AL-Go-Actions/GetWorkflowMultiRunBranches@main
7782
with:
7883
shell: powershell
79-
includeBranches: ${{ github.event.inputs.includeBranches }}
84+
workflowEventName: ${{ env.WorkflowEventName }}
85+
includeBranches: ${{ inputs.includeBranches }}
8086

8187
- name: Determine Template URL
8288
id: DetermineTemplateUrl
8389
env:
84-
TemplateUrlAsInput: '${{ github.event.inputs.templateUrl }}'
90+
TemplateUrlAsInput: '${{ inputs.templateUrl }}'
8591
run: |
8692
$templateUrl = $env:templateUrl # Available from ReadSettings step
8793
if ($ENV:TemplateUrlAsInput) {
@@ -133,12 +139,12 @@ jobs:
133139

134140
- name: Calculate Commit Options
135141
env:
136-
directCommit: '${{ github.event.inputs.directCommit }}'
137-
downloadLatest: '${{ github.event.inputs.downloadLatest }}'
142+
directCommit: '${{ inputs.directCommit }}'
143+
downloadLatest: '${{ inputs.downloadLatest }}'
138144
run: |
139145
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
140-
if('${{ github.event_name }}' -in 'workflow_dispatch', 'workflow_call') {
141-
Write-Host "Using inputs from ${{ github.event_name }} event"
146+
if($env:WorkflowEventName -in 'workflow_dispatch', 'workflow_call') {
147+
Write-Host "Using inputs from $($env:WorkflowEventName) event"
142148
$directCommit = $env:directCommit
143149
$downloadLatest = $env:downloadLatest
144150
}

Tests/DetermineProjectsToBuild.Test.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,15 +1044,15 @@ Describe "Get-ProjectsToBuild" {
10441044
}
10451045

10461046
It 'throws error when test project has buildable app folders' {
1047-
# TestProject has both projectsToTest setting AND an app folder this should fail
1047+
# TestProject has both projectsToTest setting AND an app folder - this should fail
10481048

10491049
$appFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }
10501050
New-Item -Path "$baseFolder/Project1/.AL-Go/settings.json" -type File -Force
10511051
New-Item -Path "$baseFolder/Project1/app/app.json" -Value (ConvertTo-Json $appFile -Depth 10) -type File -Force
10521052

10531053
New-Item -Path "$baseFolder/TestProject/.AL-Go/settings.json" -type File -Force
10541054
@{ projectsToTest = @("Project1") } | ConvertTo-Json -Depth 99 -Compress | Out-File (Join-Path $baseFolder "TestProject/.AL-Go/settings.json") -Encoding UTF8
1055-
# Add an app folder to the test project this should be forbidden
1055+
# Add an app folder to the test project - this should be forbidden
10561056
$testAppFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd2'; name = 'Bad App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }
10571057
New-Item -Path "$baseFolder/TestProject/app/app.json" -Value (ConvertTo-Json $testAppFile -Depth 10) -type File -Force
10581058

@@ -1066,7 +1066,7 @@ Describe "Get-ProjectsToBuild" {
10661066
}
10671067

10681068
It 'throws error when test project has buildable test folders' {
1069-
# TestProject has both projectsToTest setting AND a test folder this should fail
1069+
# TestProject has both projectsToTest setting AND a test folder - this should fail
10701070

10711071
$appFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }
10721072
New-Item -Path "$baseFolder/Project1/.AL-Go/settings.json" -type File -Force
@@ -1089,7 +1089,7 @@ Describe "Get-ProjectsToBuild" {
10891089

10901090
It 'throws error when one test project depends on another test project' {
10911091
# Project1 is a normal project, TestProject1 and TestProject2 are both test projects
1092-
# TestProject2 tries to depend on TestProject1 this should fail
1092+
# TestProject2 tries to depend on TestProject1 - this should fail
10931093
Mock OutputError {} -ModuleName DetermineProjectsToBuild
10941094

10951095
$appFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }

Tests/GetWorkflowMultiRunBranches.Test.ps1

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Describe "GetWorkflowMultiRunBranches Action" {
3939
$env:Settings = ""
4040
$env:GITHUB_REF_NAME = "main"
4141

42+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
43+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/main") }
44+
4245
# Call the action script
4346
. (Join-Path $scriptRoot "$actionName.ps1")
4447

@@ -52,6 +55,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
5255
$env:Settings = ""
5356
$env:GITHUB_REF_NAME = "main"
5457

58+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
5559
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }
5660

5761
# Call the action script
@@ -67,6 +71,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
6771
$env:Settings = ""
6872
$env:GITHUB_REF_NAME = "main"
6973

74+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
7075
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }
7176

7277
# Call the action script
@@ -82,6 +87,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
8287
$env:Settings = ""
8388
$env:GITHUB_REF_NAME = "main"
8489

90+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
8591
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/HEAD", "origin/main", "origin/develop", "origin/feature-1") }
8692

8793
# Call the action script with wildcard to get all branches
@@ -101,6 +107,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
101107
$env:Settings = "{ 'workflowSchedule': { 'includeBranches': [] } }"
102108
$env:GITHUB_REF_NAME = "default-branch"
103109

110+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
104111
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/default-branch", "origin") }
105112

106113
# Call the action script
@@ -116,6 +123,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
116123
$env:Settings = "{ 'workflowSchedule': { 'includeBranches': ['test-branch'] } }"
117124
$env:GITHUB_REF_NAME = "main"
118125

126+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
119127
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }
120128

121129
# Call the action script
@@ -131,6 +139,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
131139
$env:Settings = "{ 'workflowSchedule': { 'includeBranches': ['*branch*'] } }"
132140
$env:GITHUB_REF_NAME = "main"
133141

142+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
134143
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }
135144

136145
# Call the action script
@@ -141,4 +150,89 @@ Describe "GetWorkflowMultiRunBranches Action" {
141150
$outputValue | Should -Be "{`"branches`":[`"test-branch`",`"some-other-branch`"]}"
142151
}
143152
}
153+
154+
Context 'workflow_call event' {
155+
It 'Action sets the current branch as result when no branch patterns are specified' {
156+
$env:GITHUB_EVENT_NAME = "workflow_call"
157+
$env:Settings = ""
158+
$env:GITHUB_REF_NAME = "main"
159+
160+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
161+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/main") }
162+
163+
# Call the action script
164+
. (Join-Path $scriptRoot "$actionName.ps1")
165+
166+
$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
167+
$outputName | Should -Be "Result"
168+
$outputValue | Should -Be "{`"branches`":[`"main`"]}"
169+
}
170+
171+
It 'Action sets the input branch as result when a branch pattern is specified' {
172+
$env:GITHUB_EVENT_NAME = "workflow_call"
173+
$env:Settings = ""
174+
$env:GITHUB_REF_NAME = "main"
175+
176+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
177+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }
178+
179+
# Call the action script
180+
. (Join-Path $scriptRoot "$actionName.ps1") -includeBranches "test-branch"
181+
182+
$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
183+
$outputName | Should -Be "Result"
184+
$outputValue | Should -Be "{`"branches`":[`"test-branch`"]}"
185+
}
186+
187+
It 'Action sets the input branch as result when a branch pattern with wild card is specified' {
188+
$env:GITHUB_EVENT_NAME = "workflow_call"
189+
$env:Settings = ""
190+
$env:GITHUB_REF_NAME = "main"
191+
192+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
193+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }
194+
195+
# Call the action script
196+
. (Join-Path $scriptRoot "$actionName.ps1") -includeBranches "*branch*"
197+
198+
$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
199+
$outputName | Should -Be "Result"
200+
$outputValue | Should -Be "{`"branches`":[`"test-branch`",`"some-other-branch`"]}"
201+
}
202+
203+
It 'Action filters out HEAD symbolic reference when using wildcard' {
204+
$env:GITHUB_EVENT_NAME = "workflow_call"
205+
$env:Settings = ""
206+
$env:GITHUB_REF_NAME = "main"
207+
208+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
209+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/HEAD", "origin/main", "origin/develop", "origin/feature-1") }
210+
211+
# Call the action script with wildcard to get all branches
212+
. (Join-Path $scriptRoot "$actionName.ps1") -includeBranches "*"
213+
214+
$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
215+
$outputName | Should -Be "Result"
216+
# Verify that HEAD is not included in the result
217+
$outputValue | Should -Not -Match "HEAD"
218+
$outputValue | Should -Be "{`"branches`":[`"main`",`"develop`",`"feature-1`"]}"
219+
}
220+
}
221+
222+
Context 'Parameter override tests' {
223+
It 'workflowEventName parameter overrides GITHUB_EVENT_NAME environment variable' {
224+
$env:GITHUB_EVENT_NAME = "schedule"
225+
$env:Settings = "{ 'workflowSchedule': { 'includeBranches': ['schedule-branch'] } }"
226+
$env:GITHUB_REF_NAME = "main"
227+
228+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
229+
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/call-branch", "origin/schedule-branch", "origin/main") }
230+
231+
# Parameter should override environment variable
232+
. (Join-Path $scriptRoot "$actionName.ps1") -workflowEventName "workflow_call" -includeBranches "call-branch"
233+
234+
$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
235+
$outputValue | Should -Be "{`"branches`":[`"call-branch`"]}"
236+
}
237+
}
144238
}

0 commit comments

Comments
 (0)