Azure pipeline reuse variables in template from another repository#
Context#
In my project, I have several Azure pipelines that share some same variables, instead of declaring them in each pipeline, I would like to refactor it by using some central places to store the shared variables.
I can split the variables into 3 groups:
- organization level variables: organization name, tenant id, etc.
- project level variables: project name, resources group name, keyvault name, project support email, etc.
- repository level variables: module name, repository support email, etc.
Suppose I'm writing an Azure pipeline called cicd.yml
for the repositoryA
located at: organizationA/projectA/repositoryA
, I will save the above 3 groups of variables to 3 places:
- organization level variables -> to a new repository outside of the project, for e.g.
organizationA/sharedProject/sharedRepository
- project level variables -> to a new repository inside the same project, for e.g.
organizationA/projectA/sharedRepository
- repository level variables -> to the same repository:
organizationA/projectA/repositoryA
By checking following two official docs (in fact in the same doc :-)) : Variable reuse, Use other repositories, the file content of each variable group could be:
organization level variables#
# file: organizationA/sharedProject/sharedRepository/.azure-pipelines/variables/organization_variables.yml
variables:
organizationName: xxx
project level variables#
# file: organizationA/projectA/sharedRepository/.azure-pipelines/variables/project_variables.yml
variables:
- template: .azure-pipelines/variables/organization_variables.yml@sharedProject_sharedRepository
- name: myProjectVar
value: $(organizationName)_abc
repository level variables#
# file: organizationA/projectA/repositoryA/.azure-pipelines/variables/repository_variables.yml
variables:
- template: .azure-pipelines/variables/project_variables.yml@projectA_sharedRepository
- name: myRepositoryVar
value: $(myProjectVar)_abc
root cicd file#
# file: organizationA/projectA/repositoryA/.azure-pipelines/cicd.yml
# repository type = git means Azure DevOps repository as per https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#specify-multiple-repositories
resources:
repositories:
- repository: sharedProject_sharedRepository
type: git
name: sharedProject/sharedRepository
- repository: projectA_sharedRepository
type: git
name: projectA/sharedRepository
trigger: xxx
variables:
- template: variables/repository_variables.yml
- name: myRepositoryVar
value: xxx
pool:
vmImage: xxx
steps:
- script: |
echo $(myRepositoryVar)
displayName: test repositry level variables
Note
Note: We cannot put the resources
part elsewhere, it must be declared in the root pipeline file. Otherwise, the pipeline might throw the Unexpected value 'resources'
error. There's some black magic that the variables templates defined in other repositories (for e.g. project_variables.yml
) recognize well the sharedProject_sharedRepository
repository resource defined in the repository hosting the cicd.yml
file.