'How can I wait for the container to be healthy in GitHub action?

I am using GitHub action to do some automation test and my application was developed in docker.

name: Docker Image CI

on:
  push:
    branches: [ master]
  pull_request:
    branches: [ master]

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Build the Docker image
      run: docker-compose build

    - name: up mysql and apache container runs
      run: docker-compose up -d 

    - name: install dependencies
      run: docker exec  myapp php composer.phar install
  
    - name: show running container
      run: docker ps 


    - name: run unit test
      run: docker exec  myapp ./vendor/bin/phpunit 

At the step 'show running container', I can see that all the containers are running but for the MySQL, the status is (health: starting). Thus, my unit test cases all failed as it requires a connection to MySQL. So may I know if there is a way to start the unit case only when the MySQL container's status is healthy?



Solution 1:[1]

As the documentation states:

To handle this, design your application to attempt to re-establish a connection to the database after a failure. If the application retries the connection, it can eventually connect to the database.

If you can't implement this at the moment, you can write some simple script that will indefinitely try a simple statement on the database. Once the script succeed you exit loop and start your unit tests. Check the documentation link I've provided, you'll find there an example of such script (wait-for-it.sh).

Solution 2:[2]

You can use thegabriele97/dockercompose-health-action

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Check services healthiness
        uses: thegabriele97/dockercompose-health-action@main
        with:
          timeout: '60'
          workdir: 'src'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anemyte
Solution 2 Gabriele La Greca