Created
May 2, 2023 15:29
-
-
Save junalmeida/17c4aef6474a6c77df12e1c3317661a0 to your computer and use it in GitHub Desktop.
List changes on a mono repository to build conditionally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- task: PowerShell@2 | |
name: Changes | |
inputs: | |
targetType: "filePath" | |
filePath: "list-changes.ps1" | |
arguments: -sourceBranch $(Build.SourceBranch) -sourceCommit $(Build.SourceVersion) -targetBranch $(TargetBranch) -rules @(@("Web", "src/web/**"),@("Mobile", "src/mobile/**"),@("Shared", "src/shared/**")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ([string]$sourceBranch, [string]$sourceCommit, [string]$targetBranch, [array]$rules) | |
# -sourceBranch $(Build.SourceBranch) | |
# -sourceCommit $(Build.SourceVersion) | |
# -targetBranch $(System.PullRequest.TargetBranch) | |
# -rules @(@("Rule1", "Proj1/*.cs", "Proj1/*.ts"), ("Rule2", "Proj2/*.cs")) | |
if ($sourceBranch -like 'refs/pull/*') { | |
Write-Host "Comparing $sourceBranch with origin/$targetBranch" | |
$files = $(git diff --name-only -r origin/$targetBranch) -split ' ' | |
} | |
else { | |
Write-Host "Recursive comparing $sourceBranch with $sourceCommit" | |
$files = $(git diff-tree --no-commit-id --name-only -r $sourceCommit^1 $sourceCommit) -split ' ' | |
} | |
$result = New-Object Collections.Generic.List[string] | |
Write-Host "Total changed $($files.Length) files" | |
if ($files.Length -eq 0) { | |
Write-Error "No changes, this is a bug" | |
Exit 1 | |
} | |
foreach ($rule in $rules) { | |
$ruleName = $rule[0] | |
foreach ($pattern in ($rule | Select-Object -skip 1)) { | |
foreach ($file in $files) { | |
if ($file -like $pattern) { | |
Write-Debug $file | |
$result.Add($ruleName) | |
} | |
} | |
} | |
} | |
if (![string]::IsNullOrWhiteSpace($env:FORCE_BUILD)) { | |
$result.Add($env:FORCE_BUILD) | |
} | |
$result = $result | Get-Unique | |
Write-Host "Matched changes: $($result -join ' ')" | |
foreach ($r in $result) { | |
Write-Host "##vso[task.setvariable variable=$r;isoutput=True]True" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment