How do handle PRs for Azure DevOps YAML Pipelines if the YAML templates are in a different repo?
Azure DevOps YAML base pipelines allow the pipeline definitions to be treated like any other code. So you make changes in a branch and PR them into the main/trunk when they are approved.
This works well if all the YAML files are in the same repo, but not so well if you are using YAML templates and the templated YAML is stored in a different repo. This is because an Azure DevOps PR is limited to a single repo. So testing a change to a YAML template in a different repo needs a bit of thought.
Say for example you have a template called core.yml in a repo called YAMLTemplates and you make a change to it and start a PR. Unless you have a test YAML pipeline in that repo, which is not a stupid idea, but not always possible depending on the complexity of your build process, there is no way to test the change inside that repo.
The answer is to create a temporary branch in a repo that consumes the shared YAML template. In this temporary branch make an edit to the repository setting that references the shared YAML repo to point to the update branch contain the PR
resources:
repositories:
- repository: YAMLTemplates
type: git
name: 'Git Project/YAMLTemplates'
# defaults to ref: 'refs/heads/master'
ref: 'refs/heads/newbranch'
You don't need to make any change to the line where the template is used
extends:
template: core.yml@YAMLTemplates
parameters:
customer: ${{parameters.Customer}}
useSonarQube: ${{parameters.useSonarQube}}
You can then use this updated pipeline to validated your PR. Once you are happy it works you can
- Complete the PR in the YAML Templates repo
- Delete the temporary branch in your consuming repo.