Ok, so we have automated tests that check our code for potential bugs. But we need to run those tests manually every time. Wouldn't it be cool to automate that process?
This is a part of the so-called CI/CD process, automating some checks before deployment or merging the Pull Request.
One of the most common and easy-to-setup procedures is running tests automatically on each Pull Request. Then, the author of the PR immediately knows there's something to fix before the merging person (which may be the same developer) comes to approve the Pull Request.
The easiest tool to set it up is GitHub Actions. In this lesson, I will show you how to configure automated tests, and in the following lessons, we will add more automatic checks to this process.
GitHub Actions has a limited free version, you can find all the pricing on this official GitHub page.
To enable GitHub actions, we need only add a .github/workflows/[whatever_name].yml
file with instructions to GitHub.
That .yml
should contain the commands that would be executed on the virtual GitHub environment: setting up PHP, cloning our project's branch, and running the tests.
In our project, we did it in a few "attempts", let me show you step-by-step.
First, we create the GitHub issue to add GitHub Actions:
Then, we create a feature branch for this:
git checkout -b feature/github-actions
And now, we can push the code into that feature branch.
Get used to this process of issue -> branch -> push -> pull request -> merge, I want it to become a habit for you.
Next, here's the example of the initial .yml
file to run the tests automatically:
.github/workflows/test-suite.yml
name: Run tests on: [ push, pull_request ] jobs: tests: name: Run tests runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2' extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv coverage: none - name: Run composer install run: composer install -n --prefer-dist - name: Prepare Laravel Application run: | cp .env.example .env php artisan key:generate - name: Run tests run: php artisan test
With this file, we tell GitHub to:
on: [ push, pull_request ]
).env
and generating the key.php artisan test
.We push it to our feature branch, referencing the ID of the issue above:
git add .git commit -m "Adding Github Actions - resolves #3"git push origin feature/github-actions
This push automatically triggered the check, which, unfortunately, failed.
And the error message was about the database connection:
So, we need to make a few tweaks.
The second attempt was this change in the .yml
file:
And, of course, this change in the newly created .env.ci
file:
DB_CONNECTION=sqlite# DB_HOST=127.0.0.1# DB_PORT=3306# DB_DATABASE=laravel# DB_USERNAME=root# DB_PASSWORD=
But even then - it failed because we didn't create the SQLite database file. So, with another commit, we added the creation of a Database file:
.github/workflows/test-suite.yml
name: Run tests on: [ push, pull_request ] jobs: tests: name: Run tests runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2' extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv coverage: none - name: Run composer install run: composer install -n --prefer-dist - name: Prepare Laravel Application run: | cp .env.example .env php artisan key:generate - name: Setup Sqlite # [tl! add:start] run: touch database/database.sqlite # [tl! add:end] - name: Run tests run: php artisan test
After this commit, we still failed:
With an error message:
This time, it was because of Vite - we have not compiled the assets. So, we added the compilation of assets to the .yml
file:
.github/workflows/test-suite.yml
name: Run tests on: [ push, pull_request ] jobs: tests: name: Run tests runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2' extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv coverage: none - name: Run composer install run: composer install -n --prefer-dist - name: Run NPM install # [tl! add:start] run: npm install - name: Compile Assets for UI Testing run: npm run build # [tl! add:end] - name: Prepare Laravel Application run: | cp .env.example .env php artisan key:generate - name: Setup Sqlite # [tl! add:start] run: touch database/database.sqlite # [tl! add:end] - name: Run tests run: php artisan test
And finally, we have a successful run:
So, finally, now we have a successful process set up.
Whenever we make a Pull Request, GitHub will automatically run tests and inform us if something goes wrong.
Great. Now, we can proceed with more features. In the next lessons, we will add a few more Laravel automation tools: Pint and Larastan.