I converted Azure release pipeline classic pipeline to yaml with yamlzr Powershell command is failing inside yaml file:
You cannot call a method on a null-valued expression in powershell inside azure release pipeline yaml
$branch = $env:build_sourceBranch.substring($env:build_sourceBranch.indexOf('/', 5) + 1)
I tried to use Build.SourceBranchName built-in variable but not working.
I tried to map it
env:
BUILD_SOURCEBRANCH: $(Build.SourceBranchName)
I got (Line: 162, Col: 13): A mapping was not expected eror.
I can reproduce the issue with your script (lowercase variable) when using Ubuntu-latest
agent in pipeline.
In DevOps Variable names are transformed to uppercase, and the characters ".
" and "
" are replaced by "_
". So the variable should be $env:BUILD_SOURCEBRANCH
. See Define and modify your variables in a script
The problem is that Linux is case sensitive. It cannot identify the lowercase variable $env:build_sourceBranch
in Linux. We need to use The uppercase variable $env:BUILD_SOURCEBRANCH
when using Linux agent (Ubuntu-latest
for example).
So the correct script when using "Ubuntu-latest
" would be like this:
$branch = $env:BUILD_SOURCEBRANCH.substring($env:BUILD_SOURCEBRANCH.indexOf('/', 5) + 1)
Alternately, we can use the Windows-latest
agent with the lowercase
variable $env:build_sourceBranch
.
For example:
trigger: none
pool:
vmImage: Windows-latest
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$branch = $env:build_sourceBranch.substring($env:build_sourceBranch.indexOf('/', 5) + 1)
Write-Host "branch :" $branch