CI: Wait for DB
WARNING: This script creates an infinity loop in your CI, make sure to have a timeout.
Initially I though of using wait-for-it.sh but to add a new script (aka, dependency) for this whole thing, besides it does not work as I want.
I was expecting for the script to go on in one infinity loop until the connection is healthy.
For MySQL:
#!/bin/bash
while ! mysql -h db -D app_test -e "select 1"
do
echo "waiting for MySQL"
sleep 5
done
For PostgreSQL:
#!/bin/bash
export PGPASSWORD=postgres
while ! echo "select 1" | psql -U postgres -h db postgres
do
echo "waiting for PostgreSQL"
sleep 2
done
I put this script right before I need to use the database, for example:
- run: docker-compose up -d db
- run: docker-compose run --rm -e RAILS_ENV=test app bundle install --path=vendor/bundle --jobs=4 --retry=3
- run: docker-compose run db /app/.github/wait-for-db.sh
- run: docker-compose run --rm -e RAILS_ENV=test app bundle exec rake db:migrate
This is what I got in my Github Action:
P. S.: Don't forget to make your script executable using chmod +x
.