Automate Laravel Testing with GitHub Actions

Automate Laravel Testing with GitHub Actions

Introduction

In today’s tutorial, the focus is on demonstrating the capabilities of GitHub Actions, specifically how to run automated tests whenever a pull request is made to the main branch of a repository. This is a common scenario in deployment pipelines.

Typical Deployment Scenario

In many development workflows, team members work on separate branches, such as a ‘develop’ branch. Once a feature is ready, they make a pull request to merge their changes into the main or master branch. This pull request is then reviewed and merged by another team member, leading to a release. GitHub Actions can automate the testing process during this pull request phase, ensuring the code is validated without requiring human intervention.

Setting Up GitHub Actions for Automated Testing

Initial Setup

Imagine a basic Laravel project with some example automated tests. Running PHP artisan test or vendor/bin/phpunit in this setup should show two passing tests. To set up GitHub Actions to run these tests automatically:

  1. Navigate to GitHub Actions: Go to the ‘Actions’ tab in your repository.
  2. Select a Workflow: GitHub often suggests a workflow based on the project type. If it detects a Laravel project, it will recommend a relevant workflow. If not, you can search for it among typical workflows.
  3. Set Up the Workflow: Click on ‘Set up this workflow.’ GitHub will generate a YAML file (laravel.yml) containing the setup:
    • Prepares a virtual server with PHP 8.0.
    • Runs necessary Laravel setup commands, including composer install and key:generate.
    • Creates an SQLite database and runs vendor/bin/phpunit.

This file is saved as .github/workflows/laravel.yml, and committing it will trigger the workflow.

Testing the Workflow

Once the GitHub Action is set up, it’s time to test it with a real-life scenario:

  1. Introduce a Change: Modify the code in the ‘develop’ branch to break a test. For example, change the route from ‘home’ to ‘welcome’, which does not exist.
  2. Push the Change: Push this change to the ‘develop’ branch.
  3. Create a Pull Request: Create a pull request to merge ‘develop’ into ‘main’. GitHub Actions will automatically start the testing process. This is an excellent way to preview Laravel pull requests and ensure the integrity of the code.

The automated workflow will queue and then run the tests, typically taking around 10-15 seconds. If the tests fail, GitHub will notify the repository owner, showing detailed logs of the failure.

Benefits of Using GitHub Actions

Using GitHub Actions can significantly improve the reliability of deployments. The default setup simplifies many tasks, but it is also highly configurable. For example, you can switch from SQLite to MySQL or choose different PHP versions by modifying the YAML file. Knowing how to preview Laravel pull requests can streamline your development process and reduce errors during deployment.

Cost Considerations

For public repositories, GitHub Actions is free across all plans. For private repositories, the free tier includes 2000 automation minutes per month, which scales up with higher-tier plans:

  • Team Plan: 3000 automation minutes.
  • Enterprise Plan: 50,000 automation minutes.

This makes GitHub Actions an accessible and powerful tool for both small and large projects.

Conclusion

Setting up automated testing with GitHub Actions not only enhances the reliability and efficiency of your development workflow but also ensures that each pull request is thoroughly vetted before merging. By automating these crucial steps, you can focus on writing quality code without worrying about the deployment process. For an even more streamlined approach to previewing your pull requests, consider using Glimpse. Glimpse automatically deploys your GitHub pull requests to preview environments with the help of Laravel Forge, making your development pipeline smoother and more efficient. Try Glimpse today and elevate your deployment strategy!

Start previewing your GitHub pull requests today. Sidebar Title

This is the description, change it in Branding settings.