Pipelines/Cleanup.ps1

function Invoke-Cleanup {
    Param(
        [Parameter(Mandatory=$false)]
        [ValidateSet('Local', 'AzureDevOps', 'GithubActions', 'GitLab')]
        [string] $environment = 'Local',
        [string] $version
    )

    if ($env:APPFOLDER) {
        $baseFolder = Join-Path $env:SYSTEM_DEFAULTWORKINGDIRECTORY $env:APPFOLDER
    } else {
        $baseFolder = $env:SYSTEM_DEFAULTWORKINGDIRECTORY
    }

    if ($env:SETTINGSFILE) {
        $settingsFile = Join-Path $baseFolder $env:SETTINGSFILE
    } else {
        $settingsFile = Join-Path $baseFolder "settings.json"
    }

    Invoke-ReadSettings -environment $environment -version $version -settingsFile $settingsFile
    Install-BcContainerHelper -bcContainerHelperVersion $settings.bcContainerHelperVersion -genericImageName $settings.genericImageName

    $refreshToken = "$($ENV:BcSaasRefreshToken)"
    $environmentName = "$($ENV:EnvironmentName)"
    if ($refreshToken -and $environmentName) {
        $authContext = New-BcAuthContext -refreshToken $refreshToken
        if (Get-BcEnvironments -bcAuthContext $authContext | Where-Object { $_.Name -eq $environmentName -and  $_.type -eq "Sandbox" }) {
            Remove-BcEnvironment -bcAuthContext $authContext -environment $environmentName
        }
    }

    if ("$AgentName" -ne "Hosted Agent" -and "$AgentName" -notlike "Azure Pipelines*") {
        $cleanupMutexName = "Cleanup"
        $cleanupMutex = New-Object System.Threading.Mutex($false, $cleanupMutexName)
        try {
            try {
                if (!$cleanupMutex.WaitOne(1000)) {
                    Write-Host "Waiting for other process to finish cleanup"
                    $cleanupMutex.WaitOne() | Out-Null
                    Write-Host "Other process completed"
                }
            }
            catch [System.Threading.AbandonedMutexException] {
            Write-Host "Other process terminated abnormally"
            }

            Remove-BcContainer -containerName $containerName
            Flush-ContainerHelperCache -KeepDays 2

            Remove-Module BcContainerHelper
            $path = Join-Path $ENV:Temp $containerName
            if (Test-Path $path) {
                Remove-Item $path -Recurse -Force
            }
        }
        finally {
            $cleanupMutex.ReleaseMutex()
        }
    }
}