'How to use template with references to script in another repository within Azure DevOps Pipelines?
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'
Solution 1:[1]
When you have multiple repositories in your pipeline and you have them when you use template from another repo, they are fetched to their own folders instead of root one. It seems to be obvious but we need to have this in mind.
If you have multiple checkout steps in your job, your source code is checked out into directories named after the repositories as a subfolder of s in (Agent.BuildDirectory). If (Agent.BuildDirectory) is C:\agent_work\1 and your repositories are named tools and code, your code is checked out to C:\agent_work\1\s\tools and C:\agent_work\1\s\code.
Thus when you call a script inside a template which is in another repo you may be forced to provide alias name for the repo to properly locate your script.
For instance:
trigger: none
pr: none
resources:
repositories:
- repository: devops
type: github
name: kmadof/devops-templates
endpoint: kmadof
stages:
- template: templates/start.yaml@devops
parameters:
repo: devops-templates
buildSteps:
- checkout: self
- checkout: devops
- bash: echo Test #Passes
displayName: succeed
- bash: echo "Test"
displayName: succeed
I'm passing there repo parameter to use it in my template:
parameters:
- name: repo # defaults for any parameters that aren't specified
default: ''
steps:
- task: PowerShell@2
inputs:
filePath: ${{ parameters.repo }}/scripts/myscript.ps1
You can check code here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Krzysztof Madej |
