Search code examples
azure-devopspipelineazure-cli

Porblem in insert Subscription in AzureCli


My team have a task AzureCLI@2:

- task: AzureCLI@2
displayName: 'KEY VAULT - Get Secrets'
inputs:
  azureSubscription: '${{ variables.azuresubscription }}'
  inlineScript: |
    secrets=$(az keyvault secret list --vault-name $(postDeploy.kvName) --query "[].name" -o tsv)
    for secret in $secrets; do
      pwd=$(az keyvault secret show --name $secret --vault-name $(postDeploy.kvName) -o tsv --query value)
      echo "##vso[task.setvariable variable=${secret};issecret=true]${pwd}"
    done
  scriptLocation: 'inlineScript'
  scriptType: 'bash'
  failOnStandardError: true

but '${{ variables.KeyVaultName }}' not working ocasionally exception: enter image description here

I try others forms for inject subscription too failure, example inject in env.


Solution

  • From the error message, assume you have set up your variable on run time but this needs to be known at compilation time. Run time variables aren't supported for service connection OR azure subscription. You could refer to this ticket: DevOps Pipeline AzureCLI@2 with dynamic azureSubscription and https://github.com/microsoft/azure-pipelines-tasks/issues/10376 for more details.

    As a workaround, you could use parameters in your pipeline.

    parameters:
      - name: azuresubscription
        type: string
        values:
          - xxxx
    
    jobs:
      - job: A
        steps:
        - task: AzureCLI@2
          inputs:
            azureSubscription: '${{ parameters.azuresubscription }}'
            inlineScript: |
              az keyvault secret show --vault-name xxxx
            scriptLocation: 'inlineScript'
            scriptType: 'bash'
            failOnStandardError: true