Search code examples
azure-devopscontinuous-integrationazure-pipelinesmultibranch-pipeline

How to specify a refrence yaml file in specific branch for all builds in Azure DevOps?


In my current organization, the needed is to be able to build and release to specific environment for any branch that got updated.

This required my YAML build and release file to be pushed to all branches.

However I'm worried about if I want to update my YAML file, so I should do this on each branch for the project which is not funny at all.

So I thought of having only a single branch which contains the defined steps for build and deploy, and all other branches should be invoking that YAML file from that specific branch in that project.

Is there any way I can do this through YAML? Or any better approach which can help me in modifying the YAML (in case its needed)?

Thanks


Solution

  • You can use templates to store the pipeline steps, jobs and stages that would be reused in multiples places.

    In the same Azure Git repository, on each branch, the YAML file (i.e., azure-pipelines.yml) can only reference template files from the current branch. It cannot reference templates from other branches.

    For your case, you can try to create a repository in the same Azure DevOps project to specifically store the template files on one branch. By this way, the YAML file on each branch of the same repository can reference the templates from the same place. And when you want to update pipeline steps, you just need to update once in the template files. See "Templates - Use other repositories".

    Below is an example as reference.

    • Have a repository (TemplatesYAML) to store the template files on main branch. enter image description here

    • In another repository (DemoYAML), set the YAML pipeline to always reference the templates from the main branch of TemplatesYAML.

    . . .
    
    resources:
      repositories:
      - repository: templatesRepo
        name: TemplatesYAML
        type: git
        ref: refs/heads/main
    
    stages:
    - stage: A
      displayName: 'Stage A'
      jobs:
      - job: A1
        displayName: 'Job A1'
        steps:
        - template: For_DemoYAML/Steps_Temp/insert-step.yml@templatesRepo