From 2c827fe6d367484e3a6437eeb6f1094345e77b8a Mon Sep 17 00:00:00 2001 From: Kris Baranek Date: Fri, 16 Feb 2024 14:04:18 +0100 Subject: [PATCH 1/5] Add support for triggering workflows based on file diff --- utilities/tools/Invoke-PipelinesForBranch.ps1 | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/utilities/tools/Invoke-PipelinesForBranch.ps1 b/utilities/tools/Invoke-PipelinesForBranch.ps1 index 9b2acad2c2..15d0cb71e3 100644 --- a/utilities/tools/Invoke-PipelinesForBranch.ps1 +++ b/utilities/tools/Invoke-PipelinesForBranch.ps1 @@ -28,6 +28,9 @@ Optional. Input parameters to pass into the pipeline. Must match the names of th .PARAMETER WorkflowFilePath Required. The path to the workflow. +.PARAMETER InvokeForDiff +Optional. Trigger workflows only for those who's module files have changed (based on diff of branch to main) + .EXAMPLE Invoke-GitHubWorkflow -PersonalAccessToken '' -GitHubRepositoryOwner 'Azure' -GitHubRepositoryName 'ResourceModules' -WorkflowFileName 'ms.analysisservices.servers.yml' -TargetBranch 'main' -GitHubPipelineInputs @{ prerelease = 'false'; staticValidation = 'true'; deploymentValidation = 'true'; removeDeployment = 'true' } @@ -215,6 +218,9 @@ function Invoke-PipelinesForBranch { [Parameter(Mandatory = $false)] [string] $PipelineFilter = 'ms.*', + [Parameter(Mandatory = $false)] + [switch] $InvokeForDiff, + [Parameter(Mandatory = $false)] [ValidateSet('GitHub', 'AzureDevOps')] [string] $Environment = 'GitHub', @@ -259,6 +265,31 @@ function Invoke-PipelinesForBranch { Write-Verbose 'Fetching current GitHub workflows' -Verbose $workflows = Get-GitHubModuleWorkflowList @baseInputObject -Filter $PipelineFilter + Write-Verbose ('Fetched [{0}] workflows' -f $workflows.Count) -Verbose + + if ($InvokeForDiff) { + # Load used function + . (Join-Path $RepoRoot 'utilities' 'tools' 'helper' 'Get-PipelineFileName.ps1') + + # Get diff + $diff = git diff 'main' --name-only + + # Identify pipeline names + $pipelineNames = [System.Collections.ArrayList]@() + $pipelineNames = $diff | ForEach-Object { + $folderPath = Split-Path $_ -Parent + $pipelineFileName = Get-PipelineFileName -ResourceIdentifier $folderPath + if ($pipelineFileName -match $PipelineFilter) { + $pipelineFileName + } + } | Select-Object -Unique + + # Filter workflows + $workflows = $workflows | Where-Object { $pipelineNames -contains (Split-Path $_.path -Leaf) } + + Write-Verbose ("As per 'diff', filtered workflows down to [{0}]" -f $workflows.Count) + } + $gitHubWorkflowBadges = [System.Collections.ArrayList]@() Write-Verbose "Triggering GitHub workflows for branch [$TargetBranch]" -Verbose From cd4932f29dfc54bf251befba7e7e3d474561b3a2 Mon Sep 17 00:00:00 2001 From: Kris Baranek Date: Fri, 16 Feb 2024 19:53:13 +0100 Subject: [PATCH 2/5] Dummy change to test diff --- modules/network/ip-group/main.bicep | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/network/ip-group/main.bicep b/modules/network/ip-group/main.bicep index 08a30eee33..91f139c485 100644 --- a/modules/network/ip-group/main.bicep +++ b/modules/network/ip-group/main.bicep @@ -9,7 +9,7 @@ param name string @description('Optional. Location for all resources.') param location string = resourceGroup().location -@description('Optional. IpAddresses/IpAddressPrefixes in the IpGroups resource.') +@description('Optional. IpAddresses/IpAddressPrefixes in the IpGroups resource...') param ipAddresses array = [] @description('Optional. The lock settings of the service.') From bd9c1d18765a2f65ce208593aba3f5869d2adc86 Mon Sep 17 00:00:00 2001 From: Kris Baranek Date: Fri, 16 Feb 2024 23:07:03 +0100 Subject: [PATCH 3/5] Dummy change to test diff --- modules/network/public-ip-address/main.bicep | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/network/public-ip-address/main.bicep b/modules/network/public-ip-address/main.bicep index 46fd1decb2..b9f376fcbc 100644 --- a/modules/network/public-ip-address/main.bicep +++ b/modules/network/public-ip-address/main.bicep @@ -8,7 +8,7 @@ param name string @description('Optional. Resource ID of the Public IP Prefix object. This is only needed if you want your Public IPs created in a PIP Prefix.') param publicIPPrefixResourceId string = '' -@description('Optional. The public IP address allocation method.') +@description('Optional. The public IP address allocation method...') @allowed([ 'Dynamic' 'Static' From 41e37c50071b1448cf606ad37f9719aed09e38d3 Mon Sep 17 00:00:00 2001 From: Kris Baranek Date: Sat, 17 Feb 2024 01:16:18 +0100 Subject: [PATCH 4/5] Adding diff module filter --- utilities/tools/Invoke-PipelinesForBranch.ps1 | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/utilities/tools/Invoke-PipelinesForBranch.ps1 b/utilities/tools/Invoke-PipelinesForBranch.ps1 index 15d0cb71e3..704830f29c 100644 --- a/utilities/tools/Invoke-PipelinesForBranch.ps1 +++ b/utilities/tools/Invoke-PipelinesForBranch.ps1 @@ -76,10 +76,14 @@ function Invoke-GitHubWorkflow { } | ConvertTo-Json } if ($PSCmdlet.ShouldProcess("GitHub workflow [$workflowFileName] for branch [$TargetBranch]", 'Invoke')) { - $response = Invoke-RestMethod @requestInputObject -Verbose:$false + try { + $response = Invoke-RestMethod @requestInputObject -Verbose:$false + } catch { + Write-Error "Request failed for [$workflowFileName]. Reponse: [$_]" + } if ($response) { - Write-Error "Request failed. Reponse: [$response]" + Write-Error "Request failed for [$workflowFileName]. Reponse: [$response]" return $false } } @@ -269,7 +273,7 @@ function Invoke-PipelinesForBranch { if ($InvokeForDiff) { # Load used function - . (Join-Path $RepoRoot 'utilities' 'tools' 'helper' 'Get-PipelineFileName.ps1') + . (Join-Path $RepositoryRoot 'utilities' 'tools' 'helper' 'Get-PipelineFileName.ps1') # Get diff $diff = git diff 'main' --name-only @@ -278,16 +282,21 @@ function Invoke-PipelinesForBranch { $pipelineNames = [System.Collections.ArrayList]@() $pipelineNames = $diff | ForEach-Object { $folderPath = Split-Path $_ -Parent - $pipelineFileName = Get-PipelineFileName -ResourceIdentifier $folderPath - if ($pipelineFileName -match $PipelineFilter) { - $pipelineFileName + $resourceTypeIdentifier = ($folderPath -split 'modules[\/|\\]{1}')[1] -replace '\\', '/' + if ($resourceTypeIdentifier.Length -gt 0) { + $pipelineFileName = Get-PipelineFileName -ResourceIdentifier $resourceTypeIdentifier + if ($pipelineFileName -match $PipelineFilter) { + $pipelineFileName + } } } | Select-Object -Unique # Filter workflows - $workflows = $workflows | Where-Object { $pipelineNames -contains (Split-Path $_.path -Leaf) } + $workflows = $workflows | Where-Object { + $pipelineNames -contains (Split-Path $_.path -Leaf) + } - Write-Verbose ("As per 'diff', filtered workflows down to [{0}]" -f $workflows.Count) + Write-Verbose ("As per 'diff', filtered workflows down to [{0}]" -f $workflows.Count) -Verbose } $gitHubWorkflowBadges = [System.Collections.ArrayList]@() From f26cf22a1e26706abb2fee8d6551cb30df006c86 Mon Sep 17 00:00:00 2001 From: Kris Baranek Date: Sun, 18 Feb 2024 22:30:20 +0100 Subject: [PATCH 5/5] Removed dummy changes --- modules/network/ip-group/main.bicep | 2 +- modules/network/public-ip-address/main.bicep | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/network/ip-group/main.bicep b/modules/network/ip-group/main.bicep index 91f139c485..08a30eee33 100644 --- a/modules/network/ip-group/main.bicep +++ b/modules/network/ip-group/main.bicep @@ -9,7 +9,7 @@ param name string @description('Optional. Location for all resources.') param location string = resourceGroup().location -@description('Optional. IpAddresses/IpAddressPrefixes in the IpGroups resource...') +@description('Optional. IpAddresses/IpAddressPrefixes in the IpGroups resource.') param ipAddresses array = [] @description('Optional. The lock settings of the service.') diff --git a/modules/network/public-ip-address/main.bicep b/modules/network/public-ip-address/main.bicep index b9f376fcbc..46fd1decb2 100644 --- a/modules/network/public-ip-address/main.bicep +++ b/modules/network/public-ip-address/main.bicep @@ -8,7 +8,7 @@ param name string @description('Optional. Resource ID of the Public IP Prefix object. This is only needed if you want your Public IPs created in a PIP Prefix.') param publicIPPrefixResourceId string = '' -@description('Optional. The public IP address allocation method...') +@description('Optional. The public IP address allocation method.') @allowed([ 'Dynamic' 'Static'