Accessing a PowerShell function from a separate repository in your Azure pipelines

When working with multiple repositories in an Azure pipeline, you might need to access a PowerShell (.ps1) function from one repository while running your pipeline in another. Here’s a straightforward guide to make that happen:

Before you can use the PowerShell function, you need to ensure the relevant repository is available in your pipeline. Use the pipeline script to checkout the repository that contains the desired function. For example:

resources:
  repositories:
  - repository: AnotherRepository # The name used to reference this repository in the checkout step
    type: github
    name: OtherProject/MyAzureReposGitRepo

steps:
- checkout: self  # Checkout the current repository
- checkout:
    name: AnotherRepository

Then, in your PowerShell script, use the file path to locate and import the desired .ps1 file. The relative path to the other repository is determined based on the location of your current script. For instance:

. $PSScriptRoot/../../helper/functions.ps1

Here, $PSScriptRoot refers to the directory containing the current script. Adjust the relative path (../..) according to the structure of your repositories.

This setup ensures seamless integration between repositories, allowing you to reuse and maintain PowerShell functions efficiently.

comments powered by Disqus