Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free 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.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month 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

What are your thoughts on "How to use a MySQL database on GitHub Actions"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.