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

Webmentions

Rasmus C Nielsen liked on 8th December 2020
Songhua Hu liked on 8th December 2020
Kidi Pridi liked on 8th December 2020
8ack2Lobby liked on 8th December 2020
Erin Dalzell liked on 8th December 2020
Flynsarmy replied on 8th December 2020
You guys are so good to the open source community. Thank you
Seba Ramírez Pastore liked on 8th December 2020
Hassan Haseed liked on 8th December 2020
AKANBI TUNDE LAWAL liked on 8th December 2020
Ryan Chandler liked on 8th December 2020
Ruben Van Assche retweeted on 8th December 2020
Mark Topper liked on 8th December 2020
Ruben Van Assche liked on 8th December 2020
Ross Alexander liked on 8th December 2020
Peter Brinck 🤘 liked on 8th December 2020
Krystian Podemski liked on 8th December 2020
Huda Elzubair liked on 8th December 2020
José Cage liked on 5th November 2020
Roman Pronskiy liked on 5th November 2020
albert liked on 4th November 2020
Martin Medina liked on 4th November 2020
Rocco Howard liked on 4th November 2020
Sherief El-Feky liked on 4th November 2020
Jake Casto liked on 4th November 2020
Richard Radermacher liked on 3rd November 2020
Danny R. liked on 3rd November 2020
Spatie retweeted on 3rd November 2020
ダビッド トレス retweeted on 3rd November 2020
ダビッド トレス liked on 3rd November 2020
Craig Paul liked on 3rd November 2020
Songhua Hu liked on 3rd November 2020
airios liked on 3rd November 2020
Tauseef shah liked on 3rd November 2020
amr replied on 3rd November 2020
Much appreciated @freekmurze
Edwin I Arellano liked on 3rd November 2020
Miguel Piedrafita 🚀 liked on 3rd November 2020
Mike liked on 3rd November 2020
KGG liked on 3rd November 2020
Niels liked on 3rd November 2020
Jignesh Joisar liked on 3rd November 2020
Ryan Chandler liked on 3rd November 2020
Unknown liked on 3rd November 2020
Jiří Žižka liked on 3rd November 2020
Dario Diaz liked on 3rd November 2020
Zubair Mohsin liked on 3rd November 2020
Lennart Fischer liked on 3rd November 2020
Daniel Lucas liked on 3rd November 2020
Mark Topper liked on 3rd November 2020
DIEGO Araujo liked on 3rd November 2020
Kibru Demeke Tadesse liked on 3rd November 2020
Vaggelis Yfantis liked on 3rd November 2020
. liked on 3rd November 2020
Mariano Paz liked on 3rd November 2020
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