Small Things to note running dockerized Laravel testing with Github Actions

Ken
2 min readJul 19, 2021

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:

  1. When PHPUnit detects the presence of an .env.testing file, it will automatically use it as the .env file in the testing environment.
  2. 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.
  3. 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 set DB_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 use DB_HOST=localhost. For a more in-depth explanation, refer to this helpful tutorial on Service Containers.
  4. 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 -.
  5. We’ve set fetch-depth:1 to avoid cloning the entire Git history. This behavior is already the default for actions/checkout@v2, but we're making it explicit here. You can refer to the documentation for a list of all available inputs.
  6. 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!

--

--