Small Things to note running dockerized Laravel testing with Github Actions
It’s always good to test your code for changes to ensure that buggy code never makes its way into production. GitHub Actions is a valuable feature for continuous integration. With a free account, you have 2,000 minutes per month to utilize. Today, let’s set up Dockerized Laravel unit testing using it.
tldr;
The final workflow in .github/workflows/testing.yml
looks like this.
Note:
- When PHPUnit detects the presence of an
.env.testing
file, it will automatically use it as the.env
file in the testing environment. - By specifying a
container
, our jobs will run within the Laravel application’s Docker container rather than on the host machine. This makes it easier to access service containers. - The
services.<label>
will become your Docker container's hostname. In the example above, the MySQL database container’s hostname isdb-tests
. Therefore, in your.env.testing
file, ensure that you setDB_HOST=db-tests
. This configuration is needed because the jobs are running inside the Docker container. If they were running on the host machine, you'd useDB_HOST=localhost
. For a more in-depth explanation, refer to this helpful tutorial on Service Containers. - If you encounter connection issues with MySQL, you can use the
curl
command to test it during one of the run steps. For example:curl db-tests:3306 -output -
. - We’ve set
fetch-depth:1
to avoid cloning the entire Git history. This behavior is already the default foractions/checkout@v2
, but we're making it explicit here. You can refer to the documentation for a list of all available inputs. - The
fail-fast
strategy is enabled by default. We're simply making it clear in this tutorial.
Here’s a sample of the .env.testing
file for your reference.
That’s all for now, have fun with your testing!