-
Notifications
You must be signed in to change notification settings - Fork 226
Optimize Azure Pipelines with dynamic detection of DSC resource dependencies #2135
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
Merged
johlju
merged 24 commits into
main
from
copilot/fix-fdccc632-9ffe-4f0e-8c81-5dba10fa5a11
Aug 17, 2025
Merged
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ae0bf27
Initial plan
Copilot 96ea680
Add script to determine when DSC resource integration tests should run
Copilot 8974f5f
Implement conditional DSC resource integration tests based on changed…
Copilot b0ec675
Update CHANGELOG for DSC resource integration test optimization
Copilot ff30260
Implement dynamic discovery of public commands used by DSC resources …
Copilot 6e5f20e
Simplify Deploy stage condition by removing complex DSC test logic
Copilot fe6818a
Fix formatting and style issues in Test-ShouldRunDscResourceIntegrati…
Copilot 3237291
Revert unrelated changes
johlju 3e8f0b1
Improve output formatting for better readability in Azure Pipelines logs
Copilot c15e72a
Add detection of private functions used by class-based DSC resources
Copilot caad18c
Add Azure DevOps logging commands for improved pipeline log readability
Copilot 618ab65
Fix Azure DevOps logging commands and warning message formats
Copilot d89602a
Remove ##[warning] prefixes, add ##[endgroup] tags, and remove pipeli…
Copilot 1ae4e34
Remove exit codes and set proper Azure DevOps output variable
Copilot a90f50a
Move output variable setting to azure-pipelines.yml inline script and…
Copilot 61cd40b
Remove trailing whitespace from all blank lines in modified files
Copilot a9ca344
Merge branch 'main' into copilot/fix-fdccc632-9ffe-4f0e-8c81-5dba10fa…
johlju 65f9220
Fix markdown formatting issues in .build/README.md
Copilot a2b9b7e
Update .build/Test-ShouldRunDscResourceIntegrationTests.ps1
johlju e02341a
Update .build/Test-ShouldRunDscResourceIntegrationTests.ps1
johlju a7db956
Update CHANGELOG.md
johlju 2f29563
Update CHANGELOG.md
johlju ed6420b
Fix markdown formatting issues and remove trailing whitespace
Copilot 6e80d8c
Fix README.md
johlju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # DSC Resource Integration Test Optimization | ||
|
|
||
| This document describes the optimization implemented to conditionally run DSC resource integration tests in Azure Pipelines based on the changes made in a pull request or commit. | ||
|
|
||
| ## Overview | ||
|
|
||
| The Azure Pipelines configuration has been enhanced to automatically determine whether DSC resource integration tests need to run based on the files that have been changed. This optimization significantly reduces CI/CD time when changes don't affect DSC resources. | ||
|
|
||
| ## How It Works | ||
|
|
||
| ### Detection Script | ||
|
|
||
| The `.build/Test-ShouldRunDscResourceIntegrationTests.ps1` script analyzes git diff output to determine if DSC resource integration tests should run. It checks for changes in: | ||
|
|
||
| 1. **DSC Resources**: Any changes to files under `source/DSCResources/` or `source/Classes/` | ||
| 2. **Public Commands Used by DSC Resources**: Changes to specific public commands that are called by DSC resources or classes | ||
| 3. **Private Functions**: Changes to private functions that are used by the public commands mentioned above | ||
| 4. **Integration Test Files**: Changes to DSC resource integration test files themselves | ||
| 5. **Pipeline Configuration**: Changes to pipeline configuration files | ||
|
|
||
| ### Public Commands Monitored | ||
|
|
||
| The following public commands are monitored because they are used by DSC resources or classes: | ||
|
|
||
| - `Add-SqlDscDatabaseRoleMember` | ||
| - `Add-SqlDscServerRoleMember` | ||
| - `Connect-SqlDscDatabaseEngine` | ||
| - `Disable-SqlDscAudit` | ||
| - `Enable-SqlDscAudit` | ||
| - `Get-SqlDscAudit` | ||
| - `Get-SqlDscDatabasePermission` | ||
| - `Get-SqlDscDynamicMaxDop` | ||
| - `Get-SqlDscDynamicMaxMemory` | ||
| - `Get-SqlDscManagedComputer` | ||
| - `Get-SqlDscPercentMemory` | ||
| - `Get-SqlDscRSSetupConfiguration` | ||
| - `Get-SqlDscServerPermission` | ||
| - `Import-SqlDscPreferredModule` | ||
| - `Install-SqlDscBIReportServer` | ||
| - `Install-SqlDscReportingService` | ||
| - `Invoke-SqlDscQuery` | ||
| - `New-SqlDscAudit` | ||
| - `Remove-SqlDscAudit` | ||
| - `Remove-SqlDscDatabaseRoleMember` | ||
| - `Remove-SqlDscServerRoleMember` | ||
| - `Repair-SqlDscBIReportServer` | ||
| - `Repair-SqlDscReportingService` | ||
| - `Set-SqlDscAudit` | ||
| - `Set-SqlDscDatabasePermission` | ||
| - `Set-SqlDscServerPermission` | ||
| - `Test-SqlDscIsDatabasePrincipal` | ||
| - `Test-SqlDscIsLogin` | ||
| - `Test-SqlDscIsSupportedFeature` | ||
| - `Uninstall-SqlDscBIReportServer` | ||
| - `Uninstall-SqlDscReportingService` | ||
|
|
||
| ### Pipeline Changes | ||
|
|
||
| The Azure Pipelines configuration has been modified to: | ||
|
|
||
| 1. Add a new job `Determine_DSC_Resource_Test_Requirements` in the `Quality_Test_and_Unit_Test` stage | ||
| 2. Add conditions to the following stages to only run when DSC resource integration tests are needed: | ||
| - `Integration_Test_Resources_SqlServer` | ||
| - `Integration_Test_Resources_SqlServer_dbatools` | ||
| - `Integration_Test_Resources_ReportingServices` | ||
| - `Integration_Test_Resources_PowerBIReportServer` | ||
| - `Integration_Test_Resources_ReportingServices_dbatools` | ||
| 3. Update the `Deploy` stage to handle conditional dependencies | ||
|
|
||
| ## What Always Runs | ||
|
|
||
| The following tests and stages always run regardless of changes: | ||
|
|
||
| - **Unit Tests**: All unit tests continue to run for every change | ||
| - **QA Tests**: Quality assurance tests always run | ||
| - **Command Integration Tests**: Integration tests for public commands always run | ||
| - `Integration_Test_Commands_SqlServer` | ||
| - `Integration_Test_Commands_ReportingServices` | ||
| - `Integration_Test_Commands_BIReportServer` | ||
|
|
||
| ## What Runs Conditionally | ||
|
|
||
| The following stages only run when changes affect DSC resources: | ||
|
|
||
| - `Integration_Test_Resources_SqlServer` | ||
| - `Integration_Test_Resources_SqlServer_dbatools` | ||
| - `Integration_Test_Resources_ReportingServices` | ||
| - `Integration_Test_Resources_PowerBIReportServer` | ||
| - `Integration_Test_Resources_ReportingServices_dbatools` | ||
|
|
||
| ## Benefits | ||
|
|
||
| - **Reduced CI/CD Time**: DSC resource integration tests can take significant time; skipping them when not needed speeds up the pipeline | ||
| - **Resource Efficiency**: Fewer compute resources used when DSC resource tests are skipped | ||
| - **Faster Feedback**: Developers get faster feedback on changes that don't affect DSC resources | ||
| - **Maintained Quality**: All relevant tests still run when needed to ensure quality is not compromised | ||
|
|
||
| ## Edge Cases | ||
|
|
||
| The script errs on the side of caution: | ||
|
|
||
| - If git diff fails, DSC resource integration tests will run | ||
| - If no changes are detected, DSC resource integration tests will run | ||
| - If pipeline configuration changes, DSC resource integration tests will run | ||
|
|
||
| ## Maintenance | ||
|
|
||
| When adding new public commands that are used by DSC resources or classes, update the `$PublicCommandsUsedByDscResources` array in the `.build/Test-ShouldRunDscResourceIntegrationTests.ps1` script. | ||
|
|
||
| To check which commands are used by DSC resources, run: | ||
|
|
||
| ```bash | ||
| # Find DSC resources that use public commands | ||
| find source/DSCResources -name "*.psm1" -exec grep -l "Get-SqlDsc\|Set-SqlDsc\|Test-SqlDsc\|Connect-SqlDsc\|Invoke-SqlDsc\|New-SqlDsc\|Remove-SqlDsc\|Add-SqlDsc\|Disable-SqlDsc\|Enable-SqlDsc\|Complete-SqlDsc\|Import-SqlDsc\|Initialize-SqlDsc\|Install-SqlDsc\|Repair-SqlDsc\|Save-SqlDsc\|Uninstall-SqlDsc\|Assert-SqlDsc" {} \; | ||
|
|
||
| # Find classes that use public commands | ||
| find source/Classes -name "*.ps1" -exec grep -l "Get-SqlDsc\|Set-SqlDsc\|Test-SqlDsc\|Connect-SqlDsc\|Invoke-SqlDsc\|New-SqlDsc\|Remove-SqlDsc\|Add-SqlDsc\|Disable-SqlDsc\|Enable-SqlDsc\|Complete-SqlDsc\|Import-SqlDsc\|Initialize-SqlDsc\|Install-SqlDsc\|Repair-SqlDsc\|Save-SqlDsc\|Uninstall-SqlDsc\|Assert-SqlDsc" {} \; | ||
| ``` | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Determines if DSC resource integration tests should run based on changed files. | ||
|
|
||
| .DESCRIPTION | ||
| This script analyzes the git diff to determine if DSC resource integration tests | ||
| need to run. It checks if changes affect: | ||
| - DSC resources themselves | ||
| - Public commands used by DSC resources or classes | ||
| - Private functions used by those public commands | ||
| - Classes used by DSC resources | ||
|
|
||
| .PARAMETER BaseBranch | ||
| The base branch to compare against. Default is 'origin/main'. | ||
|
|
||
| .PARAMETER CurrentBranch | ||
| The current branch or commit to compare. Default is 'HEAD'. | ||
|
|
||
| .EXAMPLE | ||
| Test-ShouldRunDscResourceIntegrationTests | ||
|
|
||
| .EXAMPLE | ||
| Test-ShouldRunDscResourceIntegrationTests -BaseBranch 'origin/main' -CurrentBranch 'HEAD' | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter()] | ||
| [string] | ||
| $BaseBranch = 'origin/main', | ||
|
|
||
| [Parameter()] | ||
| [string] | ||
| $CurrentBranch = 'HEAD' | ||
| ) | ||
|
|
||
| # Public commands that are used by DSC resources or classes | ||
| $PublicCommandsUsedByDscResources = @( | ||
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'Add-SqlDscDatabaseRoleMember', | ||
| 'Add-SqlDscServerRoleMember', | ||
| 'Connect-SqlDscDatabaseEngine', | ||
| 'Disable-SqlDscAudit', | ||
| 'Enable-SqlDscAudit', | ||
| 'Get-SqlDscAudit', | ||
| 'Get-SqlDscDatabasePermission', | ||
| 'Get-SqlDscDynamicMaxDop', | ||
| 'Get-SqlDscDynamicMaxMemory', | ||
| 'Get-SqlDscManagedComputer', | ||
| 'Get-SqlDscPercentMemory', | ||
| 'Get-SqlDscRSSetupConfiguration', | ||
| 'Get-SqlDscServerPermission', | ||
| 'Import-SqlDscPreferredModule', | ||
| 'Install-SqlDscBIReportServer', | ||
| 'Install-SqlDscReportingService', | ||
| 'Invoke-SqlDscQuery', | ||
| 'New-SqlDscAudit', | ||
| 'Remove-SqlDscAudit', | ||
| 'Remove-SqlDscDatabaseRoleMember', | ||
| 'Remove-SqlDscServerRoleMember', | ||
| 'Repair-SqlDscBIReportServer', | ||
| 'Repair-SqlDscReportingService', | ||
| 'Set-SqlDscAudit', | ||
| 'Set-SqlDscDatabasePermission', | ||
| 'Set-SqlDscServerPermission', | ||
| 'Test-SqlDscIsDatabasePrincipal', | ||
| 'Test-SqlDscIsLogin', | ||
| 'Test-SqlDscIsSupportedFeature', | ||
| 'Uninstall-SqlDscBIReportServer', | ||
| 'Uninstall-SqlDscReportingService' | ||
| ) | ||
|
|
||
| function Get-ChangedFiles { | ||
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <# | ||
| .SYNOPSIS | ||
| Gets the list of files changed between two git references. | ||
| #> | ||
| param( | ||
| [string]$From, | ||
| [string]$To | ||
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| try { | ||
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Try different git diff approaches | ||
| $gitDiffOutput = $null | ||
|
|
||
| # First, try the standard diff | ||
| $gitDiffOutput = & git diff --name-only "$From..$To" 2>&1 | ||
| if ($LASTEXITCODE -eq 0 -and $gitDiffOutput) { | ||
| return $gitDiffOutput | Where-Object { $_ -and $_.Trim() } | ||
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| # If that fails, try without the range syntax | ||
| $gitDiffOutput = & git diff --name-only $From $To 2>&1 | ||
| if ($LASTEXITCODE -eq 0 -and $gitDiffOutput) { | ||
| return $gitDiffOutput | Where-Object { $_ -and $_.Trim() } | ||
| } | ||
|
|
||
| # If we're comparing with HEAD and have untracked files, include them | ||
| if ($To -eq 'HEAD') { | ||
| $untrackedFiles = & git ls-files --others --exclude-standard 2>&1 | ||
| if ($LASTEXITCODE -eq 0 -and $untrackedFiles) { | ||
| return $untrackedFiles | Where-Object { $_ -and $_.Trim() } | ||
| } | ||
| } | ||
|
|
||
| Write-Warning "Failed to get git diff between $From and $To. Exit code: $LASTEXITCODE. Output: $gitDiffOutput" | ||
| return @() | ||
| } | ||
| catch { | ||
| Write-Warning "Error getting changed files: $_" | ||
| return @() | ||
| } | ||
| } | ||
|
|
||
| function Get-PrivateFunctionsUsedByCommand { | ||
| <# | ||
| .SYNOPSIS | ||
| Gets private functions that a public command depends on. | ||
| #> | ||
| param( | ||
| [string]$CommandName, | ||
| [string]$SourcePath | ||
| ) | ||
|
|
||
| $commandFile = Join-Path $SourcePath "Public/$CommandName.ps1" | ||
| if (-not (Test-Path $commandFile)) { | ||
| return @() | ||
| } | ||
|
|
||
| $privateFunctions = @() | ||
| $content = Get-Content $commandFile -Raw | ||
|
|
||
| # Look for direct function calls to private functions | ||
| $privateFunctionFiles = Get-ChildItem (Join-Path $SourcePath "Private") -Filter "*.ps1" | Select-Object -ExpandProperty BaseName | ||
| foreach ($privateFunction in $privateFunctionFiles) { | ||
| if ($content -match "\b$privateFunction\b") { | ||
| $privateFunctions += $privateFunction | ||
| } | ||
| } | ||
|
|
||
| return $privateFunctions | ||
| } | ||
|
|
||
| function Test-ShouldRunDscResourceIntegrationTests { | ||
| <# | ||
| .SYNOPSIS | ||
| Main function that determines if DSC resource integration tests should run. | ||
| #> | ||
| param( | ||
| [string]$BaseBranch = 'origin/main', | ||
| [string]$CurrentBranch = 'HEAD', | ||
| [string]$SourcePath = 'source' | ||
| ) | ||
|
|
||
| Write-Output "Analyzing changes between $BaseBranch and $CurrentBranch..." | ||
|
|
||
| $changedFiles = Get-ChangedFiles -From $BaseBranch -To $CurrentBranch | ||
|
|
||
| if (-not $changedFiles) { | ||
| Write-Output "No changed files detected. DSC resource integration tests will run by default." | ||
| return $true | ||
| } | ||
|
|
||
| Write-Output "Changed files:" | ||
| $changedFiles | ForEach-Object { Write-Output " $_" } | ||
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Check if any DSC resources are directly changed | ||
| $changedDscResources = $changedFiles | Where-Object { $_ -match '^source/DSCResources/' -or $_ -match '^source/Classes/' } | ||
| if ($changedDscResources) { | ||
| Write-Output "DSC resources or classes have been modified. DSC resource integration tests will run." | ||
| Write-Output "Changed DSC resources/classes:" | ||
| $changedDscResources | ForEach-Object { Write-Output " $_" } | ||
| return $true | ||
| } | ||
|
|
||
| # Check if any public commands used by DSC resources are changed | ||
| $changedPublicCommands = $changedFiles | Where-Object { $_ -match '^source/Public/(.+)\.ps1$' } | | ||
| ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension((Split-Path $_ -Leaf)) } | ||
|
|
||
| $affectedCommands = $changedPublicCommands | Where-Object { $_ -in $PublicCommandsUsedByDscResources } | ||
| if ($affectedCommands) { | ||
| Write-Output "Public commands used by DSC resources have been modified. DSC resource integration tests will run." | ||
| Write-Output "Affected commands:" | ||
| $affectedCommands | ForEach-Object { Write-Output " $_" } | ||
| return $true | ||
| } | ||
|
|
||
| # Check if any private functions used by the affected public commands are changed | ||
| $changedPrivateFunctions = $changedFiles | Where-Object { $_ -match '^source/Private/(.+)\.ps1$' } | | ||
| ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension((Split-Path $_ -Leaf)) } | ||
|
|
||
| $affectedPrivateFunctions = @() | ||
| foreach ($command in $PublicCommandsUsedByDscResources) { | ||
| $privateFunctionsUsed = Get-PrivateFunctionsUsedByCommand -CommandName $command -SourcePath $SourcePath | ||
| $affectedPrivateFunctions += $privateFunctionsUsed | Where-Object { $_ -in $changedPrivateFunctions } | ||
| } | ||
|
|
||
| if ($affectedPrivateFunctions) { | ||
| Write-Output "Private functions used by DSC resource-related public commands have been modified. DSC resource integration tests will run." | ||
| Write-Output "Affected private functions:" | ||
| $affectedPrivateFunctions | ForEach-Object { Write-Output " $_" } | ||
| return $true | ||
| } | ||
|
|
||
| # Check if integration test files themselves are changed | ||
| $changedIntegrationTests = $changedFiles | Where-Object { $_ -match '^tests/Integration/Resources/' } | ||
| if ($changedIntegrationTests) { | ||
| Write-Output "DSC resource integration test files have been modified. DSC resource integration tests will run." | ||
| Write-Output "Changed integration test files:" | ||
| $changedIntegrationTests | ForEach-Object { Write-Output " $_" } | ||
| return $true | ||
| } | ||
|
|
||
| # Check if pipeline configuration is changed | ||
| $changedPipelineFiles = $changedFiles | Where-Object { $_ -match 'azure-pipelines\.yml$|\.build/' } | ||
| if ($changedPipelineFiles) { | ||
| Write-Output "Pipeline configuration has been modified. DSC resource integration tests will run." | ||
| Write-Output "Changed pipeline files:" | ||
| $changedPipelineFiles | ForEach-Object { Write-Output " $_" } | ||
| return $true | ||
| } | ||
|
|
||
| Write-Output "No changes detected that would affect DSC resources. DSC resource integration tests can be skipped." | ||
| return $false | ||
| } | ||
|
|
||
| # If script is run directly (not imported), execute the main function | ||
| if ($MyInvocation.InvocationName -ne '.') { | ||
| $shouldRun = Test-ShouldRunDscResourceIntegrationTests -BaseBranch $BaseBranch -CurrentBranch $CurrentBranch | ||
|
|
||
| # Output result for Azure DevOps variables | ||
| Write-Output "##vso[task.setvariable variable=ShouldRunDscResourceIntegrationTests]$shouldRun" | ||
|
|
||
| # Also output as regular output for local testing | ||
| Write-Output "ShouldRunDscResourceIntegrationTests: $shouldRun" | ||
|
|
||
| # Set exit code based on result for script usage | ||
| if ($shouldRun) { | ||
| exit 0 | ||
| } else { | ||
| exit 1 | ||
| } | ||
| } | ||
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| .vscode | ||
| .vs | ||
| output/ | ||
| *.test | ||
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.