Zitrax
Zitrax

Reputation: 20344

Jenkins pipeline not failing stage on batch failure

In a jenkins declarative pipeline if I add a step like this:

bat '''dir QWERTY'''

it will fail the stage as expected since there is no such directory.

However this is a bat script with many statements so I want to fail as soon as there is an error. I have been used to appending || exit /b to handle that. But as part of jenkins pipelines this does not seem to work.

bat '''dir QWERTY || exit /b'''
bat '''dir QWERTY || exit /b %ERRORLEVEL%'''

None of the above fail the stage. Why?

A third option:

bat '''dir QWERTY
if %errorlevel% neq 0 exit /b %ERRORLEVEL%
'''

seem to work though but that hurts the readability of the script.

Upvotes: 5

Views: 4629

Answers (2)

user7818749
user7818749

Reputation:

I do not currently have Jenkins, so I cannot really test all the methods, but technically you can test if the chain works by:

bat '''dir QWERTY && echo success || echo Failed'''

This should print one of the 2 commands, if failed was the message, the process should work. You could then setup a more readable string:

bat '''dir QWERTY && echo. || exit /b'''

You could retain your if %errorlevel% method, by calling a label:

bat '''
:error
if %errorlevel% neq 0 exit /b!

dir QWERTY & call :error
dir AZERTY & call :error
'''

Upvotes: 2

Zitrax
Zitrax

Reputation: 20344

Turns out in a Jenkins pipeline I can't use /b. Thus:

bat '''dir QWERTY || exit'''

works.

Upvotes: 2

Related Questions