Azure Devops yaml构建定义PowerShell内联语法问题

Emr*_*mre 2 powershell yaml build azure-devops azure-pipelines

我正在从经典构建系统(Azure Devops 中的任务定义)迁移到 yaml 构建定义,并在以下错误中运行。我能够在执行 PowerShell 脚本时分解错误,并发现 yaml 文件中的语法问题。

我使用 PowerShell 内联步骤,如下所示:

name: 1.1$(Rev:.r)

jobs:
- job: build
steps:
- task: PowerShell@2
    displayName: 'Test'
    inputs:
    targetType: 'inline'
    script: '$var1 = "valueVar1" 
             $var2 = "valueVar2"'
Run Code Online (Sandbox Code Playgroud)

当我运行命令时,我在 Azure Devops 中收到以下错误。

2019-12-07T18:39:15.3306239Z At D:\A\01\_temp\298966df-c117-432b-b4c1-17b4ece9d7f4.ps1:2 char:21
2019-12-07T18:39:15.3306715Z + $var1 = "var1Value" $var2 = "var2Value"
2019-12-07T18:39:15.3306784Z +                     ~~~~~
2019-12-07T18:39:15.3306847Z Unexpected token '$var2' in expression or statement.
2019-12-07T18:39:15.3307001Z     + CategoryInfo          : ParserError: (:) [], ParseException
2019-12-07T18:39:15.3307719Z     + FullyQualifiedErrorId : UnexpectedToken
2019-12-07T18:39:15.3308055Z  
2019-12-07T18:39:15.4085556Z ##[error]PowerShell exited with code '1'.
Run Code Online (Sandbox Code Playgroud)

看起来我没有在 yaml Powershell 内联定义中使用换行符。

4c7*_*b41 8

应该这样做:

script: |
  $var1 = "valueVar1"
  $var2 = "valueVar2"
Run Code Online (Sandbox Code Playgroud)

与任何内联任务 (cmd\bash\pwsh) 的工作方式相同。

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/command-line?view=azure-devops&tabs=yaml#example

事实上你可以这样做:

steps:
- powershell: |
    $var1 = "valueVar1"
    $var2 = "valueVar2"
Run Code Online (Sandbox Code Playgroud)