powershellazure-devopsazure-pipelinespipelineazure-pipelines-yaml

How to handle cross-repository script paths in Azure DevOps YAML pipelines?


I'm working on a multi-repo Azure DevOps setup involving two repositories (Repo1 and Repo2) in different projects, and I'm encountering issues with consistent path references when calling a PowerShell script from a shared YAML pipeline template.

The Setup:

  1. Repo1 (Project1) contains:

    • A pipeline, pipeline1.yaml, which calls a YAML template, pipeline_template.yaml.

    • The template pipeline_template.yaml then calls a PowerShell script, script.ps1, located in helper_scripts/script.ps1.

  2. Repo2 (Project2) contains:

    • A pipeline, pipeline2.yaml, which calls the same pipeline_template.yaml from Repo1.

However, when I run pipeline2.yaml from Repo2, I get a "file not found" error for script.ps1 unless I use the path Repo1/helper_scripts/script.ps1 instead of helper_scripts/script.ps1.

Is there a way to set up a consistent or dynamic file path in pipeline_template.yaml that allows script.ps1 to be referenced correctly when called from both Repo1 and Repo2? I'd like to avoid hardcoding paths for each repository and keep the reference universal if possible. Any advice on how to structure the file paths or configure the pipeline templates to achieve this?

Thank you for any guidance!

pipeline_template.yaml:

- name: parameter1
  type: string
- name: parameter2
  type: string

jobs:
  - job: Job1
    steps:
      - checkout: self
      - task: AzureCLI@2
        inputs:
          azureSubscription: '$(GLOBAL_SERVICE_CONNECTION)'
          scriptType: 'pscore'
          scriptLocation: 'scriptPath'
          scriptPath: 'helper_scripts/script.ps1'
          arguments: '-WebhookURL "${{ parameters.parameter1 }}" -KeyVaultsToCheck "${{ parameters.parameter2 }}"'
        env:
          GLOBAL_SUBSCRIPTION: $(GLOBAL_SUBSCRIPTION) 
    





Solution

  • In your pipeline_template.yaml, you only add - checkout: self. When you run pipeline2, only repo2 will be checked out, so that the pipeline can't find the PS script in the repo1.

    According to Check out multiple repositories in your pipeline,

    if a path is specified for a checkout step, that path is used, relative to (Agent.BuildDirectory).

    To resolve the above issue, you can checkout repo1 using - checkout: git://project1/repo1 with path and use $(Agent.BuildDirectory)/repo1/helper_scripts/script.ps1 as the path of scripts.

    Refer to the example below. (I use PowerShell task for example, it's the same for AzureCLI@2 task.)

    pipeline_template.yaml:

    jobs:
      - job: Job1
        steps:
        - checkout: self
        - checkout: git://project1/repo1
          path: repo1
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: 'tree /f /a'
            workingDirectory: '$(Agent.BuildDirectory)'
        - task: PowerShell@2
          inputs:
            filePath: '$(Agent.BuildDirectory)/repo1/helper_scripts/script.ps1'
    

    Whether to add - checkout: self depends on whether pipeline2 needs the content in repo2. If not, you can remove this line.

    Result: