Search code examples
azure-devopsazure-pipelineserror-messaging

How to make script output a more meaningful error message?


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:

errors in PR overview page

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 / ". 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?


Solution

  • 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.