Question:
If I run this powershell code locally:
1 2 3 4 5 |
$url = "refs/pull/5625/merge" $SourceBranchFromBuild = $url.split('/')[-1] $featureReleaseUrl = "http://$sourceBranchFromBuild.azurewebsites.net" Write-Output $featureReleaseUrl |
The output is:
When I run this code on a Azure Powershell:
1 2 3 4 |
$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:
1 2 3 |
$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
?
Answer:
Azure isn’t adding anything – your write-host is!
Your code is doing this:
1 2 3 4 |
PS> $x = "xxx" PS> write-host "aaa"$x aaa xxx |
but presumably you want
1 2 3 4 |
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):
1 2 |
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:
1 2 |
##vso[task.setvariable variable=prSourceBranchName;]merge |
but it’s actually writing this:
1 2 |
##vso[task.setvariable variable=prSourceBranchName;] merge |
Try switching your code to this (i.e. second quote after the variable name):
1 2 |
Write-Host "##vso[task.setvariable variable=prSourceBranchName;]$SourceBranchFromBuild" |
and it should omit the space in front of the branch name in your url.