Automate UnitTest with GitHub Actions
table of contents
Hello!
My name is Matsuki from the System Development Department.
Continuing from the previous article on GitHub Actions, this time we will be talking about GitHub Actions.
This time, the topic is ``Let's automate PHP's UnitTest by leaving it to GitHub Actions!''
Points to note
In this article, we will proceed with the article assuming that you are building 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 there.
● 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 above code, we will explain the mechanism by which UnitTest runs automatically.
This article also reiterates what was discussed in the previous Larastan article.
name:
This field describes 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 which actions trigger the workflow.
This is called a "trigger".
In the code used this time, "Push to GitHub" is the trigger to run UnitTest.
In addition to the push timing, you can also set triggers in detail, such as triggering when a pull request is created, or only when a specified branch is pushed.
jobs:
This is an item where you write the processes that will be executed within the workflow.
For example, to run UnitTest, you need execution commands such as starting Docker and installing composer.
This item describes such operations.
unitTest:
Job creation and job name.
You can use the name key in a job to set the name that will be displayed in GitHub's UI, but if you don't set it, it will be replaced with the job's name.
runs-on:
Define the virtual machine type.
When setting up a virtual machine,
you can choose from three options: GitHub hosted runner
, runner larger than GitHub hosted runner
, and self-hosted runner
This time we will use the first GitHub hosted runner (hereinafter referred to as runner).
There are several types of runners, and you can choose from three: Linux, Windows, and macOS.
Basically, I think there is no problem with using Linux, but please choose according to your environment.
This time I am using the latest version of Ubuntu as a virtual machine.
uses:
Specify the action to perform.
The action used here is a module called "actions/checkout".
The role is used to use code from the repository.
run: (1st)
Here we are running the docker compose up -d
Since this time we will be building an environment using docker, we will start docker here.
working-directory:
This specifies the directory in which to work.
This time I am writing ${{ github.workspace }}
This allows you to get the absolute path from the default working directory.
Starting docker with the obtained path.
run: (second)
The second run installs composer.
The command is running docker compose exec -i -t php composer install
Although php is written in the middle of the command, 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 uses the sleep command.
The command is running sleep 30
Mysql may not have started completely after docker compose up, so
UnitTest cannot run properly in that situation, so we are delaying the process for 30 seconds.
run: (4th)
UnitTest is performed in the fourth and final run.
Similar to the second run, the UnitTest execution command is written inside the php container.
this command docker compose exec -i -t php vendor/bin/phpunit
please replace php the name of your php container
At this point I was finally able to run UnitTest.
summary
What did you think?
You can automate many things with GitHub Actions,
It will be difficult to implement it, but once it is implemented, you will be able to increase your work efficiency!
thank you very much.
lastly
We have opened the service site "SEKARAKU Lab" for the system development department to which I belong.
Beyond is a one-stop service for everything from server design and construction to operation, so if you have any trouble with server-side development, please feel free to contact us.
● SEKARAKU Lab: https://sekarakulab.beyondjapan.com