英文:
How to make script output a more meaningful error message?
问题
我们正在设置Azure DevOps管道,用于在拉取请求中进行所需的检查。在这些管道的各种情况下,我们使用脚本块执行自定义代码:
- script: |
...
displayName: "执行XYZ"
continueOnError: false
如果此脚本失败,整个管道也会失败,这是可以接受的。
然而,在DevOps中的拉取请求概览页面上会显示以下信息:
这包括:
- 一个红色的X图标和消息:“1个...所需的检查失败”,非常明显,但非常肤浅。
- 一个红色的X图标和失败检查的名称。与前一个一样明显,但在复杂的多步检查情况下不太详细。
- 消息“集成
/ <failed step的名称>”。实际上在某种程度上有所帮助,但样式设置得我在快速浏览页面时几乎察觉不到。 - 实际的错误消息,与页面的其余部分形成极端对比,而且在我查看页面时第一眼就会引起我的注意。
这最后一个元素是我希望能够提供有关问题的最多具体信息的地方。不幸的是,当脚本失败时在此处显示的消息完全没有用:
Bash退出并显示代码 '1'。
可以肯定地说,如果PR显示这个消息,团队中的许多开发人员可能会认为管道有问题,然后联系(从而阻止)DevOps管理员,而不是自己检查构建详细信息。
我希望在这里显示的是,例如(根据脚本实际执行的操作而定):
- "单元测试中发生了2个错误。"
- "在文件Info.ts、List.ts和其他5个文件中检测到了样式指南违规。"
- "缺少所需的文件/PluginManifest.xml。"
显然,我可以在适当的情况下在脚本块中提供这样的消息,但问题是:我是否可以以某种方式使这条消息出现在PR概览页面上的错误摘要中,而不是那个不明确的返回代码?
英文:
We are setting up Azure DevOps pipelines that are used for required checks in pull requests. In various of these pipelines, we execute custom code with script blocks:
- script: |
...
displayName: "Execute XYZ"
continueOnError: false
If this script fails, the pipeline as a whole fails, which is fine.
However, the pull request overview page in DevOps then displays this information:
That is:
- A red X icon and the message "1 of ... required checks failed", quite visible, but very superficial.
- A red X icon and the name of the failed check. Equally visible as the previous one, but not very detailed in the case of complex multi-step checks, either.
- The message "Integrate <repo-name> / <name of failed step>". This is actually somewhat helpful, but it is styled in such a way that I barely perceive it when skimming over the page.
- The actual error message, styled with extreme contrast to the rest of the page, and literally the first thing that catches my attention when I look at the page.
This last element is where I expect a maximum of concrete information about the issue. Unfortunately, the message displayed here when a script fails is totally useless:
> Bash exited with code '1'.
It is safe to say that if the PR shows this, many developers in the team might assume there is something wrong with the pipeline and contact (and thereby block) the DevOps admins, rather than check the build details themselves.
What I'd expect to be shown there would be e.g. (depending on what the script actually does):
- "2 error(s) occurred in the unit tests."
- "Styleguide violations detected in files Info.ts, List.ts, and 5 more."
- "Required file /PluginManifest.xml is missing."
Clearly, I can provide such a message in the script block, where appropriate, but the question is: Can I somehow make this message appear in the error summary on the PR overview page instead of that nondescript return code?
答案1
得分: 0
你将无法更改此行为,但可以向PR添加注释。
请查看这个在SO上的回答。它将允许你向PR中添加任何Markdown样式的文本,甚至像这样
如果你想添加更简单的内容,你可以使用这段代码
- powershell: |
$body = @"
{
"comments": [
{
"parentCommentId": 0,
"content": "Your comment here",
"commentType": 1
}
],
"status": 4
}
"@
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$($env:Build_Repository_Name)/pullRequests/$($env:System_PullRequest_PullRequestId)/threads?api-version=5.1"
$result = Invoke-RestMethod -Uri $url -Method POST -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Body $Body -ContentType application/json
displayName: Post comment on PR
但在此之前,请确保你的构建服务有权限向你的存储库贡献拉取请求。
英文:
You will not be able to change this behavior but you can add comments to PR.
Please take a look here at this reply on SO. It will allow you to add any markdown-style text to PR. Even something like this
If you want to add something simpler you can use this code
- powershell: |
$body = @"
{
"comments": [
{
"parentCommentId": 0,
"content": "Your comment here",
"commentType": 1
}
],
"status": 4
}
"@
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$($env:Build_Repository_Name)/pullRequests/$($env:System_PullRequest_PullRequestId)/threads?api-version=5.1"
$result = Invoke-RestMethod -Uri $url -Method POST -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Body $Body -ContentType application/json
displayName: Post comment on PR
But before that, ensure your build service has permission to contribute to pull requests in your repository.
答案2
得分: 0
实际上,答案已经在另一个问题中提供:
日志命令是解决方案。将 "##vso[task.logissue type=error;]..."
写入标准输出将使您写的内容替代 ...
显示在错误摘要中,如果它是第一个记录的错误。
请注意,这只是将错误消息放在输出中;您仍然必须确保您的脚本(从而是管道阶段)实际上被视为失败,例如通过显式设置非零退出代码。
英文:
Actually, the answer was already in another question:
Logging Commands are the solution here. Writing "##vso[task.logissue type=error;]..."
to stdout will make whatever you write instead of ...
appear in the error summary, if it's the first error logged.
Note that this just places the error message in the output; you still have to make sure separately that your script (and thereby the pipeline stage) is actually considered failed, for example by explicitly setting a non-zero exit code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论