【问题标题】:Azure Pipeline Need to pass the power shell output to Kubernetes config map seems like Kubernetes task is not reading the variable at allAzure Pipeline Need to pass the power shell output to Kubernetes config map 似乎 Kubernetes 任务根本没有读取变量
【发布时间】:2020-05-02 14:36:53
【问题描述】:
- 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)

从不读取 $env:STORAGEACCOUNTNAME 变量

【问题讨论】:

    标签: azure kubernetes azure-devops


    【解决方案1】:

    由于设置变量的 PowerShell 任务处于构建阶段。您需要在 setvariable 语句中添加 isOutput=true。请查看Set a multi-job output variable

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

    我对您的 yaml 进行了一些更改以进行测试。请检查一下。我有环境变量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)
    

    在上面的脚本中,我通过在语句中添加isOutput=true 在阶段构建中输出变量,并将我的powershell 任务命名为name: myvariables。

    我在下一阶段 DEVRelease 中使用语句 $[ dependencies.{dependent phase name}.outputs['{task name}.{variable name}'] ] 引用输出变量并将其分配给 Variables。

    然后我可以在阶段 DEVRelease 中成功获取 powershell 任务中的值。

    【讨论】:

    • 嗨@user12719874 您是否尝试过上述答案,效果如何?如果有任何问题,请告诉我
    猜你喜欢
    • 2019-06-20
    • 2022-10-20
    • 2022-12-01
    • 2022-12-26
    • 2022-11-20
    • 2022-12-02
    • 2022-12-01
    • 2022-12-01
    • 2019-09-18
    相关资源
    最近更新 更多