Oh Dear! monitors your entire website, not just the homepage. You'll get a notification as soon as your website is down, a monthly uptime report, a warning a few days before your SSL certificate expires and much more! Start your free 10 day trial now!

How to use a MySQL database on GitHub Actions

Original – by Freek Van der Herten – 2 minute read

Recently we started using GitHub Actions to test all our packages. You can read more about our general setup in this blog post.

For most of the packages, this works great. However, some of our packages, such as Laravel Tags, use JSON functions that are not available in SQLite. Luckily it's straightforward to use a database like MySQL in GitHub Actions.

In your test workflow, you need to add MySQL to the services.

services:
    mysql:
        image: mysql:5.7
        env:
            MYSQL_ALLOW_EMPTY_PASSWORD: yes
            MYSQL_DATABASE: laravel_tags
        ports:
            - 3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

In the step that executes the test, you should add an env variable DB_PORT. Laravel uses that environment variable to set up the connection to the database.

- name: Execute tests
  run: vendor/bin/phpunit
  env:
      DB_PORT: ${{ job.services.mysql.ports[3306] }}

In phpunit.xml.dist you should add this section.

<php>
    <env name="DB_CONNECTION" value="mysql"/>
    <env name="DB_USERNAME" value="root"/>
    <env name="DB_DATABASE" value="laravel_tags"/>
    <env name="DB_HOST" value="127.0.0.1" />
    <env name="DB_PORT" value="3306" />
</php>

That DB_PORT there is used for local tests. On GitHub Actions, it will be overwritten by the port set in the workflow.

And that is all there is to it. Take a look at the entire GitHub Actions workflow and phpunit config file, to get a little bit more context where you need to use the code snippets above.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

You can comment on this post by replying to this tweet.
José Cage liked on 6th March 2020
Siddharaj Solanki liked on 5th March 2020
Scorp974 liked on 5th March 2020
Matthew Poulter liked on 5th March 2020
bernard kssy 💻 Busy retweeted on 5th March 2020
Tony Messias liked on 5th March 2020
Thibault Lavoisey liked on 4th March 2020
Aryeh Raber liked on 4th March 2020
Abbah Anoh retweeted on 4th March 2020
mr flash liked on 4th March 2020
Freek Van der Herten replied on 4th March 2020
Lol
David Heremans replied on 4th March 2020
Stop sending messages from the future!
Michael Aguiar liked on 4th March 2020
Spatie retweeted on 4th March 2020
Salman Zafar liked on 4th March 2020
Rati Wannapanop liked on 4th March 2020
冻鱼 liked on 4th March 2020
Mike liked on 4th March 2020
Daniele Rosario liked on 4th March 2020
. liked on 4th March 2020
togo liked on 4th March 2020
MD Iyasin Arafat retweeted on 4th March 2020
Andre Sayej liked on 4th March 2020
simon davies ™ liked on 4th March 2020
Ruud van Zuidam liked on 4th March 2020
Jorge González liked on 4th March 2020
Dholund Trum liked on 4th March 2020
MD Iyasin Arafat liked on 4th March 2020
Möhre liked on 4th March 2020
Matthijs Lubbers liked on 4th March 2020
Owen Voke (pxgamer) liked on 4th March 2020
Lennart Fischer liked on 4th March 2020
IamM liked on 4th March 2020
Stephen Jude liked on 4th March 2020