Conditional Build and Testing
Updated on 28 Dec 2018
In the previous section we saw how to invoke codeception for unit testing and viewing the log file. In this section we’ll have jenkins examine the results of the automated testing and decide if it should continue with the deployment.
Process xUnit testing
In the next step, we want to Add build step, by selecting Process xUnit test result report
.
When the new section is displayed, we want to further refine our test results by selecting PHPUnit-3.x
.
Fill in the missing bits
In the Pattern field, we fill out the pattern on where to find the xml report. For Codeception, the report output is found in the following directory
tests/_output
and the file will be report.xml.
For the Failed tests section we place a 0 in the Total and New fields. This means that if any of the tests fail, then the build status will be set to failed as well. This becomes important later for conditional builds.
Conditional Build
We can add a new step, Conditional step (single), to our build process.
There are alot of choices in the Run section, but we’ll just select Current build status
.
There is no equals choice here, but rather a range. Worst
and Best
Status indicate the lower and upper bounds of the status. When I select Success
for both Worst and Best, this essentially means ‘When the Current build status is Success’ then continue.
What I tell it to do, is execute another shell command.
Rsync Shell Script
deploy/deploy2.sh
#!/bin/sh
rsync -av $WORKSPACE /home/brent/myCI --exclude-from=$WORKSPACE'/deploy/exclude_me.txt' --delete
chown -R jenkins:www-data /home/brent/myCI/myDeploy
chmod -R 755 /home/brent/myCI/myDeploy
I am using rsync because there are some folders/files I want to copy over to the deployment directory, but there are others I want to skip. rsync allows me to provide an exclude-from
file so that certain files/folders wont be copied.
exclude_me.txt
Jenkinsfile
.git
composer.lock
deploy/
vendor/
tests/
Run - success
A run that is a success is going to look like this
With a log file that is equally pleasing
Run - failure
We need to check that this process is going to work when a unit test fails. I.e. if a unit test fails, then the files should not be deployed.
Let’s change one of the unit tests to ensure a failure and see what happens.
With a log file that also explains an issue
Even better is clicking on the Tests link to see which tests actually failed.