From 9ae36f14e8b63b4894ec6cebf42a7fd30aeb4f80 Mon Sep 17 00:00:00 2001 From: agneszitte Date: Wed, 27 Mar 2024 19:12:14 -0400 Subject: [PATCH] ci: Adjust CI Build for Doc-Only PRs validations --- azure-pipelines.yml | 33 ++++++--------- build/stage-determine-changes.yml | 69 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 build/stage-determine-changes.yml diff --git a/azure-pipelines.yml b/azure-pipelines.yml index cf86ba553..ed1149188 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -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: @@ -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) @@ -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 diff --git a/build/stage-determine-changes.yml b/build/stage-determine-changes.yml new file mode 100644 index 000000000..e44331822 --- /dev/null +++ b/build/stage-determine-changes.yml @@ -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" \ No newline at end of file