Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utilities] Enabled Workflow-Trigger function to trigger only for module diff (ported from AVM) #4499

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading