Skip to content

Commit

Permalink
Merge pull request #498 from lanedirt/495-improve-docker-entrypoint-s…
Browse files Browse the repository at this point in the history
…o-db-migrations-and-composer-updates-run-automatically

Make db migrations and composer updates run automatically on docker container start
  • Loading branch information
lanedirt authored Dec 16, 2024
2 parents 47ee4e5 + 0233b46 commit af7c552
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 215 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DEBUGBAR_REMOTE_SITES_PATH=/var/www
DEBUGBAR_LOCAL_SITES_PATH=/path/to/your/project

DB_CONNECTION=mysql
DB_HOST=ogame-db
DB_HOST=ogamex-db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
Expand Down
2 changes: 1 addition & 1 deletion .env.example-prod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DEBUGBAR_REMOTE_SITES_PATH=/var/www
DEBUGBAR_LOCAL_SITES_PATH=/path/to/your/project

DB_CONNECTION=mysql
DB_HOST=ogame-db
DB_HOST=ogamex-db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
Expand Down
71 changes: 56 additions & 15 deletions .github/workflows/run-docker-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,62 @@ jobs:
- name: Modify Dockerfile to disable USER www (which is not supported by GitHub Actions, as it runs as root)
run: |
sed -i '/USER www/s/^/#/' ./Dockerfile
- name: Set up Docker Compose
run: |
# Build the images and start the services
docker compose -f docker-compose.prod.yml up -d
# Ensure the database is ready
docker compose exec -T ogame-db bash -c 'until mariadb -h "ogame-db" -u "root" -p"toor" -e "SELECT 1"; do sleep 1; done'
- name: Set Permissions
run: docker compose exec -T ogame-app chmod -R 777 /var/www
run: sudo chmod -R 777 /var/www
- name: Copy .env
run: docker compose exec -T ogame-app cp .env.example-prod .env
- name: Run Laravel setup commands
run: sudo cp .env.example-prod .env
- name: Build and start the services
run: |
docker compose exec -T ogame-app composer install --no-dev
docker compose exec -T ogame-app php artisan key:generate
- name: Run DB Migrations
run: docker compose exec -T ogame-app php artisan migrate --force
- name: Run configuration cache commands
run: docker compose exec -T ogame-app php artisan cache:clear && docker compose exec -T ogame-app php artisan config:cache && docker compose exec -T ogame-app php artisan route:cache && docker compose exec -T ogame-app php artisan view:cache
# Build the images and start the services
docker compose -f docker-compose.prod.yml up -d
- name: Test if the services are running
uses: nick-fields/retry@v3
with:
timeout_minutes: 5
max_attempts: 5
command: |
sleep 15
# Array of endpoints to test
declare -A endpoints=(
["Web"]="https://localhost:443/login"
)
failed=false
# Test HTTP endpoints
for name in "${!endpoints[@]}"; do
url="${endpoints[$name]}"
echo "Testing $name at $url"
# Store both response body and HTTP code
response=$(curl -k -s -w "\nHTTP_CODE=%{http_code}" "$url")
http_code=$(echo "$response" | grep "HTTP_CODE=" | cut -d= -f2)
body=$(echo "$response" | sed '$d') # Remove the last line (HTTP_CODE)
if [ "$http_code" -ne 200 ]; then
echo "❌ $name failed with HTTP $http_code at $url"
echo "Response body:"
echo "$body"
failed=true
else
echo "✅ $name responded with HTTP 200"
fi
done
# Exit with error if any service failed
if [ "$failed" = true ]; then
# Get container logs
echo "Container Logs ogamex-app:"
docker compose logs ogamex-app
echo "Container Logs ogamex-db:"
docker compose logs ogamex-db
echo "Container Logs ogamex-webserver:"
docker compose logs ogamex-webserver
echo "Container Logs ogamex-scheduler:"
docker compose logs ogamex-scheduler
# Restart containers for next test in case of failure
docker compose restart
exit 1
fi
46 changes: 0 additions & 46 deletions .github/workflows/run-tests-docker-compose-prod.yml

This file was deleted.

28 changes: 12 additions & 16 deletions .github/workflows/run-tests-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,23 @@ jobs:
- name: Modify Dockerfile to disable USER www (which is not supported by GitHub Actions, as it runs as root)
run: |
sed -i '/USER www/s/^/#/' ./Dockerfile
- name: Set Permissions
run: sudo chmod -R 777 /var/www
- name: Copy .env
run: sudo cp .env.example .env
- name: Set up Docker Compose
run: |
# Build the images and start the services
docker compose -f docker-compose.yml up -d
# Ensure the database is ready
docker compose exec -T ogame-db bash -c 'until mariadb -h "ogame-db" -u "root" -p"toor" -e "SELECT 1"; do sleep 1; done'
- name: Set Permissions
run: docker compose exec -T ogame-app chmod -R 777 /var/www
- name: Copy .env
run: docker compose exec -T ogame-app cp .env.example .env
- name: Run Laravel setup commands
- name: Wait 10 seconds
run: sleep 10
- name: Wait for application to be ready up to 60 seconds
run: |
docker compose exec -T ogame-app composer install
docker compose exec -T ogame-app php artisan key:generate
- name: Run DB Migrations
run: docker compose exec -T ogame-app php artisan migrate
- name: Run configuration cache commands
run: docker compose exec -T ogame-app php artisan cache:clear && docker compose exec -T ogame-app php artisan config:cache && docker compose exec -T ogame-app php artisan route:cache && docker compose exec -T ogame-app php artisan view:cache
# Wait for the application to be ready (max 60 seconds)
timeout 60 bash -c 'until docker compose exec -T ogamex-app php artisan migrate:status > /dev/null 2>&1; do echo "Waiting for application to be ready..." && sleep 2; done'
- name: Run Tests
run: docker compose exec -T ogame-app php artisan test
run: docker compose exec -T ogamex-app php artisan test
- name: Run custom Race Condition Tests
run: |
docker compose exec -T ogame-app php artisan test:race-condition-unitqueue
docker compose exec -T ogame-app php artisan test:race-condition-game-mission
docker compose exec -T ogamex-app php artisan test:race-condition-unitqueue
docker compose exec -T ogamex-app php artisan test:race-condition-game-mission
Loading

0 comments on commit af7c552

Please sign in to comment.