learner
learner

Reputation: 255

AzureDevops: Can we pass dynamic value as service connection in yaml

I have a yaml. where I need to pass serviceconnections to a script/template based on an another task which retrieve all required subscriptions. Question is : Can I pass a dynamic value to a service connection? It is giving me compile time error.

My code below:

trigger: none
pr: none

parameters:
- name: AzureSubscription
  type: object
  default: 
    xxx:
      Sub: xxx
    yyy:
      Sub: yyy

jobs:
- job:  Updating
  condition: succeeded()
  pool: 
    vmImage: "windows-latest"
  strategy:
      maxParallel: 10
      matrix: ${{ parameters.AzureSubscription }} 
  steps:
    - task: AzurePowerShell@5
      displayName: Tes
      inputs:
        azureSubscription: 'zzz'
        ScriptType: 'InlineScript'
        Inline: |
              Write-Output "subcriptionList ---- $(Sub)"
        FailOnStandardError: true
        azurePowerShellVersion: 'LatestVersion'
        pwsh: true
        
    - task: AzurePowerShell@4
      displayName: Updating
      inputs:
        **azureSubscription: ${{ sub }}** # here it is giving me error?
        ScriptType: 'FilePath'
        ScriptPath: '$(System.DefaultWorkingDirectory)/Foundation/xxxxx.ps1'
        azurePowerShellVersion: 'LatestVersion'

So in the 2nd task, I am passing subscription from my parameter.

Error is :  Unrecognized value: 'sub'. 

Can someone help me?

Upvotes: 3

Views: 4655

Answers (2)

DreadedFrost
DreadedFrost

Reputation: 2970

This is possible with some creativity but not native to the task. Rather using variables and dynamically loading the variable file.

I usually declare the Service Connection name as a variable template file in a separate repository. This allows for reuse across all projects in the org, not required but find it easier that way. Part of the template name would be the environment being deployed to. So a template file might be called azure.dev.yml or azure.uat.yml and look like:

variables:
AzureSubscriptionServiceConnectionName: Azure - Dev

Then a variable defined within the scope of the stage/job would load the template file like below, assuming that a parameter or a local variable would be passed in with the given environmentName.

  variables: 
  - template: /azure.${{ parameters.environmentName }}.yml

Then the stage/job can reference this variable via:

${{ variables.AzureSubscriptionServiceConnectionName }}

Here is some more Microsoft Documentation on YAML Pipeline Variable Scope

Upvotes: 10

Krzysztof Madej
Krzysztof Madej

Reputation: 40533

You cannot set azureSubcription dynamically. It is known limitation.

@JoeGaggler this feature isn't supported today. Usage of service endpoints (Azure Subscription is one of kind) in release/build definition is controlled by some permissions. At the time of saving a release/build definition service validates that the author (whoever is saving the definition) has appropriate permissions on the endpoint. If we support variable replacements for service endpoint input then service can't validate that the author has required permissions or not and it might become a security issue.

Upvotes: 5

Related Questions