infra: reorganize k8s manifests and update terraform for eks/alb auto… #9
This file contains hidden or 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
| name: Build, test and push Docker image | |
| on: | |
| push: | |
| branches: | |
| - master | |
| jobs: | |
| build-test-push: | |
| runs-on: ubuntu-latest | |
| env: | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| POSTGRES_DB: app | |
| POSTGRES_PORT: 5432 | |
| REDIS_PORT: 6379 | |
| API_PORT: 3000 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Authenticate with Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Configure Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Start PostgreSQL and Redis services | |
| run: | | |
| docker compose up -d database redis | |
| echo "Waiting for database to become healthy..." | |
| for i in {1..30}; do | |
| status=$(docker inspect --format='{{.State.Health.Status}}' database || echo "unknown") | |
| echo "database health: $status" | |
| if [ "$status" = "healthy" ]; then | |
| echo "Database is healthy ✅" | |
| break | |
| fi | |
| if [ "$status" = "unhealthy" ]; then | |
| echo "Database became unhealthy ❌" | |
| docker compose logs database | |
| exit 1 | |
| fi | |
| sleep 2 | |
| done | |
| echo "Waiting a bit for redis..." | |
| sleep 5 | |
| - name: Build API image for testing | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: false | |
| load: true | |
| platforms: linux/amd64 | |
| tags: chauhansomay/nodejs-api:ci-${{ github.sha }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Run smoke test against API container | |
| run: | | |
| # Dynamically detect the Docker Compose network | |
| NETWORK_NAME=$(docker network ls --filter "name=app-network" --format "{{.Name}}" | head -n 1) | |
| if [ -z "$NETWORK_NAME" ]; then | |
| echo "Could not find app-network, listing available networks:" | |
| docker network ls | |
| exit 1 | |
| fi | |
| echo "Using Docker network: $NETWORK_NAME" | |
| # Run container with Docker health check - no arbitrary timeouts | |
| docker run -d \ | |
| --name nodejs-api-test \ | |
| --network $NETWORK_NAME \ | |
| -p 3000:3000 \ | |
| -e NODE_ENV=production \ | |
| -e DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:${POSTGRES_PORT}/${POSTGRES_DB} \ | |
| -e REDIS_URL=redis://redis:${REDIS_PORT} \ | |
| --health-cmd="wget -qO- http://localhost:3000/health || exit 1" \ | |
| --health-interval=5s \ | |
| --health-timeout=3s \ | |
| --health-retries=60 \ | |
| --health-start-period=10s \ | |
| chauhansomay/nodejs-api:ci-${{ github.sha }} | |
| echo "Waiting for API container to become healthy..." | |
| while true; do | |
| status=$(docker inspect --format='{{.State.Health.Status}}' nodejs-api-test 2>/dev/null || echo "unknown") | |
| echo "API health: $status" | |
| if [ "$status" = "healthy" ]; then | |
| echo "API is healthy ✅" | |
| break | |
| fi | |
| if [ "$status" = "unhealthy" ]; then | |
| echo "API became unhealthy ❌, showing logs:" | |
| docker logs nodejs-api-test || true | |
| docker stop nodejs-api-test || true | |
| docker compose logs database redis || true | |
| exit 1 | |
| fi | |
| # Check if container exited unexpectedly | |
| if ! docker ps -q -f name=nodejs-api-test | grep -q .; then | |
| echo "API container exited unexpectedly ❌" | |
| docker logs nodejs-api-test || true | |
| exit 1 | |
| fi | |
| sleep 3 | |
| done | |
| echo "Smoke test passed ✅" | |
| docker stop nodejs-api-test | |
| - name: Build and push multi-arch image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: true | |
| platforms: linux/amd64,linux/arm64 | |
| tags: chauhansomay/nodejs-api:latest | |
| cache-from: type=gha | |
| - name: Cleanup containers | |
| if: always() | |
| run: | | |
| docker compose down |