Azure DevOps Powershell task template for multi and single repo cases

55 views Asked by At

I want to move some powershell script tasks to templates.
According to the Microsoft article Checkout path i need to use the different pathes in case of the single and multi repositories

#Task for the multi repo checkouts 
 - task: PowerShell@2
    displayName: Test
    inputs:
      pwsh: true
      targetType: filePath
      filePath: ./RepoName/Script.ps1
#Task for the single repo checkout 
 - task: PowerShell@2
    displayName: Test
    inputs:
      pwsh: true
      targetType: filePath
      filePath: ./Script.ps1

For the template we have a posibility to call it from the specified repo

  - checkout: self
  - checkout: repo2
  - template: template.yml@self

Is it possible to have a similar way for the powershell task and call the Powershell task from the same repo as a template?

Have a possibility to call the powershell script file from the same repo as a template.

1

There are 1 answers

0
Alvin Zhao - MSFT On BEST ANSWER

As of now, there appears to be no automatic way to detect from which repository resource the template is expanded and execute the script under the corresponding path on the agent, when we checkout multiple repos.

However, we can opt to specify the path for a checkout step, and the code from repository resources will be downloaded to the path relative to Agent.BuildDirectory. The subfolder name is thus set as well with the name we defined rather than RepoName, even when we only checkout single repo resource. Kindly refer to the sample below.

# main.yml in self repo

pool:
  vmImage: ubuntu-latest

resources:
  repositories:
  - repository: self
  - repository: repo2
    type: git
    name: AnotherRepo

variables:
- name: repoResource1
  value: repoResource1
- name: repoResource2
  value: repoResource2

steps:
- checkout: self
  path: $(repoResource1)
- checkout: repo2
  path: $(repoResource2)
- script: tree $(Agent.BuildDirectory)
- template: template.yml@self
- template: template.yml@repo2
# template.yml in self repo

steps:
- task: PowerShell@2
  displayName: Test1
  inputs:
    filePath: '$(Agent.BuildDirectory)/$(repoResource1)/Script.ps1'
    pwsh: true

enter image description here

Hope the workaround may help address your concerns.