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

Viewed 2469

I have added yaml templates for .Net CI CD pipeline in a separate repository named 'DevOps'. Each .Net service pipeline is calling this yaml template from 'DevOps'.

In DevOps I have two branches - one is main and one is beta.

All the triggers from source/development branches in .Net service pipeline should use the template in beta branch of 'DevOps'. Pipeline trigger from main branch in .Net service pipeline should use the template in main.- this is the requirement.

I have used regular expression/if condition/syntax using when/if, all these are either throwing expression/condition error or simply take the template from main. Below is the screenshot of various attempts (older ones are commented) I have made.

enter image description here

Is there a way to implement a condition in repository syntax?

Update: I have updated pipeline like below as per Kotaro's solution. I hope variable can be added inside resource syntax

enter image description here

But this is what I am getting when I try to run the pipeline:

enter image description here

3 Answers

How about this?

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

No. Here you cannot uses expressions. I'm afraid that even runtime parameters are not allowed here.

You cannot change the branch dynamically.

Create a pipeline and call it using the REST API.

The source has not been verified.

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-login-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}}
  
Related