Reputation: 16349
We use a CI tool for running automated tests of our PHP projects.
As part of this process, we wish to report the results of the CI tests to our internal dashboard tool.
The CI tool that we use allows us to provide bash scripts to execute. By default, these are executed with set -e
prepended to the script.
What I am trying to do, is run our test suite (PHPUnit), capture the exit code, make a call to the API of our internal dashboard tool (if this call fails for any reason, I do not want it to use a non-0 exit code as I want it to "always succeed" (this way if it fails it won't cause the build to fail), I then want to exit with the original PHPUnit exit code so that if any of tests failed then our build fails.
Currently my script looks like this:
# To reverse the `set -e` prepended by the CI tool
set +e
# Run the test suite and capture the exit code
phpunit --coverage-xml coverage || phpunitexitcode=$? && exit 0
# Send the data I want to our internal dashboard tool and have it always return exit 0
author=$(cat ~/.ci-dashboard-env | grep AUTHOR | cut -d'=' -f2)
start=$(cat ~/.ci-dashboard-env | grep CHIPPER_BUILD_START | cut -d'=' -f2)
duration=$(($(date '+%s') - $start))
curl -X POST -s -o /dev/null -w "CI Dashboard Response: %{http_code}" \
-F "project=hub" \
-F "author=${author}" \
-F "build_time=${duration}" \
-F "phpunit_output=@coverage/index.xml" \
-F "branch=${CI_COMMIT_BRANCH}" \
https://dashboard.example.com/api/build || exit 0
# Exit with the original PHPUnit exit code so that if the tests failed then the CI tool fails
exit $phpunitexitcode
When running this however, it seems to be that if the test suite fails, no call is ever made to the internal dashboard API. As the output from the script running only shows the PHPUnit suite nothing and does not include the output from the curl request.
Where am I going wrong?
Upvotes: 0
Views: 748
Reputation: 7297
As Mechaflash said this line phpunit --coverage-xml coverage || phpunitexitcode=$? && exit 0
is not correct
Take a look at this example
$ ls file || echo fail && echo ok
ls: cannot access 'file': No such file or directory
fail
ok
Both part executed, so if you using this syntax ||, && you should set && first
$ ls file && echo ok || echo fail
ls: cannot access 'file': No such file or directory
fail
Upvotes: 0
Reputation: 15920
Looks like your test suite execution line will always stop your script and return 0 if phpunit fails and returns an exit code of 0.
What's likely happening is phpunitexitcode is returning 0. Did you already verify the output of phpunitexitcode under a failure condition?
|| indicating if the prior command is successful to never execute the latter command. So if it fails, it will execute the latter command which is meant to grab the phpunit exit code and if it's 0 to exit and return 0 vs. if it's <0> it resumes down the script.
Upvotes: 1