2
votes

I am new to working with Azure DevOps and I am trying to set up build pipelines for multiple projects and share a yml template between them. I will demonstrate more clearly what I want to achieve but first let me show you our projects' structure:

proj0-common/
    |----src/
    |----azure-pipelines.yml
    |----pipeline-templates/
            |----build-project.yml
            |----install-net-core
proj1/
    |----src/
    |----azure-pipelines.yml
proj2/
    |----src/
    |----azure-pipelines.yml
proj3/
    |----src/
    |----azure-pipelines.yml

The first folder is our Common project in which we want to put our common scripts and packages and use them in the projects. The rest of the folders (proj1-proj3) are .net core projects and act as microservice projects. As you can see, each project has its own azure-pipelines.yml pipeline file and each project resides in its own repository in Github. Then there are the template pipeline files (build-project.yml and install-net-core) which reside in the common project.

All the projects have the same build steps, therefore I would like to use the build-project.yml template for all the three projects (instead of hardcoding every step in every file).

My problem is that since they reside in distinct projects, I cannot access the template files simply, let's say from project3, by just addressing it like this:

.
.
.
- template: ../proj0-common/pipeline-templates/build-project.yml
.
.
.

And [I believe] the reason is that each project will have its own isolated build pool(please do correct me on this if I am wrong).

I was thinking if Azure DevOps had similar functionality to the variable groups but for pipeline templates, that could solve my problem, however, I cannot find such a feature. Could someone suggest a solution to this problem?

2

2 Answers

1
votes

Could you copy this use case? I experimented a bit after checking out some of the docs. It had some gaps though, like most of Microsoft's other docs around Azure DevOps.

Say you have azdevops-settings.yml that specifies the pipeline in one of your service branches. In the example below it has two task steps that runs an external template in another repository, but in one of them I supply a parameter that is otherwise set to some default in the template.

Notice I had to use the endpoint tag, otherwise it will complain. Something that could be further specified in the docs.

# In ThisProject
# ./azdevops-settings.yml
resources: 
  repositories:
  - repository: templates
    type: bitbucket
    name: mygitdomain/otherRepo
    endpoint: MyNameOfTheGitServiceConnection

steps:
- template: sometemplate.yml@templates
  parameters:
    Param1: 'Changed Param1'
- template: sometemplate.yml@templates

In the template I first have the available parameters that I want to pipe to the template. I tried out referencing parameters without pipeing them, like build id and other predefined variables and they worked fine.

I also tried using an inline script as well as a script path reference. The 'test.ps1' just prints a string, like the output below.

# otherRepo/sometemplate.yml
parameters:
  Param1: 'hello there'

steps: 
- powershell:  |
    Write-Host "Your parameter is now: $env:Param"
    Write-Host "When outputting standard variable build id: $(Build.BuildId)"
    Write-Host "When outputting standard variable build id via env: $env:BuildNumber"
    Write-Host "The repo name is: $(Build.Repository.Name)"
    Write-Host "The build definition name is: $(Build.DefinitionName)"
  env: 
    Param: ${{parameters.Param1}}
    BuildNumber: $(Build.BuildId)
- powershell: './test.ps1'

And the separate powershell script:

# otherRepo/test.ps1
Write-Host "Running script from powershell specification"

Output:

========================== Starting Command Output ===========================
Your parameter is now: Changed Param1
When outputting standard variable build id: 23
When outputting standard variable build id via env: 23
The repo name is: mygitdomain/thisRepo
The build definition name is: ThisProject
Finishing: PowerShell

========================== Starting Command Output ===========================
Running script from powershell specification
Finishing: PowerShell

..and so on..
0
votes

I found only one solution to actually do that. You can reference the parent directory by using an absolute path. The key was to populate the root path using a system variable. The solution for your example:

- template: ${{variables['System.DefaultWorkingDirectory']}}/proj0-common/pipeline-templates/build-project.yml