Automate your UnitTests with GitHub Actions

Hello!

This is Matsuki from the System Development Department

Following on from the previous article on GitHub Actions, this article will also be about GitHub Actions

This time, we will be talking about "Automating PHP UnitTests with GitHub Actions!"

Points to note

In this article, we will proceed under the assumption that you have already set up a Laravel environment using Docker

| |- docker | |- mysql | | |- Dockerfile | | |- my.cnf | | | |- php | |- Dockerfile | |- php.ini | |- docker-compose.yml | |- src

What are GitHub Actions?

This is briefly explained in a previous article, so please take a look

Automate Larastan with GitHub Actions!

Automate Larastan with GitHub Actions!

environment

Docker: 23.0.1

Laravel: 10.x

PHP: 8.2

MySQL: 8.0

Code content

The code we will use this time is as follows:

The code below is written in "/.github/workflows/unitTest.yml"

name: UnitTest on: push: jobs: unitTest: runs-on: ubuntu-latest steps: - name: Check out code uses: actions/ [email protected] - name: Set up Docker Compose run: docker compose up -d working-directory: ${{ github.workspace }} # Specify the root directory of the repository - name: composer install run: docker compose exec -i -t php-fpm composer install - name: thirty seconds sleep run: sleep 30 - name: Run PHPUnit run: docker compose exec -i -t php-fpm vendor/bin/phpunit

Based on the code above, we will explain in parts how UnitTest runs automatically

I will also reiterate in this article what I explained in my previous Larastan article

name:

This field specifies the name of the workflow to be registered in GitHub Actions

By entering this, it will be displayed on the GitHub Actions screen

Here, the name "UnitTest" is registered

If not registered, the relative path of the file will be registered

In this case, it will be registered with the name "./.github/workflows/UnitTest.yml"

on:

You can decide what action will trigger the workflow to run

This is called a "trigger."

The code we will use this time is set to trigger "pushing to GitHub" and run the UnitTest

You can not only set the timing of the push, but also set detailed triggers, such as triggering when a pull request is created, or only when a specified branch is pushed

jobs:

This is the section where you write the processes that will be executed within the workflow

For example, to run UnitTest, you need to execute commands such as starting Docker and installing composer

This section describes such operations

unitTest:

Job creation and job name

You can use the name key in the job to set the name that will be displayed in the GitHub UI, but if not set, it will be replaced by the job name

runs-on:

Defines the type of virtual machine


There are three options for configuring a virtual machine:
a GitHub hosted runner
, a runner larger than the GitHub hosted runner,
or a self-hosted runner

For this example, we will use the first GitHub hosted runner (hereinafter referred to as the runner)

There are several types of runners, and you can choose from three: Linux, Windows, and macOS

Basically, there should be no problem with using Linux, but please choose according to your environment

This time we are using the latest Ubuntu as a virtual machine

uses:

Specifies the action to perform

The action used here is a module called "actions/checkout"

As a role, it is used to use the code in the repository

run: (first)

Here we are running the command docker compose up -d

Since we are assuming that the environment will be set up using Docker, we will start Docker here

working-directory:

This specifies the directory in which to work

This time, we are writing ${{ github.workspace }}

This allows you to get the absolute path from the default working directory

Docker is started with the obtained path

run: (second)

The second run installs composer

The command is running docker compose exec -i -t php composer install

The command says php, but please enter the name of your php container

By writing the above command, composer install will be executed within the php container

run: (third)

The third run executes the sleep command

The command executes sleep 30

After docker compose up, mysql may not be fully started yet,

In this situation, the UnitTest cannot be executed properly, so the processing is delayed by 30 seconds

run: (fourth)

The final run, the fourth run, is where we run the UnitTest

As with the second run, the UnitTest execution command is written in the php container

In this command docker compose exec -i -t php vendor/bin/phpunit please replace php the name of your php container

Now we can finally run the UnitTest

summary

What did you think?

You can automate a lot of things with GitHub Actions

It's hard to implement, but once you've done so, you can increase your work efficiency!

thank you very much

lastly

The System Development Department, to which I belong, has launched a service site called "SEKARAKU Lab."
Beyond offers a one-stop service for everything from server design and construction to operation, so if you have any problems with server-side development, please feel free to contact us.

● SEKARAKU Lab: https://sekarakulab.beyondjapan.com

If you found this article useful, please click [Like]!
4
Loading...
4 votes, average: 1.00 / 14
1,051
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Matsuki

I like poker and rugby.
My favorite Marvel character is "Hulk"

My motto is concentration x time + luck