I have an Azure DevOps pipeline setup with YAML templates shared across multiple repositories. Specifically, I have:
- A YAML template file named kv_template.yaml in Repo1 (Project1). This template runs an Azure CLI task that requires a service connection to authenticate with Azure.
- Service Connection 1 is configured for Project1 and is used by kv_template.yaml when run directly in Repo1.
- Service Connection 2 is configured for Project2 and should be used when kv_template.yaml is called by a pipeline in Repo2 (Project2).
I’ve considered Sharing multiple Service Connections in the Repository where kv_template.yaml is saved, but I’m not sure of the best way to detect or pass which repository/project is calling kv_template.yaml. My ideal solution would dynamically use Service Connection 1 when kv_template.yaml is called within Repo1 and Service Connection 2 when called within Repo2.
My objective is to have kv_template.yaml dynamically select the appropriate service connection based on which repository/pipeline is calling it, without needing to hardcode or duplicate the template file.
Is there a way in Azure DevOps YAML to detect the calling repository or project and use the appropriate Service Connection?
kv_template.yaml script:
- name: parameter1
type: string
- name: parameter2
type: string
jobs:
- job: Job1
steps:
- checkout: self
- task: AzureCLI@2
inputs:
azureSubscription: '$(SERVICE_CONNECTION_1)'
scriptType: 'pscore'
scriptLocation: 'scriptPath'
scriptPath: 'helper_scripts/script.ps1'
arguments: '-WebhookURL "${{ parameters.parameter1 }}" -KeyVaultsToCheck "${{ parameters.parameter2 }}"'
env:
GLOBAL_SUBSCRIPTION: $(GLOBAL_SUBSCRIPTION)


