diff --git a/tools/variables/_all.ps1 b/tools/variables/_all.ps1 index cc6e8810..d5423d58 100644 --- a/tools/variables/_all.ps1 +++ b/tools/variables/_all.ps1 @@ -12,9 +12,35 @@ param ( $vars = @{} -Get-ChildItem "$PSScriptRoot\*.ps1" -Exclude "_*" |% { +Get-ChildItem "$PSScriptRoot\*.ps1" -Exclude "_*" | % { Write-Host "Computing $($_.BaseName) variable" - $vars[$_.BaseName] = & $_ + # AzDO variables are stored as environment variables which has a max length of ~32k characters. + if ($_.Length -gt 32000) { + # If the InsertJsonValues script returns a value longer than that, + # write the value to a temp file and use the InsertJsonValuesFile variable that InsertVSPayload understands. + if ($_.BaseName -eq "InsertJsonValues") { + Write-Warning "The script InsertJsonValues.ps1 is longer than 32k characters." + Write-Warning "Writing to a temp file and setting INSERTJSONVALUESFILE to the location of the file." + + $tempDir = "$env:AGENT_TEMPDIRECTORY" + if (-not $tempDir) { + Write-Warning "AGENT_TEMPDIRECTORY environment variable is not set. Defaulting to %TMP% which is set to '$env:TMP'." + $tempDir = $env:TMP + } + + $tmpFile = Join-Path "$tempDir" ("InsertJsonValuesFile_{0}.txt" -f ([guid]::NewGuid())) + Write-Host "Writing contents of InsertJsonValues to file: $tmpFile." + $contents = & $_ + Set-Content -Path $tmpFile -Value $contents + + # Write file to the new parameter. + $vars["InsertJsonValuesFile"] = "$tmpFile" + } else { + Write-Error "The script $($_.BaseName).ps1 is too long (> 32k characters). Skipping setting the environment variable." + } + } else { + $vars[$_.BaseName] = & $_ + } } $vars diff --git a/tools/variables/_define.ps1 b/tools/variables/_define.ps1 index d40e5cf1..d5e7d025 100644 --- a/tools/variables/_define.ps1 +++ b/tools/variables/_define.ps1 @@ -11,21 +11,27 @@ param ( ) -(& "$PSScriptRoot\_all.ps1").GetEnumerator() |% { +(& "$PSScriptRoot\_all.ps1").GetEnumerator() | % { # Always use ALL CAPS for env var names since Azure Pipelines converts variable names to all caps and on non-Windows OS, env vars are case sensitive. $keyCaps = $_.Key.ToUpper() if ((Test-Path "env:$keyCaps") -and (Get-Content "env:$keyCaps")) { Write-Host "Skipping setting $keyCaps because variable is already set to '$(Get-Content env:$keyCaps)'." -ForegroundColor Cyan } else { Write-Host "$keyCaps=$($_.Value)" -ForegroundColor Yellow - if ($env:TF_BUILD) { - # Create two variables: the first that can be used by its simple name and accessible only within this job. - Write-Host "##vso[task.setvariable variable=$keyCaps]$($_.Value)" - # and the second that works across jobs and stages but must be fully qualified when referenced. - Write-Host "##vso[task.setvariable variable=$keyCaps;isOutput=true]$($_.Value)" - } elseif ($env:GITHUB_ACTIONS) { - Add-Content -LiteralPath $env:GITHUB_ENV -Value "$keyCaps=$($_.Value)" + + if ($($_.Value).Length -gt 32000) { + Write-Warning "The value is too long (> 32k characters). Skipping setting the environment variables." + } else { + if ($env:TF_BUILD) { + # Create two variables: the first that can be used by its simple name and accessible only within this job. + Write-Host "##vso[task.setvariable variable=$keyCaps]$($_.Value)" + # and the second that works across jobs and stages but must be fully qualified when referenced. + Write-Host "##vso[task.setvariable variable=$keyCaps;isOutput=true]$($_.Value)" + } elseif ($env:GITHUB_ACTIONS) { + Add-Content -LiteralPath $env:GITHUB_ENV -Value "$keyCaps=$($_.Value)" + } + + Set-Item -LiteralPath "env:$keyCaps" -Value $_.Value } - Set-Item -LiteralPath "env:$keyCaps" -Value $_.Value } }