I am creating a build server right now and trying to run builds in parallel, however when a build fails I wish to send a global variable flag to tell other build servers to Finnish their build then stop without exiting the whole script half way. To do this I was hoping to use global variables however it doesn't seem to pickup changes to the value when i change it from another script. Here are the three example scripts I have to see if I can get this to work:
Here is the main script that calls the other two scripts in parallel
$global:BUILD_FAILED = $false
function failBuild
{
$global:BUILD_FAILED = $true
}
function buildHasFailed
{
if ($global:BUILD_FAILED -eq $false)
{
return $false
}
else
{
return $true
}
}
$test1 = Start-Job -Name "test1" -FilePath "C:\Users\username\Desktop\Untitled2.ps1"
$test2 = Start-Job -Name "test2" -FilePath "C:\Users\username\Desktop\Untitled3.ps1"
while ($test1.State -eq "Running" -or $test2.State -eq "Running")
{
Receive-Job $test1
Receive-Job $test2
}
Write-Host "Test0 finnish:"
$global:BUILD_FAILED
if (buildHasFailed)
{
Write-Host "Fail"
Exit 1
}
Write-Host "Pass"
Exit 0
And here are the two scripts for testing how to change the value in one script and see a result:
function failBuild
{
$global:BUILD_FAILED = $true
}
function buildHasFailed
{
if ($global:BUILD_FAILED -eq $false)
{
return $false
}
else
{
return $true
}
}
Write-Host "Test2 started, $global:BUILD_FAILED"
buildHasFailed
Write-Host "Test2 sleeping 10s"
Start-Sleep -Milliseconds 10000
Write-Host "Test2 $global:BUILD_FAILED"
buildHasFailed
Exit 0
and:
function failBuild
{
$global:BUILD_FAILED = $true
}
function buildHasFailed
{
if ($global:BUILD_FAILED -eq $false)
{
return $false
}
else
{
return $true
}
}
Write-Host "Test1 started, $global:BUILD_FAILED, sleeping 5s"
Start-Sleep -Milliseconds 5000
failBuild
Write-Host "Test1 ending, Status:"
buildHasFailed
$global:BUILD_FAILED
Exit 0
My current output is:
Test1 started, , sleeping 5s
Test2 started,
True
Test2 sleeping 10s
Test1 ending, Status:
True
True
Test2
True
Test0 finnish:
False
Pass
and I'm expecting after test1 sets $global:BUILD_FAILED to true the rest of the outputs should be false however its not updating.
Any suggestions?