Visual Studio Team Services 说 "Task PowerShell failed" 虽然没有

Visual Studio Team Services says "Task PowerShell failed" although it didn't

我在 VS Team Services 中的 powershell 脚本末尾收到以下错误:

Task PowerShell failed. This caused the job to fail. Look at the logs for the task for more details.

但是,脚本并没有失败。它做了我想让它做的一切。有什么方法可以向 VS Team Services 解释吗?

该脚本从 VS Team Services 获取经过编译和测试的代码,并将其推送到 bitbucket 上的 git 存储库。这样,我可以从中自动创建一个 docker 容器并更新我的应用程序。这是脚本:

git clone BITBUCKET_URL/example-repo.git
cd example-repo

echo "Clean app-folder and remove Dockerfile"

remove-item app -recurse -force
remove-item Dockerfile -force

new-item -name app -itemtype directory

echo "Copy new files into repository"

cp ../deploy/Dockerfile .
cp ../ExampleApp/bin/Debug/* ./app

echo "Set email, name and include all files"

git config user.email "EMAIL"
git config user.name "NAME"
git add -A

echo "What has changed:"
git status

echo "Commit and Push..."
git commit -m "VS Online Commit"
git push origin master

此脚本贯穿始终并正确执行所有操作。但是,这是我的 Visual Studio 团队服务日志的结尾:

******************************************************************************
Starting task: Powershell: deploy/deploy.ps1
******************************************************************************
Cloning into 'example-repo'...
Clean app-folder and remove Dockerfile
    Directory: C:\PROJECT\example-repo
Mode                LastWriteTime     Length Name                                                                      
----                -------------     ------ ----                                                                      
d----         7/17/2015   7:59 AM            app                                                                       
Copy new files into repository
Set email, name and include all files
What has changed:
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    modified:   app/App.exe
    modified:   app/App.pdb
Commit and Push...
 VS Online Commit
 2 files changed, 0 insertions(+), 0 deletions(-)
To BITBUCKET_URL/example-repo.git
b861c96..afd33e6 master -> master
******************************************************************************
Finishing task: PowerShell
******************************************************************************
Task PowerShell failed. This caused the job to fail. Look at the logs for the task for more details.
******************************************************************************
Finishing Build
******************************************************************************

从这个输出中,以下行是红色的,与错误行类似:

Cloning into 'example-repo'...

To BITBUCKET_URL/example-repo.git
b861c96..afd33e6 master -> master

难道这里 returns 不是 0,这就是 VS Team Services "detects" 失败的原因?

我该如何预防?

更新:

运行 只有 git clone 已经表明存在错误,所以看起来 git clonegit push 确实会导致一些被视为错误的东西VS 团队服务。

显然 Visual Studio 对错误通道的反应就像脚本出错一样。由于您的 git 将一些输出发送到错误通道,您可以将脚本与 2>&1 输出重定向器一起使用,或者将 $ErrorActionPreference 设置为脚本中的 silentlyContinue,或者重定向所有 git命令输出同理2>&1

关于退出代码 - 如果您的脚本不包含 exit <code> 命令,则其退出代码为零,因为所有 git 命令的退出代码在解释脚本时都会被 Powershell 吸收,在每个命令之后的脚本中可用 $lastExitCode 但如果不使用则丢弃。

正如 Vesper 在他的回答中所写,git 正在将输出发送到 stderr,VS Team Services 认为它​​因此而失败。

但是,仅重定向或忽略 stderr 对我来说似乎不是正确的解决方案。相反,您可以指定:

git clone --quiet ...
git push --porcelain ...

停止 git 在 stderr 上报告状态,或者完全停止报告 (--quiet) or sending it to stdout (--porcelain)。

更新:

如果 git add 显示有关行尾的警告,您可以使用以下配置禁用它:

git config core.safecrlf false

当然,你可能还想在配置中设置core.autocrlf