Skip to content

Commit

Permalink
ci: Adjust CI Build for Doc-Only PRs validations
Browse files Browse the repository at this point in the history
  • Loading branch information
agneszitte committed Mar 27, 2024
1 parent d2eb75c commit 9ae36f1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 20 deletions.
33 changes: 13 additions & 20 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@
- legacy/*
- feature/*

paths:
include:
- '/'
exclude:
- .github/
# don't trigger the CI if only docs files are changed
- doc/*
- '**/*.md'

pr:
branches:
include:
Expand All @@ -25,15 +16,6 @@ pr:
- legacy/*
- feature/*

paths:
include:
- '/'
exclude:
- .github/
# don't trigger the CI if only docs files are changed
- doc/*
- '**/*.md'

variables:
# Path where packages (nuget or app packages) will be copied to.
PackageOutputPath: $(Build.ArtifactStagingDirectory)
Expand All @@ -49,18 +31,29 @@ variables:
# AndroidNdkDirectory: C:\Microsoft\AndroidNDK64\android-ndk-r16b

stages:
- stage: DetermineChanges
jobs:
- template: build/stage-determine-changes.yml

- stage: Validations
displayName: Validations
dependsOn: DetermineChanges
# Trigger this stage when docs files are changed
condition: or(eq(variables['docsOnly'], 'true'), eq(variables['mixedChanges'], 'true'))
jobs:
- template: build/stage-validations.yml

- stage: Packages
dependsOn: Validations
dependsOn: DetermineChanges
# Don't trigger this stage if only docs files are changed
condition: eq(variables['nonDocsOnly'], 'true')
jobs:
- template: build/stage-build-packages.yml

- stage: Build_Samples
dependsOn: Validations
dependsOn: DetermineChanges
# Don't trigger this stage if only docs files are changed
condition: eq(variables['nonDocsOnly'], 'true')
jobs:
- template: build/stage-build-ios.yml
- template: build/stage-build-android.yml
Expand Down
69 changes: 69 additions & 0 deletions build/stage-determine-changes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
jobs:
- job: evaluate_changes
displayName: 'Check for Doc Only Changes'
pool:
vmImage: 'ubuntu-latest'
steps:
- powershell: |
# Determine the context of the build (PR or push) and fetch the target branch if necessary
$prTargetBranch = "$(System.PullRequest.TargetBranch)"
$isPR = -not [string]::IsNullOrWhiteSpace($prTargetBranch)
$defaultBaseBranch = "master"
Write-Host "Build context determined: $(if ($isPR) { 'Pull Request' } else { 'Push' })"
if ($isPR) {
Write-Host "It's a PR build. Fetching the target branch ($defaultBaseBranch)..."
git fetch origin $defaultBaseBranch
$mergeBase = git merge-base HEAD "origin/$defaultBaseBranch"
Write-Host "Merge base identified at $mergeBase"
} else {
Write-Host "It's a push build. Finding the most recent common ancestor with $defaultBaseBranch..."
git fetch origin $defaultBaseBranch
$mergeBase = git merge-base HEAD "origin/$defaultBaseBranch"
Write-Host "Merge base for push build identified at $mergeBase"
}
Write-Host "Comparing changes from $mergeBase..."
$gitDiffCommand = "git diff $mergeBase --name-only"
$changedFiles = Invoke-Expression $gitDiffCommand
$docsOnly = $true
$nonDocsOnly = $false
$mixedChanges = $false
if ($changedFiles) {
Write-Host "Changed files:"
Write-Host $changedFiles
} else {
Write-Host "No files have changed."
}
foreach ($file in $changedFiles -split "`n") {
if ($file.EndsWith(".md") -or $file -like "*/toc.yml") {
if ($nonDocsOnly) {
$mixedChanges = $true
$docsOnly = $false
break
}
} else {
$nonDocsOnly = $true
if ($docsOnly) {
$docsOnly = $false
$mixedChanges = $true
} else {
break
}
}
}
if ($docsOnly) {
Write-Host "All changes are documentation-only."
} elseif ($mixedChanges) {
Write-Host "Mixed changes detected: Both documentation and non-documentation files have been modified."
} else {
Write-Host "Non-documentation changes detected."
}
# Output the results as pipeline variables
Write-Host "##vso[task.setvariable variable=docsOnly]$docsOnly"
Write-Host "##vso[task.setvariable variable=nonDocsOnly]$nonDocsOnly"
Write-Host "##vso[task.setvariable variable=mixedChanges]$mixedChanges"

0 comments on commit 9ae36f1

Please sign in to comment.