Peter Boomsma
Peter Boomsma

Reputation: 9836

Azure adds leading space to variable

If I run this powershell code locally:

$url = "refs/pull/5625/merge"
$SourceBranchFromBuild = $url.split('/')[-1]
$featureReleaseUrl = "http://$sourceBranchFromBuild.azurewebsites.net"
Write-Output $featureReleaseUrl

The output is:

http://merge.azurewebsites.net

When I run this code on a Azure Powershell:

$url = "refs/pull/5625/merge"
$SourceBranchFromBuild = $url.split('/')[-1]
Write-Host "##vso[task.setvariable variable=prSourceBranchName;]"$SourceBranchFromBuild

And then create the URL in another Azure Powershell script:

$featureReleaseUrl = "http://$env:prSourceBranchName.azurewebsites.net"
Write-Output $featureReleaseUrl

The ouput is

http:// merge.azurewebsites.net

What's causing this leading space character in the $env:prSourceBranchName?

Upvotes: 5

Views: 1686

Answers (2)

mclayton
mclayton

Reputation: 10075

Azure isn't adding anything - your write-host is!

Your code is doing this:

PS> $x = "xxx"
PS> write-host "aaa"$x
aaa xxx

but presumably you want

PS> $x = "xxx"
PS> write-host "aaa$x"
aaaxxx

Note where the second quote is the write-host in both examples. In the first it's before the $x variable name. In the second it's after.

In your question it's calling this (with the quote before the variable name):

Write-Host "##vso[task.setvariable variable=prSourceBranchName;]"$SourceBranchFromBuild

which will write a logging command to the log file, and Azure DevOps will process that and update the environment variable.

You're probably expecting it to write this to the log file:

##vso[task.setvariable variable=prSourceBranchName;]merge

but it's actually writing this:

##vso[task.setvariable variable=prSourceBranchName;] merge

Try switching your code to this (i.e. second quote after the variable name):

Write-Host "##vso[task.setvariable variable=prSourceBranchName;]$SourceBranchFromBuild"

and it should omit the space in front of the branch name in your url.

Upvotes: 7

AmanGarg-MSFT
AmanGarg-MSFT

Reputation: 1153

@PeterBoomsma Try putting $SourceBranchFromBuild inside the double quotes like this:

Write-Host "##vso[task.setvariable variable=prSourceBranchName;]$SourceBranchFromBuild"

Upvotes: 0

Related Questions