添加选择分支的条件以在 Azure 管道中获取 yaml 模板

Adding condition for selecting branch to fetch the yaml template in Azure pipelines

我在名为 'DevOps' 的单独存储库中为 .Net CI CD 管道添加了 yaml 模板。每个 .Net 服务管道都从 'DevOps'.

调用这个 yaml 模板

在 DevOps 中,我有两个分支 - 一个是主要分支,一个是测试版。

.Net 服务管道中 source/development 个分支的所有触发器都应使用 'DevOps' beta 分支中的模板。来自 .Net 服务管道中主分支的管道触发器应使用 main 中的模板。-这是要求。

我使用 expression/if condition/syntax 和 when/if,所有这些要么抛出 expression/condition 错误,要么只是从 main 中获取模板。下面是我做过的各种尝试的截图(老的有评论)

有没有办法在存储库语法中实现条件?

更新: 根据 Kotaro 的解决方案,我已经像下面这样更新了管道。我希望可以在资源语法中添加变量

但这就是我尝试 运行 管道时得到的结果:

没有。在这里你不能使用表达式。恐怕这里连运行时参数都不允许。

您不能动态更改分支。

创建管道并使用 REST 调用它API。

来源尚未验证。

variables:
  devOpsOrg: https://dev.azure.com/{ devOpsname } #  https://dev.azure.com/{ devOpsname }/{project name}/
  devopsProject: {project name}
  DevOpspipelineId: {id} # https://dev.azure.com/{ devOpsname } #  https://dev.azure.com/_build?definitionId={id}&_a=summary
  ${{ if ne(variables['Build.SourceBranchName'], 'main') }}:
    DevOpsbranchName: "beta"
  ${{ if eq(variables['Build.SourceBranch'], 'main') }}:
    DevOpsbranchName: "main"

steps:
- template: devops-login-pipelines.yml
  parameters:
    devopsOrg: $(devopsOrg)
    devopsProject: $(devopsProject)

- script: |
    PIPELINE_ID=`az pipelines list --query "[?name == '$PIPELINE_NAME'].id | [0]"`  
    PIPELINES=`az pipelines show --id $PIPELINE_ID -o json`

    echo $PIPELINES | jq '.triggers[0].branchFilters |= '.+'["+${DEV_OPS_BRANCH_NAME}"]' \
        > pipeline.json

    az devops invoke --http-method PUT --area build \
        --resource definitions \
        --route-parameters project=$DEV_OPS_PROJECT definitionId=$PIPELINE_ID \
        --query-parameters branchName=develop \
        -o json \
        --in-file pipeline.json
    fi
env:
    DEV_OPS_PROJECT: $(devopsProject)
    PIPELINE_ID: $(DevOpspipelineId)
    DEV_OPS_BRANCH_NAME: $(DevOpsbranchName)

devops-登录-pipelines.yml

parameters:
  devopsOrg: 'https://dev.azure.com/xxxxxxx'
  devopsProject: 'xxxxxxxx'

steps:
  # Updating the python version available on the linux agent
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.x'
      architecture: 'x64'
  
  # Updating pip to latest
  - script: python -m pip install --force-reinstall --upgrade pip
    displayName: 'Upgrade pip'
  
  # Updating to latest Azure CLI version.
  - script: pip install azure-cli==2.5.1
    displayName: 'upgrade azure cli'
  
  - script: az --version
    displayName: 'Show Azure CLI version'
  
  - script: az extension add -n azure-devops
    displayName: 'Install Azure DevOps Extension'
  
  - script: echo ${AZURE_DEVOPS_CLI_PAT} | az devops login
    env:
      AZURE_DEVOPS_CLI_PAT: $(System.AccessToken)
      DEV_OPS_ORG: ${{parameters.devopsOrg}}
    displayName: 'Login Azure DevOps Extension'
  
  - script: |
      az devops configure --defaults organization=${DEV_OPS_ORG} project=${DEV_OPS_PROJECT} \
                          --use-git-aliases true
    displayName: 'setting configure'
    env:
      DEV_OPS_ORG: ${{parameters.devopsOrg}}
      DEV_OPS_PROJECT: ${{parameters.devopsProject}}
  

这个怎么样?

trigger: none


pr: none

resources:
  repositories:
  - repository: main
    type: git
    ref: main
    name: xxxxx/DevOps

  - repository: beta
    type: git
    ref: beta
    name: xxxxx/DevOps
    
steps:
  - ${{ if ne(variables['Build.SourceBranchName'], 'main') }}:
    - template: aaaaa.yml@beta
  - ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
    - template: aaaaa.yml@main