【问题标题】:unable to pass parameter from azure Devops yaml to PowerShell无法将参数从 azure Devops yaml 传递到 PowerShell
【发布时间】:2021-09-13 18:31:03
【问题描述】:
parameters:
- name: AzureSubscription
  default: 'abc'
- name: BlobName
  type: string
  default: ""
stages:
- stage: MyStage
  displayName: 'My Stage'
  variables:
    - name: sas

 jobs:
   - job: ABC
     displayName: ABC 
     steps:
     - task: AzureCLI@2
       displayName: 'XYZ'
       inputs:
        azureSubscription: ${{ parameters.AzureSubscription }}
        scriptType: pscore
        arguments: 
        scriptLocation: inlineScript
        inlineScript: |
          $sas=az storage account generate-sas --account-key "mykey" --account-name "abc" --expiry (Get-Date).AddHours(100).ToString("yyyy-MM-dTH:mZ") --https-only --permissions rw --resource-types sco --services b
          Write-Host "My Token: " $sas

    - task: PowerShell@2
      inputs:
        targetType: 'filepath'
        filePath: $(System.DefaultWorkingDirectory)/psscript.ps1
        arguments: >
          -Token "????"
          -BlobName "${{parameters.BlobName}}"
        displayName: 'some work'

在这个 Azure Devops yaml 中,我创建了 2 个任务。 AzureCLI@2 和 PowerShell@2

在 AzureCLI@2 中,我在 $sas 变量中获得了价值。 Write-Host 确认了这一点,但 $sas 没有作为参数传递给 PowerShell@2 powershell 文件作为参数。

"${{parameters.BlobName}}" 工作正常。在 powershell 中,我能够读取该值。 如何传递sas变量值?
试过了

-令牌 $sas # 无效

-Token "${{sas}}" # 无效

【问题讨论】:

    标签: azure powershell azure-devops


    【解决方案1】:

    Azure Pipeline 中的不同任务不共享允许它们保留或传递变量的公共运行空间。

    因此,Azure Pipelines 提供了特殊的日志记录命令,允许从任务中获取字符串输出以更新可在后续任务中使用的 Azure Pipeline 环境变量:Set variables in scripts (Microsoft Docs)

    在您的情况下,您将使用这样的日志记录命令使您的 sas 令牌可用于下一个任务:

    Write-Host "##vso[task.setvariable variable=sas]$sas"
    

    在后续任务的参数中(在同一个作业中)使用 Azure Pipelines 的变量语法:

    -Token '$(sas)'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 2019-06-29
      • 2019-09-01
      相关资源
      最近更新 更多