azure 发布管道 yaml 内出现 Powershell 错误

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

我使用 yamlzr 将 Azure 发布管道经典管道转换为 yaml Powershell 命令在 yaml 文件内失败:

您无法在 azure 发布管道 yaml 内的 powershell 中调用空值表达式上的方法

$branch = $env:build_sourceBranch.substring($env:build_sourceBranch.indexOf('/', 5) + 1)

  1. 我尝试使用 Build.SourceBranchName 内置变量但不起作用。

  2. 我尝试绘制它

 env:
            BUILD_SOURCEBRANCH: $(Build.SourceBranchName)

我得到(行:162,列:13):映射不是预期的错误。

powershell yaml azure-pipelines azure-pipelines-release-pipeline azure-pipelines-yaml
1个回答
0
投票

在管道中使用

Ubuntu-latest
代理时,我可以使用脚本重现该问题。

但是它可以使用

Windows-latest
代理来工作。因此,请在您的 Yaml 文件中使用
Windows-latest
代理:

例如:

trigger: none

pool:
  vmImage: Windows-latest

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $branch = $env:build_sourceBranch.substring($env:build_sourceBranch.indexOf('/', 5) + 1)
      
      Write-Host "branch :" $branch

© www.soinside.com 2019 - 2024. All rights reserved.