5

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

Asterix
  • 331
  • 6
  • 22

3 Answers3

7

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

Kotaro Inoue
  • 126
  • 4
1

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

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
1

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}}
  
Kotaro Inoue
  • 126
  • 4
  • Thank you @Kotaro. I'll try this and revert by early next week – Asterix Sep 17 '21 at 21:26
  • I have updated the question with the details of the changes I have made according to your solution and the error I have received. Kindly let me know where I have gone wrong. P.S: I am trying to avoid scripting section maximum here. thats why modified the solution – Asterix Sep 20 '21 at 07:59
  • I have followed this method which is similar to your solution. https://stackoverflow.com/questions/57532138/can-conditional-variable-assignment-be-done-in-azure-pipelines ; it throws "value is already defined error. I have seen the example set in this documentation as well - https://learn.microsoft.com/en-us/azure/devops/release-notes/2021/sprint-192-update#new-yaml-conditional-expressions. nothing works – Asterix Sep 20 '21 at 13:14
  • Repository tags should not support dynamically changing branches. So you have to do it another way. You should be able to dynamically execute the branch specification by calling it with the API. – Kotaro Inoue Sep 21 '21 at 03:47