Azure Dev Ops 警告内联 shell 脚本

时间:2021-02-15 15:01:18

标签: azure-devops azure-pipelines azure-pipelines-yaml

问题

我在 Azure Dev Ops 管道中通过 SSH 在远程机器上运行内联 shell 脚本。根据某些条件,运行管道应该会抛出自定义警告。

该功能有效,尽管方式非常复杂:运行管道时,警告总是出现。更准确地说:

  • 如果不满足条件,则警告出现一次。
  • 如果满足条件,警告会出现两次。

下面的例子应该清楚地说明了这个问题。

示例

假设我们有以下 .yaml 管道模板。请根据您的设置调整 poolsshEndpoint 设置。

pool: 'Default'

steps:

- checkout: none
  displayName: Suppress regular checkout

- task: SSH@0
  displayName: 'Run shell inline on remote machine'
  inputs:
    sshEndpoint: 'user@machine'
    runOptions: inline
    inline: |
     if [[ 3 -ne 0 ]]
     then
       echo "ALL GOOD!"
     else
      echo "##vso[task.logissue type=warning]Something is fishy..."
     fi
    failOnStdErr: false

预期行为

因此,上面的 bash 脚本应该 echo ALL GOOD! as 3 is not 0。因此,不应触发警告消息。

当前行为

但是,当我在 Azure Dev Ops 中运行上述管道时,步骤日志概述说有一个警告:

Step Log Overview

日志本身看起来像这样(连接细节被涂黑):

enter image description here

因此,即使代码采用 ALL GOOD! 代码路径,也会出现警告消息。我认为这是因为整个 bash 脚本都回显到日志中 - 但这只是一个假设。

问题

如何确保警告仅在满足条件时才出现在执行的管道中?

1 个答案:

答案 0 :(得分:2)

看起来像 the SSH task logs the script to the console prior to executing it。您可能会欺骗日志解析器:

HASHHASH="##"
echo $HASHHASH"vso[task.logissue type=warning]Something is fishy..."

I'd consider it a bug that the script is appended to the log as-as. I've filed an issue.(似乎也进行了一些解析)...