I tried with
trigger:
branches:
include:
${{ if flavour == 'staging' }}:
- staging
${{ else }}:
- master
But Azure returns the following error:
/devops/build&upload.yml (Line: 4, Col: 7): A mapping was not expected
The result I want to achieve is to have triggers depending on the value of the flavour variable. It can be set to staging, in which case I want the value of include to be staging, or it can have other values, in which case I want the include to have the value master.
Trigger blocks can't contain variables or template expressions.
You can check the link for the details.
If you would triggers depending on the value of the flavour variable, as an alternative, you can:
UI
trigger to override the yaml trigger, as if you use trigger in yaml, it could have different triggers defined between branches.The new
yaml sample:
pool:
vmImage: ubuntu-latest
variables:
- name: flavour
value: staging
steps:
- ${{ if eq(variables.flavour, 'staging') }}:
- script: echo "##vso[task.setvariable variable=branchToUse;]staging"
- ${{ else }}:
- script: echo "##vso[task.setvariable variable=branchToUse;]main"
- powershell: |
$Organization = "org"
$Project = "project"
$PipelineId = "pipelineid"
$header = @{Authorization = "Bearer $(System.AccessToken)"}
$request = "https://dev.azure.com/$($Organization)/$($Project)/_apis/build/definitions/$($PipelineId)?api-version=7.1-preview.7"
echo $request # check the request url
$json = Invoke-WebRequest -Uri $request -Method Get -Headers $header -ContentType "application/json" | ConvertFrom-Json
# Set Branch Filter
$defaultBranch = "$(branchToUse)"
$branchFilters = @("+refs/heads/$($defaultBranch)")
$json.triggers[0].branchFilters = $branchFilters
echo $json.triggers[0].branchFilters # check the branch value
# Convert PSObject back to Json
$json = $json | ConvertTo-Json -Depth 100
# Update Pipeline
$request = "https://dev.azure.com/$($Organization)/$($Project)/_apis/build/definitions/$($PipelineId)?api-version=7.1-preview.7"
$result = Invoke-RestMethod -Uri $request -Method Put -ContentType "application/json" -Headers $header -Body $json
The branch updated:
Here i used system.accestoken
in the rest api, please make sure on target pipeline
, the build service account
has edit pipeline
permission otherwise the rest api will be denied.