diff --git a/utilities/tools/Invoke-PipelinesForBranch.ps1 b/utilities/tools/Invoke-PipelinesForBranch.ps1 index 9b2acad2c2..704830f29c 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' } @@ -73,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 } } @@ -215,6 +222,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 +269,36 @@ 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 $RepositoryRoot '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 + $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) + } + + Write-Verbose ("As per 'diff', filtered workflows down to [{0}]" -f $workflows.Count) -Verbose + } + $gitHubWorkflowBadges = [System.Collections.ArrayList]@() Write-Verbose "Triggering GitHub workflows for branch [$TargetBranch]" -Verbose