Reputation: 55
I have a BRANCH_NAME variable based on the trigger branch which is working great. Now based on my BRANCH_NAME value I have to select a json file. Im using starts with but not sure why my TARGET_ENV is not working
variables:
system.debug: 'false'
${{ if startsWith(variables['Build.SourceBranch'],
'refs/heads/') }}:
BRANCH_NAME: $[replace(variables['Build.SourceBranch'],
'refs/heads/', '')]
${{ if startsWith(variables['Build.SourceBranch'],
'refs/heads/feature') }}:
BRANCH_NAME: $[replace(variables['Build.SourceBranchName'],
'/', '-')]
###
${{ if eq(variables['BRANCH_NAME'], 'dev') }}:
TARGET_ENV: dev
${{ if startsWith(variables['BRANCH_NAME'], 'test') }}:
TARGET_ENV: test
${{ if or( startsWith(variables['BRANCH_NAME'], 'XX'), startsWith(variables['BRANCH_NAME'], 'VV') ) }}:
TARGET_ENV: feature
And the value of TARGET_ENV should be used in a script ( echo $TARGET_ENV )
Upvotes: 0
Views: 2360
Reputation: 115017
The yaml file isn't executes as soon as an element is parted, the yaml file is parted in stages. During the template expansion stage only the variables set at queue time are available.
After the template has been expanded (e.g. the ${{}} blocks have been evaluated the yaml is interpreted top to bottom. So the value of BRANCH_NAME isn't available yet.
- bash: |
echo "##vso[task.setvariable variable=myVar;]foo"
condition: $[ eq(variables['BRANCH_NAME'], 'dev' ]
Or you can put all the logic in a single PowerShell block and let PowerShell evaluate the values of the variables.
- powershell: |
if ($env:BRANCH_NAME -eq 'dev') {
Write-host ##vso[task.setvariable variable=TARGET_ENV;]dev
}
...
...
This variable will only be available in the job the task runs. You can make it an output variable to make the task available in other jobs, but then you need to refer to it by its long name:
Dependencies.jobname.stepname.outputvariable
See also:
Upvotes: 2