Do manual build fail in Jenkins using shell script

Solution 1:

All you need to do is exit 1.

if [ -f "$file" ]
then
    echo "$file found."
else
    echo "$file not found."
    exit 1
fi

Solution 2:

To fail a Jenkins build from .NET, you can use Environment.Exit(1).

Also, see How do I specify the exit code of a console application in .NET?.

Solution 3:

To fail a Jenkins build from Windows PowerShell, you can use Steve's tip for C# as follows:

$theReturnCode = 1
[System.Environment]::Exit( $theReturnCode )

Notes;

1.Windows recycles exit codes higher than 65535. ( https://stackoverflow.com/a/31881959/242110 )

2.The Jenkins REST API has a '/stop' command, but that will cancel the build instead of actually failing it. ( https://jenkinsapi.readthedocs.org/en/latest/build.html )

3.Jenkins can also mark a build 'unstable', and this post ( https://stackoverflow.com/a/8822743/242110 ) has details on that solution.