##vso[task.complete result=Failed;] is not failing a script step
Asked Answered
P

4

8

I thought that a call to ##vso[task.complete result=Failed;] should make the script step where it is called fail immediately but I have now seen one case in my pipeline where this is not the case.

Is my assumption wrong or there a bug in the Azure pipelines script task?

My build agent is running the task in an Ubuntu Container and the agent itself is a Linux agent.

Piggery answered 4/11, 2021 at 7:1 Comment(0)
P
2

I just made a small test and using below code leads to the desired goal of aborting the step with an error but showing only one error message in the Azure pipelines web ui:

echo "##vso[task.logissue type=error]Something went very wrong."
echo "##vso[task.complete result=Failed;]Make step fail"
exit 0

This is working fine but it looks really ugly since the "exit 0" gives the impression that the step is ok but actually the status will be "Failed" because of the task.complete call.

Still I would like to know if this behaviour of the task.complete command to not abort the step is "by design" or I just found a workaround for a bug.

Piggery answered 4/11, 2021 at 14:13 Comment(0)
C
5

You can try to add the command line 'exit 1' after the '##vso[task.complete result=Failed;]' command.

echo "##vso[task.complete result=Failed;]"
exit 1

Similarly, you also can try using the logging command 'LogIssue' in your pipeline task to log an error on the task.

echo "##vso[task.logissue type=error]Something went very wrong."
exit 1
Crannog answered 4/11, 2021 at 9:25 Comment(3)
I know this already but then (your 2nd example) in the Azure Pipelines web UI you see two error messages, one for the exit 1 call (just saying "exit code was 1") and one for the logissue command that has a proper error message. Having two error messages for one error is just bad usability that I want to avoid if possible.Piggery
Hi @Marko, The logging commands can't make the script step be failed immediately. You must add the exit 1 after the logging commands if you the script step to be failed immediately when logging an error with the logging commands. We no way to only use the logging commands to make the script step be failed immediately.Crannog
The logging command from my point of view does not need to make it break but the "task.complete result=failed" should be potentially doing that. As I said I have now a solution with the exit 0 but it is somehow ugly.Piggery
P
2

I just made a small test and using below code leads to the desired goal of aborting the step with an error but showing only one error message in the Azure pipelines web ui:

echo "##vso[task.logissue type=error]Something went very wrong."
echo "##vso[task.complete result=Failed;]Make step fail"
exit 0

This is working fine but it looks really ugly since the "exit 0" gives the impression that the step is ok but actually the status will be "Failed" because of the task.complete call.

Still I would like to know if this behaviour of the task.complete command to not abort the step is "by design" or I just found a workaround for a bug.

Piggery answered 4/11, 2021 at 14:13 Comment(0)
E
0

Check if there is a try-catch around the line of exiting the script.

It took me a whole day why it was showing an error but the task would never be marked as failure.

If there is no try-catch around the exit or [system.environment]::Exit(1) line it should work..

Egesta answered 21/10, 2022 at 15:48 Comment(0)
F
0

Use PowerShell and Write-Output for your desired behavior in Azure DevOps

- powershell: |
    Write-Host "##[error] Something went wrong"
    Write-Output "##vso[task.complete result=Failed;]Finished"
Facility answered 12/8 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.