How to use template with references to script in another repository within Azure DevOps Pipelines?
Nov 5
I'm trying to use a template that's stored in another Git repository in Azure DevOps where that template has a reference to a script also contained within that repository. While I can successfully use the template from the main pipeline it doesn't reference the script the template requires.
Is there some way I can do this please?
The HelloWorld template/script are stored in Azure DevOps Repo 'HelloWorld' in a sub-folder called 'Templates'.
HelloWorld.ps1 script in the same directory as the below template.
param($Name)
Write-Host "Hello, $Name!"
Template #1 with reference to local script
parameters:
- name: azureSubscription
type: string
- name: name
type: string
jobs:
- job: HelloWorld
displayName: Hello World
variables:
name: ${{ parameters.name }}
steps:
- task: AzurePowerShell@4
name: HelloWorld
displayName: Hello World
inputs:
azureSubscription: ${{ parameters.azureSubscription }}
scriptType: filePath
scriptPath: ./HelloWorld.ps1
azurePowerShellVersion: latestVersion
failOnStandardError: true
scriptArguments:
-Name "$(name)"
Template #2
resources:
repositories:
- repository: helloworld
type: git
name: HelloWorld
stages:
- stage: Variables
jobs:
- template: Scripts/hello-world.yml@helloworld
parameters:
azureSubscription: 'My Subscription'
name: 'Billy'
1 answer
Accepted answer · original discussion
Nov 5
If your pipeline has templates in another repository, or if you want to use multi-repo checkout with a repository that requires a service connection, you must let the system know about that repository. The repository keyword lets you specify an external repository.
You could refer to Repository resource and templates in another repository for more details.
Such as in templates repository, we'd expect to find test.yml at the root.
resources:
repositories:
- repository: templates
type: git
name: templates
steps:
- template: test.yml@templates
Update1
I update your code and it works, please check it.
The repo HelloWord structure and the repo contain template file and PS1 file
Template #1 with reference to local script. Note: I add checkout step to checkout the HelloWord and pipeline self repo
parameters:
- name: azureSubscription
type: string
- name: name
type: string
jobs:
- job: HelloWorld
displayName: Hello World
variables:
name: ${{ parameters.name }}
steps:
- checkout: self
- checkout: helloworld
- task: AzurePowerShell@4
name: HelloWorld
displayName: Hello World
inputs:
azureSubscription: ${{ parameters.azureSubscription }}
scriptType: filePath
scriptPath: $(build.sourcesdirectory)/Helloworld/Subfolder/HelloWorld.ps1
azurePowerShellVersion: latestVersion
failOnStandardError: true
scriptArguments:
-Name "$(name)"
In the another repo create yaml build definition.
resources:
repositories:
- repository: helloworld
type: git
name: HelloWorld
pool:
vmImage: 'windows-latest'
stages:
- stage: Variables
jobs:
- template: hello-world.yml@helloworld
parameters:
azureSubscription: 'My Subscription'
name: 'Billy'
Result:
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.