Making a build show as failed or succeeded
By default, the build script executed when building an environment on Dropsolid Experience Platform will fail or succeed based on the result of the last command executed in the script.
This may or may not be the expected result, but you can create a more opinionated version so you can rely on the outcome of a build.
Ways to fail the build script¶
On first error¶
Depending on what your script does during a build, it can be useful to just stop the script when an error occurs.
Add following line after the shebang line in your script
set -o errexit
There are some caveats you need to be aware of (scripts failing when they shouldn't), see http://mywiki.wooledge.org/BashFAQ/105
Opinionated¶
A more useful way is to determine per project if how and when you want
to fail or succeed a build.
The key concept is that you can get the exit code of the last command
that ran with $?
example script
#/bin/bash
command_result=0
a_command
command_result=$(( $command_result + $? ))
a_command_that_can_fail
a_command_that_should_fail_immediately || exit 1
another_commandcommand_result=$(( $command_result + $? ))
# exit with the combined exit code of all commands that mattered
exit $command_result
- when
a_command
a_command_that_should_fail_immediately
andanother_command
succeed, the exit code will be 0, marking the build as passed - when
a_command
a_command_that_should_fail_immediately
oranother_command
fails, the exit code would be > 0, marking the build as failed.. - If
a_command_that_should_fail_immediately
fails, the script will stop immediately, marking the build as failed a_command_that_can_fail
can fail, it won't affect the outcome of the script.
Other possibilities when an opinionated build fails¶
As we can store the exit code of every command, we can also react on it.
- create a db dump in the beginning of the script, an restore it on failure (be aware that code changes won't be restored)
- keep the site in maintenance mode until the error is reviewed
- retry certain commands when they fail (don't create endless loops!)