Azure Pipeline Need to pass the power shell output to Kubernetes config map seems like Kubernetes task is not reading the variable at all

1/15/2020
- task: PowerShell@2
    displayName: Save Storage account Secrets to Build Variables
    inputs:
      azureSubscription: 
      targetType: 'inline'
      script: '$outputs = ConvertFrom-Json $($env:STORAGE); foreach ($output in $outputs.PSObject.Properties) { echo $output.Name; echo $output.Value.value; Write-Host ("##vso[task.setvariable variable=$($output.Name);]$($output.Value.value)");}'

- phase: DEVRelease
  dependsOn: Build
  queue: Hosted Ubuntu 1604
  steps:
  - task: Kubernetes@1
    displayName: Apply Kubernetes Deployment
    inputs:
      kubernetesServiceEndpoint: 
      arguments: "-f conf/deploy_local.yaml"
      command: apply
      azureSubscription: 
      azureContainerRegistry: 
      configMapName: myconfig
      forceUpdateConfigMap: true
      configMapArguments: --from-literal=myname=$($env:STORAGEACCOUNTNAME1)

Never reads the $env:STORAGEACCOUNTNAME variable

-- user12719874
azure
azure-devops
kubernetes

1 Answer

1/16/2020

Since the PowerShell task to set the variables is in phase build. You need to add isOutput=true to the setvariable statement. Please check Set a multi-job output variable

"##vso[task.setvariable variable=$($output.name);isOutput=true]$($output.Value)"

I made a little bit changes to your yaml for testing. Please check it out. I have the env variable STORAGE = {'tags':[{'name':'A', 'Value':'1' }, { 'name':'B', 'Value':'2'}]}

phases:
- phase: build
  queue: Hosted Ubuntu 1604
  steps:
  - powershell: |
      $outputs = ConvertFrom-Json $($env:STORAGE)
      foreach ($output in $outputs.tags) { echo $output.name; echo $output.Value; Write-Host ("##vso[task.setvariable variable=$($output.name);isOutput=true]$($output.Value)");}
    name: myvariables
  - powershell: |
      echo "$(myvariables.A)"
      echo "$(myvariables.A)"


- phase: DEVRelease
  dependsOn: Build
  queue: Hosted Ubuntu 1604
  variables: 
    Da: $[ dependencies.build.outputs['myvariables.A'] ]
    Db: $[ dependencies.build.outputs['myvariables.B'] ]

  steps:
  - powershell: |
      echo $(Da)
      echo $(Db)

In above script I output variable in phase build by adding isOutput=true to the statement, and give my powershell task a name name: myvariables.

And I refer to the output variable in the next phase DEVRelease by using statement $[ dependencies.{dependent phase name}.outputs['{task name}.{variable name}'] ] and assign it to the Variables.

Then i can successfully get the value in powershell task in phase DEVRelease. enter image description here

-- Levi Lu-MSFT
Source: StackOverflow