Last active
May 6, 2024 17:26
-
-
Save Justintime50/2de9303a491c22627ee502aaa7b1f289 to your computer and use it in GitHub Desktop.
Sets up a Laravel project for the first time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# shellcheck disable=SC2104 | |
# The following script will setup the project for the first time for local dev | |
# To run the project after setup, use `docker compose up -d` | |
set -e | |
REPO_NAME="$1" | |
CONTAINER_NAME="${2:-$1}" | |
DATABASE="$3" | |
SQLITE_DATABASE_FILEPATH=storage/database/database.sqlite | |
setup() { | |
_setup_dependencies | |
_setup_sqlite | |
_docker_build | |
# Run database migrations once the database container is up and able to accept connections | |
for ((ATTEMPT = 0; ATTEMPT <= 5; ATTEMPT += 1)); do | |
if ! _healthcheck; then | |
echo "Database is not ready for connections after attempt #$ATTEMPT, retrying..." | |
sleep 5 | |
else | |
if [ -n "$DATABASE" ]; then | |
echo "Database passed healthcheck, migrating and seeding..." | |
just migrate seed | |
fi | |
echo "Setup complete!" | |
exit 0 | |
fi | |
done | |
echo "Healthchecks failed on setup!" | |
exit 1 | |
} | |
_setup_dependencies() { | |
just install | |
php artisan optimize:clear | |
php artisan key:generate | |
} | |
_setup_sqlite() { | |
if [ "$DATABASE" = "sqlite" ]; then | |
mkdir -p storage/database | |
touch "$SQLITE_DATABASE_FILEPATH" | |
echo "SQLite database setup." | |
fi | |
} | |
_docker_build() { | |
docker compose up -d --build --force-recreate | |
} | |
_healthcheck() { | |
if ! docker ps | grep -q "$REPO_NAME-$CONTAINER_NAME-1"; then | |
echo "Container not found!" | |
break | |
fi | |
if [ "$DATABASE" = "has_database" ] || [ "$DATABASE" = "mysql" ]; then | |
if ! docker ps | grep -q "$REPO_NAME-$CONTAINER_NAME-db-1"; then | |
echo "Database container not found!" | |
break | |
fi | |
docker exec -t "$REPO_NAME-$CONTAINER_NAME-db-1" mariadb -uroot -ppassword -e "show databases;" &>/dev/null || false | |
elif [ "$DATABASE" = "sqlite" ]; then | |
if ! [ -f "$SQLITE_DATABASE_FILEPATH" ]; then | |
echo "$SQLITE_DATABASE_FILEPATH not found!" | |
break | |
fi | |
else | |
echo "No database configured, skipping healthcheck." | |
fi | |
} | |
setup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment