File size: 43,270 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
param()
Set-StrictMode -Version 3.0
$ErrorActionPreference = 'continue'
$repoRoot = Join-Path $PSScriptRoot '..'
$script:administratorsGroupSID = "S-1-5-32-544"
$script:usersGroupSID = "S-1-5-32-545"
# set .NET path
$dotNetPath = "$env:USERPROFILE\Appdata\Local\Microsoft\dotnet"
if(Test-Path $dotNetPath)
{
$env:PATH = $dotNetPath + ';' + $env:PATH
}
# import build into the global scope so it can be used by packaging
# argumentList $true says ignore tha we may not be able to build
Write-Verbose "Importing build.psm1" -Verbose
Import-Module (Join-Path $repoRoot 'build.psm1') -Scope Global -ArgumentList $true
$buildCommands = Get-Command -Module build
Write-Verbose "Imported build.psm1 commands: $($buildCommands.Count)" -Verbose
Write-Verbose "Importing packaging.psm1" -Verbose
Import-Module (Join-Path $repoRoot 'tools\packaging') -Scope Global
$packagingCommands = Get-Command -Module packaging
Write-Verbose "Imported packaging.psm1 commands: $($packagingCommands.Count)" -Verbose
# import the windows specific functcion only in Windows PowerShell or on Windows
if($PSVersionTable.PSEdition -eq 'Desktop' -or $IsWindows)
{
Import-Module (Join-Path $PSScriptRoot 'WindowsCI.psm1') -Scope Global
}
# tests if we should run a daily build
# returns true if the build is scheduled
# or is a pushed tag
Function Test-DailyBuild
{
$trueString = 'True'
if(($env:PS_DAILY_BUILD -eq $trueString) -or $env:BUILD_REASON -eq 'Schedule')
{
return $true
}
return $false
}
# Sets a build variable
Function Set-BuildVariable
{
param(
[Parameter(Mandatory=$true)]
[string]
$Name,
[Parameter(Mandatory=$true)]
[string]
$Value,
[switch]
$IsOutput
)
$IsOutputString = if ($IsOutput) { 'true' } else { 'false' }
$command = "vso[task.setvariable variable=$Name;isOutput=$IsOutputString]$Value"
# always log command to make local debugging easier
Write-Verbose -Message "sending command: $command" -Verbose
if ($env:TF_BUILD) {
# In VSTS
Write-Host "##$command"
# The variable will not show up until the next task.
}
# Setting in the current session for the same behavior as the CI and to make it show up in the same task
Set-Item env:/$name -Value $Value
}
# Emulates running all of CI but locally
function Invoke-CIFull
{
param(
[switch] $CleanRepo
)
if($CleanRepo)
{
Clear-PSRepo
}
Invoke-CIInstall
Invoke-CIBuild
Invoke-CITest -ErrorAction Continue
Invoke-CIFinish
}
# Implements the CI 'build_script' step
function Invoke-CIBuild
{
param(
[ValidateSet('Debug', 'Release', 'CodeCoverage', 'StaticAnalysis')]
[string]$Configuration = 'Release'
)
$releaseTag = Get-ReleaseTag
# check to be sure our test tags are correct
$result = Get-PesterTag
if ( $result.Result -ne "Pass" )
{
$result.Warnings
throw "Tags must be CI, Feature, Scenario, or Slow"
}
if(Test-DailyBuild)
{
Start-PSBuild -Configuration 'CodeCoverage' -PSModuleRestore -CI -ReleaseTag $releaseTag
}
Start-PSBuild -PSModuleRestore -Configuration $Configuration -CI -ReleaseTag $releaseTag -UseNuGetOrg
Save-PSOptions
$options = (Get-PSOptions)
$path = Split-Path -Path $options.Output
$psOptionsPath = (Join-Path -Path $PSScriptRoot -ChildPath '../psoptions.json')
$buildZipPath = (Join-Path -Path $PSScriptRoot -ChildPath '../build.zip')
Compress-Archive -Path $path -DestinationPath $buildZipPath
Push-Artifact -Path $psOptionsPath -Name 'build'
Push-Artifact -Path $buildZipPath -Name 'build'
}
# Implements the CI 'install' step
function Invoke-CIInstall
{
param(
[switch]
$SkipUser
)
# Switch to public sources in CI
Switch-PSNugetConfig -Source Public
# Make sure we have all the tags
Sync-PSTags -AddRemoteIfMissing
if(Test-DailyBuild)
{
if ($env:BUILD_REASON -eq 'Schedule')
{
Write-Host "##vso[build.updatebuildnumber]Daily-$env:BUILD_SOURCEBRANCHNAME-$env:BUILD_SOURCEVERSION-$((Get-Date).ToString("yyyyMMddhhss"))"
}
}
if ($env:TF_BUILD -and !$SkipUser.IsPresent)
{
# Generate new credential for CI (only) remoting tests.
Write-Verbose "Creating account for remoting tests in CI." -Verbose
# Password
$randomObj = [System.Random]::new()
$password = ""
1..(Get-Random -Minimum 15 -Maximum 126) | ForEach-Object { $password = $password + [char]$randomObj.next(45,126) }
# Account
$userName = 'ciRemote'
New-LocalUser -username $userName -Password $password
Add-UserToGroup -username $userName -groupSid $script:administratorsGroupSID
# Provide credentials globally for remote tests.
$ss = ConvertTo-SecureString -String $password -AsPlainText -Force
$ciRemoteCredential = [PSCredential]::new("$env:COMPUTERNAME\$userName", $ss)
$ciRemoteCredential | Export-Clixml -Path "$env:TEMP\CIRemoteCred.xml" -Force
# Check that LocalAccountTokenFilterPolicy policy is set, since it is needed for remoting
# using above local admin account.
Write-Log -Message "Checking for LocalAccountTokenFilterPolicy in the CI."
$haveLocalAccountTokenFilterPolicy = $false
try
{
$haveLocalAccountTokenFilterPolicy = ((Get-ItemPropertyValue -Path HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy) -eq 1)
}
# ignore if anything is caught:
catch {}
if (!$haveLocalAccountTokenFilterPolicy)
{
Write-Verbose "Setting the LocalAccountTokenFilterPolicy for remoting tests"
Set-ItemProperty -Path HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy -Value 1
}
}
Set-BuildVariable -Name TestPassed -Value False
}
function Invoke-CIxUnit
{
param(
[switch]
$SkipFailing
)
$env:CoreOutput = Split-Path -Parent (Get-PSOutput -Options (Get-PSOptions))
$path = "$env:CoreOutput\pwsh.exe"
if($IsMacOS -or $IsLinux)
{
$path = "$env:CoreOutput\pwsh"
}
if(!(Test-Path $path))
{
throw "CoreCLR pwsh.exe was not built"
}
$xUnitTestResultsFile = Join-Path -Path $PWD -ChildPath "xUnitTestResults.xml"
Start-PSxUnit -xUnitTestResultsFile $xUnitTestResultsFile
Push-Artifact -Path $xUnitTestResultsFile -name xunit
if(!$SkipFailing.IsPresent)
{
# Fail the build, if tests failed
Test-XUnitTestResults -TestResultsFile $xUnitTestResultsFile
}
}
# Install Pester module if not already installed with a compatible version
function Install-CIPester
{
[CmdletBinding()]
param(
[string]$MinimumVersion = '5.0.0',
[string]$MaximumVersion = '5.99.99',
[switch]$Force
)
Write-Verbose "Checking for Pester module (required: $MinimumVersion - $MaximumVersion)" -Verbose
# Check if a compatible version of Pester is already installed
$installedPester = Get-Module -Name Pester -ListAvailable |
Where-Object { $_.Version -ge $MinimumVersion -and $_.Version -le $MaximumVersion } |
Sort-Object -Property Version -Descending |
Select-Object -First 1
if ($installedPester -and -not $Force) {
Write-Host "Pester version $($installedPester.Version) is already installed and meets requirements" -ForegroundColor Green
return
}
if ($Force) {
Write-Host "Installing Pester module (forced)" -ForegroundColor Yellow
} else {
Write-Host "Installing Pester module" -ForegroundColor Yellow
}
try {
Install-Module -Name Pester -Force -SkipPublisherCheck -MaximumVersion $MaximumVersion -ErrorAction Stop
Write-Host "Successfully installed Pester module" -ForegroundColor Green
}
catch {
Write-Error "Failed to install Pester module: $_"
throw
}
}
# Implement CI 'Test_script'
function Invoke-CITest
{
[CmdletBinding()]
param(
[ValidateSet('UnelevatedPesterTests', 'ElevatedPesterTests')]
[string] $Purpose,
[ValidateSet('CI', 'Others')]
[string] $TagSet,
[string] $TitlePrefix,
[string] $OutputFormat = "NUnitXml"
)
Write-Verbose -Verbose "CI test: OutputFormat: $OutputFormat"
# Set locale correctly for Linux CIs
Set-CorrectLocale
# Pester doesn't allow Invoke-Pester -TagAll@('CI', 'RequireAdminOnWindows') currently
# https://github.com/pester/Pester/issues/608
# To work-around it, we exlude all categories, but 'CI' from the list
switch ($TagSet) {
'CI' {
Write-Host -Foreground Green 'Running "CI" CoreCLR tests..'
$ExcludeTag = @('Slow', 'Feature', 'Scenario')
}
'Others' {
Write-Host -Foreground Green 'Running non-CI CoreCLR tests..'
$ExcludeTag = @('CI')
}
Default {
throw "Unknown TagSet: '$TagSet'"
}
}
if($IsLinux -or $IsMacOS)
{
return Invoke-LinuxTestsCore -Purpose $Purpose -ExcludeTag $ExcludeTag -TagSet $TagSet -TitlePrefix $TitlePrefix -OutputFormat $OutputFormat
}
# CoreCLR
$env:CoreOutput = Split-Path -Parent (Get-PSOutput -Options (Get-PSOptions))
Write-Host -Foreground Green 'Run CoreCLR tests'
$testResultsNonAdminFile = "$PWD\TestsResultsNonAdmin-$TagSet.xml"
$testResultsAdminFile = "$PWD\TestsResultsAdmin-$TagSet.xml"
if(!(Test-Path "$env:CoreOutput\pwsh.exe"))
{
throw "CoreCLR pwsh.exe was not built"
}
# Get the experimental feature names and the tests associated with them
$ExperimentalFeatureTests = Get-ExperimentalFeatureTests
if ($Purpose -eq 'UnelevatedPesterTests') {
$unelevate = $true
$environment = Get-EnvironmentInformation
if ($environment.OSArchitecture -eq 'arm64') {
Write-Verbose -Verbose "running on arm64, running unelevated tests as elevated"
$unelevate = $false
}
$arguments = @{
Bindir = $env:CoreOutput
OutputFile = $testResultsNonAdminFile
Unelevate = $unelevate
Terse = $true
Tag = @()
ExcludeTag = $ExcludeTag + 'RequireAdminOnWindows'
OutputFormat = $OutputFormat
}
$title = "Pester Unelevated - $TagSet"
if ($TitlePrefix) {
$title = "$TitlePrefix - $title"
}
Write-Verbose -Verbose "Starting Pester with output format $($arguments.OutputFormat)"
Start-PSPester @arguments -Title $title
# Fail the build, if tests failed
Test-PSPesterResults -TestResultsFile $testResultsNonAdminFile
# Run tests with specified experimental features enabled
foreach ($entry in $ExperimentalFeatureTests.GetEnumerator())
{
$featureName = $entry.Key
$testFiles = $entry.Value
$expFeatureTestResultFile = "$PWD\TestsResultsNonAdmin.$featureName.xml"
$arguments['OutputFile'] = $expFeatureTestResultFile
$arguments['ExperimentalFeatureName'] = $featureName
if ($testFiles.Count -eq 0) {
# If an empty array is specified for the feature name, we run all tests with the feature enabled.
# This allows us to prevent regressions to a critical engine experimental feature.
$arguments.Remove('Path')
} else {
# If a non-empty string or array is specified for the feature name, we only run those test files.
$arguments['Path'] = $testFiles
}
$title = "Pester Experimental Unelevated - $featureName"
if ($TitlePrefix) {
$title = "$TitlePrefix - $title"
}
# We just built the test tools, we don't need to rebuild them
Write-Verbose -Verbose "Starting Pester with output format $($arguments.OutputFormat)"
Start-PSPester @arguments -Title $title -SkipTestToolBuild
# Fail the build, if tests failed
Test-PSPesterResults -TestResultsFile $expFeatureTestResultFile
}
}
if ($Purpose -eq 'ElevatedPesterTests') {
$arguments = @{
Terse = $true
Bindir = $env:CoreOutput
OutputFile = $testResultsAdminFile
Tag = @('RequireAdminOnWindows')
ExcludeTag = $ExcludeTag
OutputFormat = $OutputFormat
}
$title = "Pester Elevated - $TagSet"
if ($TitlePrefix) {
$title = "$TitlePrefix - $title"
}
Write-Verbose -Verbose "Starting Pester with output format $($arguments.OutputFormat)"
Start-PSPester @arguments -Title $title
# Fail the build, if tests failed
Test-PSPesterResults -TestResultsFile $testResultsAdminFile
# Run tests with specified experimental features enabled
foreach ($entry in $ExperimentalFeatureTests.GetEnumerator())
{
$featureName = $entry.Key
$testFiles = $entry.Value
$expFeatureTestResultFile = "$PWD\TestsResultsAdmin.$featureName.xml"
$arguments['OutputFile'] = $expFeatureTestResultFile
$arguments['ExperimentalFeatureName'] = $featureName
if ($testFiles.Count -eq 0)
{
# If an empty array is specified for the feature name, we run all tests with the feature enabled.
# This allows us to prevent regressions to a critical engine experimental feature.
$arguments.Remove('Path')
}
else
{
# If a non-empty string or array is specified for the feature name, we only run those test files.
$arguments['Path'] = $testFiles
}
$title = "Pester Experimental >levated - $featureName"
if ($TitlePrefix) {
$title = "$TitlePrefix - $title"
}
Write-Verbose -Verbose "Starting Pester with output format $($arguments.OutputFormat)"
# We just built the test tools, we don't need to rebuild them
Start-PSPester @arguments -Title $title -SkipTestToolBuild
# Fail the build, if tests failed
Test-PSPesterResults -TestResultsFile $expFeatureTestResultFile
}
}
Set-BuildVariable -Name TestPassed -Value True
}
function New-CodeCoverageAndTestPackage
{
[CmdletBinding()]
param()
if (Test-DailyBuild)
{
Start-PSBuild -Configuration 'CodeCoverage' -Clean
$codeCoverageOutput = Split-Path -Parent (Get-PSOutput)
$codeCoverageArtifacts = Compress-CoverageArtifacts -CodeCoverageOutput $codeCoverageOutput
$destBasePath = if ($env:TF_BUILD) {
$env:BUILD_ARTIFACTSTAGINGDIRECTORY
} else {
Join-Path (Get-Location).Path "out"
}
if (-not (Test-Path $destBasePath))
{
$null = New-Item -ItemType Directory -Path $destBasePath
}
Write-Host -ForegroundColor Green 'Upload CodeCoverage artifacts'
$codeCoverageArtifacts | ForEach-Object {
Copy-Item -Path $_ -Destination $destBasePath
$newPath = Join-Path $destBasePath (Split-Path $_ -Leaf)
Push-Artifact -Path $newPath -Name 'CodeCoverage'
}
New-TestPackage -Destination $destBasePath
$testPackageFullName = Join-Path $destBasePath 'TestPackage.zip'
Write-Verbose "Created TestPackage.zip" -Verbose
Write-Host -ForegroundColor Green 'Upload test package'
Push-Artifact $testPackageFullName -Name 'CodeCoverage'
}
}
# Wrapper to push artifact
function Push-Artifact
{
param(
[Parameter(Mandatory)]
[ValidateScript({Test-Path -Path $_})]
$Path,
[string]
$Name
)
if(!$Name)
{
$artifactName = [system.io.path]::GetFileName($Path)
}
else
{
$artifactName = $Name
}
if ($env:TF_BUILD) {
# In Azure DevOps
Write-Host "##vso[artifact.upload containerfolder=$artifactName;artifactname=$artifactName;]$Path"
} elseif ($env:GITHUB_WORKFLOW -and $env:RUNNER_WORKSPACE) {
# In GitHub Actions
$destinationPath = Join-Path -Path $env:RUNNER_WORKSPACE -ChildPath $artifactName
# Create the folder if it does not exist
if (!(Test-Path -Path $destinationPath)) {
$null = New-Item -ItemType Directory -Path $destinationPath -Force
}
Copy-Item -Path $Path -Destination $destinationPath -Force -Verbose
} else {
Write-Warning "Push-Artifact is not supported in this environment."
}
}
function Compress-CoverageArtifacts
{
param([string] $CodeCoverageOutput)
# Create archive for test content, OpenCover module and CodeCoverage build
$artifacts = New-Object System.Collections.ArrayList
Add-Type -AssemblyName System.IO.Compression.FileSystem
$resolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath((Join-Path $PSScriptRoot '..\test\tools\OpenCover'))
$zipOpenCoverPath = Join-Path $PWD 'OpenCover.zip'
[System.IO.Compression.ZipFile]::CreateFromDirectory($resolvedPath, $zipOpenCoverPath)
$null = $artifacts.Add($zipOpenCoverPath)
$zipCodeCoveragePath = Join-Path $PWD "CodeCoverage.zip"
Write-Verbose "Zipping ${CodeCoverageOutput} into $zipCodeCoveragePath" -Verbose
[System.IO.Compression.ZipFile]::CreateFromDirectory($CodeCoverageOutput, $zipCodeCoveragePath)
$null = $artifacts.Add($zipCodeCoveragePath)
return $artifacts
}
function Get-ReleaseTag
{
$metaDataPath = Join-Path -Path $PSScriptRoot -ChildPath 'metadata.json'
$metaData = Get-Content $metaDataPath | ConvertFrom-Json
$releaseTag = $metadata.NextReleaseTag
if($env:BUILD_BUILID)
{
$releaseTag = $releaseTag.split('.')[0..2] -join '.'
$releaseTag = $releaseTag + '.' + $env:BUILD_BUILID
}
return $releaseTag
}
# Implements CI 'on_finish' step
function Invoke-CIFinish
{
param(
[string] $Runtime = 'win7-x64',
[string] $Channel = 'preview',
[Validateset('Build','Package')]
[string[]] $Stage = ('Build','Package')
)
# Switch to public sources in CI
Switch-PSNugetConfig -Source Public
if ($PSEdition -eq 'Core' -and ($IsLinux -or $IsMacOS) -and $Stage -contains 'Build') {
return New-LinuxPackage
}
$artifacts = New-Object System.Collections.ArrayList
try {
$buildFolder = "${env:SYSTEM_ARTIFACTSDIRECTORY}/mainBuild"
if ($Stage -contains "Build") {
if ($Channel -eq 'preview') {
$releaseTag = Get-ReleaseTag
$previewVersion = $releaseTag.Split('-')
$previewPrefix = $previewVersion[0]
$previewLabel = $previewVersion[1].replace('.','')
if (Test-DailyBuild) {
$previewLabel = "daily{0}" -f $previewLabel
}
$prereleaseIteration = (get-date).Day
$preReleaseVersion = "$previewPrefix-$previewLabel.$prereleaseIteration"
# Build clean before backing to remove files from testing
Start-PSBuild -PSModuleRestore -Configuration 'Release' -ReleaseTag $preReleaseVersion -Clean -Runtime $Runtime -output $buildFolder -PSOptionsPath "${buildFolder}/psoptions.json"
$options = Get-PSOptions
# Remove symbol files.
$filter = Join-Path -Path (Split-Path $options.Output) -ChildPath '*.pdb'
Write-Verbose "Removing symbol files from $filter" -Verbose
Remove-Item $filter -Force -Recurse
} else {
$releaseTag = Get-ReleaseTag
$releaseTagParts = $releaseTag.split('.')
$preReleaseVersion = $releaseTagParts[0]+ ".9.9"
Write-Verbose "newPSReleaseTag: $preReleaseVersion" -Verbose
Start-PSBuild -PSModuleRestore -Configuration 'Release' -ReleaseTag $preReleaseVersion -Clean -Runtime $Runtime -output $buildFolder -PSOptionsPath "${buildFolder}/psoptions.json"
$options = Get-PSOptions
# Remove symbol files.
$filter = Join-Path -Path (Split-Path $options.Output) -ChildPath '*.pdb'
Write-Verbose "Removing symbol files from $filter" -Verbose
Remove-Item $filter -Force -Recurse
}
# Set a variable, both in the current process and in AzDevOps for the packaging stage to get the release tag
$env:CI_FINISH_RELASETAG=$preReleaseVersion
$vstsCommandString = "vso[task.setvariable variable=CI_FINISH_RELASETAG]$preReleaseVersion"
Write-Verbose -Message "$vstsCommandString" -Verbose
Write-Host -Object "##$vstsCommandString"
}
if ($Stage -contains "Package") {
Restore-PSOptions -PSOptionsPath "${buildFolder}/psoptions.json"
$preReleaseVersion = $env:CI_FINISH_RELASETAG
# Build packages
$packageTypes = 'zip', 'zip-pdb', 'msix'
$packages = Start-PSPackage -Type $packageTypes -ReleaseTag $preReleaseVersion -SkipReleaseChecks -WindowsRuntime $Runtime
foreach ($package in $packages) {
if (Test-Path $package -ErrorAction Ignore) {
Write-Log "Package found: $package"
} else {
Write-Warning -Message "Package NOT found: $package"
}
if ($package -is [string]) {
$null = $artifacts.Add($package)
}
}
}
} catch {
Get-Error -InputObject $_
throw
} finally {
$pushedAllArtifacts = $true
$artifacts | ForEach-Object {
Write-Log -Message "Pushing $_ as CI artifact"
if (Test-Path $_) {
Push-Artifact -Path $_ -Name 'artifacts'
} else {
$pushedAllArtifacts = $false
Write-Warning "Artifact $_ does not exist."
}
}
if (!$pushedAllArtifacts) {
throw "Some artifacts did not exist!"
}
}
}
function Set-Path
{
param
(
[Parameter(Mandatory)]
[string]
$Path,
[Parameter(Mandatory)]
[switch]
$Append
)
$machinePathString = [System.Environment]::GetEnvironmentVariable('path',[System.EnvironmentVariableTarget]::Machine)
$machinePath = $machinePathString -split ';'
if($machinePath -inotcontains $path)
{
$newPath = "$machinePathString;$path"
Write-Verbose "Adding $path to path..." -Verbose
[System.Environment]::SetEnvironmentVariable('path',$newPath,[System.EnvironmentVariableTarget]::Machine)
Write-Verbose "Added $path to path." -Verbose
}
else
{
Write-Verbose "$path already in path." -Verbose
}
}
# Display environment variables in a log group for GitHub Actions
function Show-Environment
{
Write-LogGroupStart -Title 'Environment'
Get-ChildItem -Path env: | Out-String -width 9999 -Stream | Write-Verbose -Verbose
Write-LogGroupEnd -Title 'Environment'
}
# Bootstrap script for Linux and macOS
function Invoke-BootstrapStage
{
$createPackages = Test-DailyBuild
Write-Log -Message "Executing ci.psm1 Bootstrap Stage"
# Make sure we have all the tags
Sync-PSTags -AddRemoteIfMissing
Start-PSBootstrap -Scenario Package:$createPackages
}
# Run pester tests for Linux and macOS
function Invoke-LinuxTestsCore
{
[CmdletBinding()]
param(
[ValidateSet('UnelevatedPesterTests', 'ElevatedPesterTests', 'All')]
[string] $Purpose = 'All',
[string[]] $ExcludeTag = @('Slow', 'Feature', 'Scenario'),
[string] $TagSet = 'CI',
[string] $TitlePrefix,
[string] $OutputFormat = "NUnitXml"
)
$output = Split-Path -Parent (Get-PSOutput -Options (Get-PSOptions))
$testResultsNoSudo = "$PWD/TestResultsNoSudo.xml"
$testResultsSudo = "$PWD/TestResultsSudo.xml"
$testExcludeTag = $ExcludeTag + 'RequireSudoOnUnix'
$pesterPassThruNoSudoObject = $null
$pesterPassThruSudoObject = $null
$noSudoResultsWithExpFeatures = $null
$sudoResultsWithExpFeatures = $null
$noSudoPesterParam = @{
'BinDir' = $output
'PassThru' = $true
'Terse' = $true
'Tag' = @()
'ExcludeTag' = $testExcludeTag
'OutputFile' = $testResultsNoSudo
'OutputFormat' = $OutputFormat
}
# Get the experimental feature names and the tests associated with them
$ExperimentalFeatureTests = Get-ExperimentalFeatureTests
# Running tests which do not require sudo.
if($Purpose -eq 'UnelevatedPesterTests' -or $Purpose -eq 'All')
{
$title = "Pester No Sudo - $TagSet"
if ($TitlePrefix) {
$title = "$TitlePrefix - $title"
}
$pesterPassThruNoSudoObject = Start-PSPester @noSudoPesterParam -Title $title
# Running tests that do not require sudo, with specified experimental features enabled
$noSudoResultsWithExpFeatures = @()
foreach ($entry in $ExperimentalFeatureTests.GetEnumerator()) {
$featureName = $entry.Key
$testFiles = $entry.Value
$expFeatureTestResultFile = "$PWD\TestResultsNoSudo.$featureName.xml"
$noSudoPesterParam['OutputFile'] = $expFeatureTestResultFile
$noSudoPesterParam['ExperimentalFeatureName'] = $featureName
if ($testFiles.Count -eq 0) {
# If an empty array is specified for the feature name, we run all tests with the feature enabled.
# This allows us to prevent regressions to a critical engine experimental feature.
$noSudoPesterParam.Remove('Path')
}
else
{
# If a non-empty string or array is specified for the feature name, we only run those test files.
$noSudoPesterParam['Path'] = $testFiles
}
$title = "Pester Experimental No Sudo - $featureName - $TagSet"
if ($TitlePrefix) {
$title = "$TitlePrefix - $title"
}
$passThruResult = Start-PSPester @noSudoPesterParam -Title $title -SkipTestToolBuild
$noSudoResultsWithExpFeatures += $passThruResult
}
}
# Running tests, which require sudo.
if($Purpose -eq 'ElevatedPesterTests' -or $Purpose -eq 'All')
{
$sudoPesterParam = $noSudoPesterParam.Clone()
$sudoPesterParam.Remove('Path')
$sudoPesterParam['Tag'] = @('RequireSudoOnUnix')
$sudoPesterParam['ExcludeTag'] = $ExcludeTag
$sudoPesterParam['Sudo'] = $true
$sudoPesterParam['OutputFile'] = $testResultsSudo
$sudoPesterParam['OutputFormat'] = $OutputFormat
$title = "Pester Sudo - $TagSet"
if ($TitlePrefix) {
$title = "$TitlePrefix - $title"
}
$pesterPassThruSudoObject = Start-PSPester @sudoPesterParam -Title $title
# Running tests that require sudo, with specified experimental features enabled
$sudoResultsWithExpFeatures = @()
foreach ($entry in $ExperimentalFeatureTests.GetEnumerator()) {
$featureName = $entry.Key
$testFiles = $entry.Value
$expFeatureTestResultFile = "$PWD\TestResultsSudo.$featureName.xml"
$sudoPesterParam['OutputFile'] = $expFeatureTestResultFile
$sudoPesterParam['ExperimentalFeatureName'] = $featureName
if ($testFiles.Count -eq 0)
{
# If an empty array is specified for the feature name, we run all tests with the feature enabled.
# This allows us to prevent regressions to a critical engine experimental feature.
$sudoPesterParam.Remove('Path')
}
else
{
# If a non-empty string or array is specified for the feature name, we only run those test files.
$sudoPesterParam['Path'] = $testFiles
}
$title = "Pester Experimental Sudo - $featureName - $TagSet"
if ($TitlePrefix) {
$title = "$TitlePrefix - $title"
}
# We just built the test tools for the main test run, we don't need to rebuild them
$passThruResult = Start-PSPester @sudoPesterParam -Title $title -SkipTestToolBuild
$sudoResultsWithExpFeatures += $passThruResult
}
}
# Determine whether the build passed
try {
$allTestResultsWithNoExpFeature = @($pesterPassThruNoSudoObject, $pesterPassThruSudoObject)
$allTestResultsWithExpFeatures = @($noSudoResultsWithExpFeatures, $sudoResultsWithExpFeatures)
# This throws if there was an error:
$allTestResultsWithNoExpFeature | Where-Object {$null -ne $_} | ForEach-Object { Test-PSPesterResults -ResultObject $_ }
$allTestResultsWithExpFeatures | Where-Object {$null -ne $_} | ForEach-Object { Test-PSPesterResults -ResultObject $_ -CanHaveNoResult }
$result = "PASS"
} catch {
# The build failed, set the result:
$resultError = $_
$result = "FAIL"
}
# If the tests did not pass, throw the reason why
if ( $result -eq "FAIL" )
{
Write-Warning "Tests failed. See the issue below."
Throw $resultError
}
else
{
Write-Verbose "Tests did not fail! Nice job!"
}
}
function New-LinuxPackage
{
$isFullBuild = Test-DailyBuild
$releaseTag = Get-ReleaseTag
$packageParams = @{}
$packageParams += @{ReleaseTag=$releaseTag}
# Only build packages for PowerShell/PowerShell repository
# branches, not pull requests
$packages = @(Start-PSPackage @packageParams -SkipReleaseChecks -Type deb, rpm, rpm-fxdependent-arm64, tar)
foreach($package in $packages)
{
if (Test-Path $package)
{
Write-Log "Package found: $package"
}
else
{
Write-Error -Message "Package NOT found: $package"
}
if ($package -isnot [System.IO.FileInfo])
{
$packageObj = Get-Item $package
Write-Error -Message "The PACKAGE is not a FileInfo object"
}
else
{
$packageObj = $package
}
# Determine artifacts directory (GitHub Actions or Azure DevOps)
$artifactsDir = if ($env:GITHUB_ACTIONS -eq 'true') {
"${env:GITHUB_WORKSPACE}/../packages"
} else {
"${env:BUILD_ARTIFACTSTAGINGDIRECTORY}"
}
# Ensure artifacts directory exists
if (-not (Test-Path $artifactsDir)) {
New-Item -ItemType Directory -Path $artifactsDir -Force | Out-Null
}
Write-Log -message "Artifacts directory: $artifactsDir"
Copy-Item $packageObj.FullName -Destination $artifactsDir -Force
}
if ($IsLinux)
{
# Determine artifacts directory (GitHub Actions or Azure DevOps)
$artifactsDir = if ($env:GITHUB_ACTIONS -eq 'true') {
"${env:GITHUB_WORKSPACE}/../packages"
} else {
"${env:BUILD_ARTIFACTSTAGINGDIRECTORY}"
}
# Create and package Raspbian .tgz
# Build must be clean for Raspbian
Start-PSBuild -PSModuleRestore -Clean -Runtime linux-arm -Configuration 'Release'
$armPackage = Start-PSPackage @packageParams -Type tar-arm -SkipReleaseChecks
Copy-Item $armPackage -Destination $artifactsDir -Force
}
}
function Invoke-InitializeContainerStage {
param(
[string]
$ContainerPattern = '.'
)
Write-Verbose "Invoking InitializeContainerStage with ContainerPattern: ${ContainerPattern}" -Verbose
$fallbackSeed = (get-date).DayOfYear
Write-Verbose "Fall back seed: $fallbackSeed" -Verbose
# For PRs set the seed to the PR number so that the image is always the same
$seed = $env:SYSTEM_PULLREQUEST_PULLREQUESTID
if(!$seed) {
# for non-PRs use the integer identifier of the build as the seed.
$seed = $fallbackSeed
}
# cut down to 32 bits and keep the most varying parts, which are lower bits
if ($seed -ge [Int32]::MaxValue) {
$seed = [int]($seed -band [int]::MaxValue)
}
Write-Verbose "Seed: $seed" -Verbose
# Get the latest image matrix JSON for preview
$matrix = ./PowerShell-Docker/build.ps1 -GenerateMatrixJson -FullJson -Channel preview | ConvertFrom-Json
# Chose images that are validated or validating, Linux and can be used in CI.
$linuxImages = $matrix.preview |
Where-Object {$_.IsLinux -and $_.UseInCi -and $_.DistributionState -match 'Validat.*' -and $_.JobName -match $ContainerPattern -and $_.JobName -notlike "*arm*"} |
Select-Object JobName, Taglist |
Sort-Object -property JobName
# Use the selected seed to pick a container
$index = Get-Random -Minimum 0 -Maximum $linuxImages.Count -SetSeed $seed
$selectedImage = $linuxImages[$index]
# Filter to the first test-deps compatible tag
$tag = $selectedImage.Taglist -split ';' | Where-Object {$_ -match 'preview-\D+'} | Select-Object -First 1
# Calculate the container name
$containerName = "mcr.microsoft.com/powershell/test-deps:$tag"
Set-BuildVariable -Name containerName -Value $containerName -IsOutput
Set-BuildVariable -Name containerBuildName -Value $selectedImage.JobName -IsOutput
if($env:BUILD_REASON -eq 'PullRequest') {
Write-Host "##vso[build.updatebuildnumber]PR-${env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}-$($selectedImage.JobName)-$((get-date).ToString("yyyyMMddhhmmss"))"
} else {
Write-Host "##vso[build.updatebuildnumber]${env:BUILD_SOURCEBRANCHNAME}-$($selectedImage.JobName)-${env:BUILD_SOURCEVERSION}-$((get-date).ToString("yyyyMMddhhmmss"))"
# Cannot do this for a PR
Write-Host "##vso[build.addbuildtag]$($selectedImage.JobName)"
}
}
Function Test-MergeConflictMarker
{
<#
.SYNOPSIS
Checks files for Git merge conflict markers and outputs results for GitHub Actions.
.DESCRIPTION
Scans the specified files for Git merge conflict markers (<<<<<<<, =======, >>>>>>>)
and generates console output, GitHub Actions outputs, and job summary.
Designed for use in GitHub Actions workflows.
.PARAMETER File
Array of file paths (relative or absolute) to check for merge conflict markers.
.PARAMETER WorkspacePath
Base workspace path for resolving relative paths. Defaults to current directory.
.PARAMETER OutputPath
Path to write GitHub Actions outputs. Defaults to $env:GITHUB_OUTPUT.
.PARAMETER SummaryPath
Path to write GitHub Actions job summary. Defaults to $env:GITHUB_STEP_SUMMARY.
.EXAMPLE
Test-MergeConflictMarker -File @('file1.txt', 'file2.cs') -WorkspacePath $env:GITHUB_WORKSPACE
#>
[CmdletBinding()]
param(
[Parameter()]
[AllowEmptyCollection()]
[string[]] $File = @(),
[Parameter()]
[string] $WorkspacePath = $PWD,
[Parameter()]
[string] $OutputPath = $env:GITHUB_OUTPUT,
[Parameter()]
[string] $SummaryPath = $env:GITHUB_STEP_SUMMARY
)
Write-Host "Starting merge conflict marker check..." -ForegroundColor Cyan
# Helper function to write outputs when no files to check
function Write-NoFilesOutput {
param(
[string]$Message,
[string]$OutputPath,
[string]$SummaryPath
)
# Output results to GitHub Actions
if ($OutputPath) {
"files-checked=0" | Out-File -FilePath $OutputPath -Append -Encoding utf8
"conflicts-found=0" | Out-File -FilePath $OutputPath -Append -Encoding utf8
}
# Create GitHub Actions job summary
if ($SummaryPath) {
$summaryContent = @"
# Merge Conflict Marker Check Results
## Summary
- **Files Checked:** 0
- **Files with Conflicts:** 0
## ℹ️ No Files to Check
$Message
"@
$summaryContent | Out-File -FilePath $SummaryPath -Encoding utf8
}
}
# Handle empty file list (e.g., when PR only deletes files)
if ($File.Count -eq 0) {
Write-Host "No files to check (empty file list)" -ForegroundColor Yellow
Write-NoFilesOutput -Message "No files were provided for checking (this can happen when a PR only deletes files)." -OutputPath $OutputPath -SummaryPath $SummaryPath
return
}
# Filter out *.cs files from merge conflict checking
$filesToCheck = @($File | Where-Object { $_ -notlike "*.cs" })
$filteredCount = $File.Count - $filesToCheck.Count
if ($filteredCount -gt 0) {
Write-Host "Filtered out $filteredCount *.cs file(s) from merge conflict checking" -ForegroundColor Yellow
}
if ($filesToCheck.Count -eq 0) {
Write-Host "No files to check after filtering (all files were *.cs)" -ForegroundColor Yellow
Write-NoFilesOutput -Message "All $filteredCount file(s) were filtered out (*.cs files are excluded from merge conflict checking)." -OutputPath $OutputPath -SummaryPath $SummaryPath
return
}
Write-Host "Checking $($filesToCheck.Count) changed files for merge conflict markers" -ForegroundColor Cyan
# Convert relative paths to absolute paths for processing
$absolutePaths = $filesToCheck | ForEach-Object {
if ([System.IO.Path]::IsPathRooted($_)) {
$_
} else {
Join-Path $WorkspacePath $_
}
}
$filesWithConflicts = @()
$filesChecked = 0
foreach ($filePath in $absolutePaths) {
# Check if file exists (might be deleted)
if (-not (Test-Path $filePath)) {
Write-Verbose " Skipping deleted file: $filePath"
continue
}
# Skip binary files and directories
if ((Get-Item $filePath) -is [System.IO.DirectoryInfo]) {
continue
}
$filesChecked++
# Get relative path for display
$relativePath = if ($WorkspacePath -and $filePath.StartsWith($WorkspacePath)) {
$filePath.Substring($WorkspacePath.Length).TrimStart([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
} else {
$filePath
}
Write-Host " Checking: $relativePath" -ForegroundColor Gray
# Search for conflict markers using Select-String
try {
# Git conflict markers are 7 characters followed by a space or end of line
# Regex pattern breakdown:
# ^ - Matches the start of a line
# (<{7}|={7}|>{7}) - Matches exactly 7 consecutive '<', '=', or '>' characters (Git conflict markers)
# (\s|$) - Ensures the marker is followed by whitespace or end of line
$pattern = '^(<{7}|={7}|>{7})(\s|$)'
$matchedLines = Select-String -Path $filePath -Pattern $pattern -AllMatches -ErrorAction Stop
if ($matchedLines) {
# Collect marker details with line numbers (Select-String provides LineNumber automatically)
$markerDetails = @()
foreach ($match in $matchedLines) {
$markerDetails += [PSCustomObject]@{
Marker = $match.Matches[0].Groups[1].Value
Line = $match.LineNumber
}
}
$filesWithConflicts += [PSCustomObject]@{
File = $relativePath
MarkerDetails = $markerDetails
}
Write-Host " ❌ CONFLICT MARKERS FOUND in $relativePath" -ForegroundColor Red
foreach ($detail in $markerDetails) {
Write-Host " Line $($detail.Line): $($detail.Marker)" -ForegroundColor Red
}
}
}
catch {
# Skip files that can't be read (likely binary)
Write-Verbose " Skipping unreadable file: $relativePath"
}
}
# Output results to GitHub Actions
if ($OutputPath) {
"files-checked=$filesChecked" | Out-File -FilePath $OutputPath -Append -Encoding utf8
"conflicts-found=$($filesWithConflicts.Count)" | Out-File -FilePath $OutputPath -Append -Encoding utf8
}
Write-Host "`nSummary:" -ForegroundColor Cyan
Write-Host " Files checked: $filesChecked" -ForegroundColor Cyan
Write-Host " Files with conflicts: $($filesWithConflicts.Count)" -ForegroundColor Cyan
# Create GitHub Actions job summary
if ($SummaryPath) {
$summaryContent = @"
# Merge Conflict Marker Check Results
## Summary
- **Files Checked:** $filesChecked
- **Files with Conflicts:** $($filesWithConflicts.Count)
"@
if ($filesWithConflicts.Count -gt 0) {
Write-Host "`n❌ Merge conflict markers detected in the following files:" -ForegroundColor Red
$summaryContent += "`n## ❌ Conflicts Detected`n`n"
$summaryContent += "The following files contain merge conflict markers:`n`n"
foreach ($fileInfo in $filesWithConflicts) {
Write-Host " - $($fileInfo.File)" -ForegroundColor Red
$summaryContent += "### 📄 ``$($fileInfo.File)```n`n"
$summaryContent += "| Line | Marker |`n"
$summaryContent += "|------|--------|`n"
foreach ($detail in $fileInfo.MarkerDetails) {
Write-Host " Line $($detail.Line): $($detail.Marker)" -ForegroundColor Red
$summaryContent += "| $($detail.Line) | ``$($detail.Marker)`` |`n"
}
$summaryContent += "`n"
}
$summaryContent += "`n**Action Required:** Please resolve these conflicts before merging.`n"
Write-Host "`nPlease resolve these conflicts before merging." -ForegroundColor Red
} else {
Write-Host "`n✅ No merge conflict markers found" -ForegroundColor Green
$summaryContent += "`n## ✅ No Conflicts Found`n`nAll checked files are free of merge conflict markers.`n"
}
$summaryContent | Out-File -FilePath $SummaryPath -Encoding utf8
}
# Exit with error if conflicts found
if ($filesWithConflicts.Count -gt 0) {
throw "Merge conflict markers detected in $($filesWithConflicts.Count) file(s)"
}
}
|