Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions tools/variables/_all.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than special case InsertJsonValues in this script (which should be general purpose), I think converting the value to a .txt file should be done at insertion time where it has to deal with this variable specifically.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you're saying that the check (and converting it to a text file) should be in _define.ps1 ?


$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."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a warning rather than an error.

}
} else {
$vars[$_.BaseName] = & $_
}
}

$vars
24 changes: 15 additions & 9 deletions tools/variables/_define.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading