githubgithub-actionsphing

GitHub Actions: access service from an 'use' statement


I've a small workflow for testing my app. It requires MySQL, and I use it as a service so I can specify which version I want. Tests run through phing, and I had to fork and customize the official image adding some commands I require.

The Phing target runs some commands, like importing data into the MySQL DB. It appears the Run phing tests step is unable to connect to the maindb instance.

According to docs:

The hostname of the service container is automatically mapped to the label name. For example, if you create a service container with the label redis, the hostname of the service container is redis.

But the action has the following output:

[echo] mysql -udbuser -p --port=3306 -hmaindb dbname -e 'source ./data/sql/testing.create.sql'
ERROR 2005 (HY000): Unknown server host 'maindb' (-3)

This is my workflow definition:

name: Unit tests
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
on:
  push:
    branches:
      - production

jobs:
  phpunit:
    runs-on: ubuntu-20.04
    timeout-minutes: 15

    services:
      maindb:
        image: mysql:5.7
        env:
          # Values from build/properties/default.properties
          MYSQL_DATABASE: dbname
          MYSQL_USER: dbuser
          MYSQL_PASSWORD: dbpass
        ports:
          - 3306:3306
    steps:
...
      - name: Run phing tests
        uses: maxxer/phing-github-action@main
        with:
            targets: runtests

How can I allow the phing-github-action image access the MySQL service? Thanks


Solution

  • I was missing the options tag in the MySQL service definition with health checks. The working definition is the following:

        services:
          maindb:
            image: mysql:5.7
            env:
              MYSQL_DATABASE: dbname
              MYSQL_USER: dbuser
              MYSQL_PASSWORD: dbpass
              MYSQL_RANDOM_ROOT_PASSWORD: yes
            ports:
              - 3306:3306
            options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    

    I also had to include MYSQL_RANDOM_ROOT_PASSWORD to make mysqladmin work.