我尝试过
trigger:
branches:
include:
${{ if flavour == 'staging' }}:
- staging
${{ else }}:
- master
但是 Azure 返回以下错误:
/devops/build&upload.yml(行:4,列:7):不需要映射
我想要实现的结果是根据 flavour 变量的值来触发。它可以设置为 staging,在这种情况下,我希望 include 的值为 staging,或者它可以具有其他值,在这种情况下,我希望 include 具有值 master。
触发器块不能包含变量或模板表达式。
您可以查看链接了解详情。
如果您要根据风味变量的值触发,作为替代方案,您可以:
UI
来控制触发器,就像使用yaml管道一样,它可以在分支之间定义不同的触发器。new
yaml 示例:
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
分行更新:
这里我在其余API中使用了
system.accestoken
,请确保在target pipeline
上,build service account
具有edit pipeline
权限,否则其余API将被拒绝。