Skip to content

Commit

Permalink
[Utilities] Enabled Workflow-Trigger function to trigger only for mod…
Browse files Browse the repository at this point in the history
…ule diff (ported from AVM) (#4499)

* Add support for triggering workflows based on file diff

* Dummy change to test diff

* Dummy change to test diff

* Adding diff module filter

* Removed dummy changes
  • Loading branch information
krbar authored Feb 19, 2024
1 parent bcef5fe commit 4cceefc
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions utilities/tools/Invoke-PipelinesForBranch.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<Placeholder>' -GitHubRepositoryOwner 'Azure' -GitHubRepositoryName 'ResourceModules' -WorkflowFileName 'ms.analysisservices.servers.yml' -TargetBranch 'main' -GitHubPipelineInputs @{ prerelease = 'false'; staticValidation = 'true'; deploymentValidation = 'true'; removeDeployment = 'true' }
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4cceefc

Please sign in to comment.