Automate your UnitTests with GitHub Actions

table of contents
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!
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
For virtual machine configuration,
a GitHub-hosted runner
, a runner larger than a GitHub-hosted runner
, or a self-hosted runner
you can choose from three options:
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,`docker compose up -d`is being executed.
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
In this example${{ github.workspace }}, we are using the notation
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 isdocker compose exec -i -t php composer installexecuting
The command includes "php," butthe name of your PHP containerplease replace this with
By writing the above command, composer install will be executed within the php container
run: (third)
The third run executes the sleep command
The commandsleep 30executes
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 the command`docker compose exec -i -t php vendor/bin/phpunit``php`the name of your PHP containerplease replace
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
We have launched "SEKARAKU Lab," a service site for the System Development Department to which I belong.
Beyond offers a one-stop service from server design and construction to operation, so please feel free to contact us if you have any problems with server-side development.
● SEKARAKU Lab:https://sekarakulab.beyondjapan.com
4
