Azure 管道 - powershell 变量在步骤之间无法访问

问题描述 投票:0回答:1

我正在创建一个管道,由于某种原因我无法访问步骤之间的变量。 一切都包含在一项工作中。我尝试了 isOutput=true 标志,我还尝试通过首先指定步骤名称来访问变量,但仍然不起作用 - 请参阅末尾的 token01 并以错误消息作为注释。

这是我的代码:

trigger:
- master

jobs:
- job: RunTests
  pool:
    name: 'some_name'

  steps:
  - task: AzureCLI@2
    inputs:
      azureSubscription: 'some_subscription'
      scriptType: 'ps'
      scriptLocation: 'inlineScript'
      inlineScript: |
         $password = az keyvault secret show --name "some_name123" --vault-name "keyvault_name" --query value -o tsv
         Write-Host "##vso[task.setvariable variable=SECRET_PASSWORD]$password"
  - powershell: |
      $env:SECRET_PASSWORD = "$(SECRET_PASSWORD)"
      cd src/AutomatedTests/PlaywrightTests
      npm install --verbose
      npm list --verbose
      npx playwright install chromium
      npx playwright test tests/getTokens.spec.js --project=chromium --reporter=list | tee playwright-output.log
      Get-Location
      $logContent = Get-Content "playwright-output.log" -Raw
      $token01 = ''
      $token02 = ''

      foreach ($line in $logContent) {
          if ($line -match 'TOKEN01:([^\s]+)') {
              $token01 = $matches[1]
              Write-Host "Found TOKEN01: $token01"
          }
          if ($line -match 'TOKEN02:([^\s]+)') {
              $token02 = $matches[1]
              Write-Host "Found TOKEN02: $token02"
          }
      }

      Write-Host "##vso[task.setvariable variable=token01;isOutput=true]$token01"
      Write-Host "##vso[task.setvariable variable=token02;isOutput=true]$token02"
    name: GetTokens
    displayName: 'test'

  - task: PublishBuildArtifacts@1
    inputs:
      pathToPublish: 'src/AutomatedTests/PlaywrightTests/playwright-output.log'
      artifactName: 'PlaywrightLogs' 
      publishLocation: 'Container'

  - powershell: |
      newman -v
      Write-Host "Token01: $(token01)" #token01 : The term 'token01' is not recognized as the name of a cmdlet, function, script file, or operable program. 
      Write-Host "Token02: $(token02)"
 

 
powershell azure-pipelines
1个回答
0
投票

在 PowerShell 任务 (

name: GetTokens
) 上,将
token01
token02
设置为两个输出变量。

在同一个作业中,您应该使用表达式“

$(taskName.varName)
”来访问输出变量。因此,在后续任务中,您应该访问如下输出变量。

steps:
. . .

- powershell: |
    . . .
    Write-Host "##vso[task.setvariable variable=token01;isOutput=true]$token01"
    Write-Host "##vso[task.setvariable variable=token02;isOutput=true]$token02"
  name: GetTokens
  displayName: 'test'

. . .

- powershell: |
    newman -v
    Write-Host "Token01: $(GetTokens.token01)" 
    Write-Host "Token02: $(GetTokens.token02)"
© www.soinside.com 2019 - 2024. All rights reserved.