🔧 refactor: simplify local CI/CD script by assuming Docker availability

- Remove conditional Docker availability checks
- Assume Docker is always available (required for workflow)
- Simplify logic flow
- Maintain same functionality but cleaner code
- Match CI/CD workflow assumptions

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
2026-04-07 23:41:15 +02:00
parent 18213f365d
commit 22099d0ac6

View File

@@ -16,17 +16,17 @@ if ! command -v go >/dev/null 2>&1; then
exit 1 exit 1
fi fi
# Assume Docker is available (required for this workflow)
if ! command -v docker >/dev/null 2>&1; then if ! command -v docker >/dev/null 2>&1; then
echo "⚠️ Docker not found. Docker build steps will be skipped" echo " Docker is required for this CI/CD workflow"
HAS_DOCKER=false echo "Please install Docker and Docker Compose plugin"
else exit 1
HAS_DOCKER=true fi
# Check for docker compose plugin # Check for docker compose plugin
if ! docker compose version >/dev/null 2>&1; then if ! docker compose version >/dev/null 2>&1; then
echo "⚠️ Docker Compose plugin not found. Installing..." echo "⚠️ Docker Compose plugin not found. Installing..."
sudo apt-get update && sudo apt-get install -y docker-compose-plugin sudo apt-get update && sudo apt-get install -y docker-compose-plugin
fi
fi fi
echo "✅ Environment ready" echo "✅ Environment ready"
@@ -41,74 +41,60 @@ echo ""
# 3. Check for Docker cache # 3. Check for Docker cache
echo "3. Checking for Docker build cache..." echo "3. Checking for Docker build cache..."
if [ "$HAS_DOCKER" = true ]; then IMAGE_NAME="gitea.arcodange.lab/arcodange/dance-lessons-coach-build-cache:$DEPS_HASH"
IMAGE_NAME="gitea.arcodange.lab/arcodange/dance-lessons-coach-build-cache:$DEPS_HASH"
# Try to pull the cache image # Try to pull the cache image
if docker pull "$IMAGE_NAME" >/dev/null 2>&1; then if docker pull "$IMAGE_NAME" >/dev/null 2>&1; then
echo "✅ Cache hit - using existing build cache" echo "✅ Cache hit - using existing build cache"
USE_DOCKER_CACHE=true USE_DOCKER_CACHE=true
else
echo "⚠️ Cache miss - will build without cache"
USE_DOCKER_CACHE=false
fi
else else
echo "⚠️ Docker not available - running natively" echo "⚠️ Cache miss - will build without cache"
USE_DOCKER_CACHE=false USE_DOCKER_CACHE=false
fi fi
echo "" echo ""
# 4. Start PostgreSQL with Docker Compose # 4. Start PostgreSQL with Docker Compose
echo "4. Starting PostgreSQL..." echo "4. Starting PostgreSQL..."
if [ "$HAS_DOCKER" = true ]; then docker compose -f docker-compose.yml up -d postgres
docker compose -f docker-compose.yml up -d postgres
# Wait for PostgreSQL to be ready # Wait for PostgreSQL to be ready
echo "Waiting for PostgreSQL to be ready..." echo "Waiting for PostgreSQL to be ready..."
for i in {1..30}; do for i in {1..30}; do
if docker exec dance-lessons-coach-postgres pg_isready -U postgres; then if docker exec dance-lessons-coach-postgres pg_isready -U postgres; then
echo "✅ PostgreSQL is ready!" echo "✅ PostgreSQL is ready!"
break break
fi
echo "Waiting for PostgreSQL... ($i/30)"
sleep 2
done
# Set PostgreSQL environment variables for BDD tests
# Use container name for Docker, localhost for native (port mapping)
if [ "$HAS_DOCKER" = true ]; then
export DLC_DATABASE_HOST="dance-lessons-coach-postgres"
else
export DLC_DATABASE_HOST="localhost"
fi fi
export DLC_DATABASE_PORT=5432 echo "Waiting for PostgreSQL... ($i/30)"
export DLC_DATABASE_USER=postgres sleep 2
export DLC_DATABASE_PASSWORD=postgres done
export DLC_DATABASE_NAME=dance_lessons_coach_bdd_test
export DLC_DATABASE_SSL_MODE=disable # Set PostgreSQL environment variables for BDD tests
else export DLC_DATABASE_HOST="dance-lessons-coach-postgres"
echo "⚠️ Docker not available - PostgreSQL tests will be skipped" export DLC_DATABASE_PORT=5432
fi export DLC_DATABASE_USER=postgres
export DLC_DATABASE_PASSWORD=postgres
export DLC_DATABASE_NAME=dance_lessons_coach_bdd_test
export DLC_DATABASE_SSL_MODE=disable
echo "" echo ""
# 5. Check dependencies # 5. Install dependencies
echo "5. Checking dependencies..."
if [ "$USE_DOCKER_CACHE" = true ]; then if [ "$USE_DOCKER_CACHE" = true ]; then
echo "5. Checking dependencies..."
echo "✅ Using pre-installed dependencies from Docker cache" echo "✅ Using pre-installed dependencies from Docker cache"
# No need to run go mod tidy - dependencies are already in the cache
else else
echo "Running natively - ensuring dependencies are up to date..." echo "5. Installing dependencies..."
go mod tidy go mod tidy
fi fi
echo "✅ Dependencies ready" echo "✅ Dependencies ready"
echo "" echo ""
# 6. Generate Swagger Docs # 6. Generate Swagger Docs
echo "6. Generating Swagger documentation..."
if [ "$USE_DOCKER_CACHE" = true ]; then if [ "$USE_DOCKER_CACHE" = true ]; then
echo "6. Generating Swagger documentation..."
echo "Running in Docker Compose container..." echo "Running in Docker Compose container..."
docker compose -f docker-compose.build.yml run -w /workspace/pkg/server build-cache sh -c "go generate" docker compose -f docker-compose.build.yml run -w /workspace/pkg/server build-cache sh -c "go generate"
else else
echo "6. Generating Swagger documentation..."
echo "Running natively..." echo "Running natively..."
cd pkg/server && go generate cd pkg/server && go generate
cd ../.. cd ../..
@@ -117,11 +103,12 @@ echo "✅ Swagger documentation generated"
echo "" echo ""
# 7. Build and test # 7. Build and test
echo "7. Building and testing..."
if [ "$USE_DOCKER_CACHE" = true ]; then if [ "$USE_DOCKER_CACHE" = true ]; then
echo "7. Building and testing..."
echo "Running in Docker Compose container..." echo "Running in Docker Compose container..."
docker compose -f docker-compose.build.yml run -w /workspace build-cache sh -c "go build ./..." docker compose -f docker-compose.build.yml run -w /workspace build-cache sh -c "go build ./..."
else else
echo "7. Building and testing..."
echo "Running natively..." echo "Running natively..."
go build ./... go build ./...
fi fi
@@ -141,12 +128,6 @@ if [ "$USE_DOCKER_CACHE" = true ]; then
sh -c "go test ./... -coverprofile=coverage.out -v && go tool cover -func=coverage.out > coverage.txt" sh -c "go test ./... -coverprofile=coverage.out -v && go tool cover -func=coverage.out > coverage.txt"
else else
echo "Running natively with Docker Compose PostgreSQL..." echo "Running natively with Docker Compose PostgreSQL..."
export DLC_DATABASE_HOST=dance-lessons-coach-postgres
export DLC_DATABASE_PORT=5432
export DLC_DATABASE_USER=postgres
export DLC_DATABASE_PASSWORD=postgres
export DLC_DATABASE_NAME=dance_lessons_coach_bdd_test
export DLC_DATABASE_SSL_MODE=disable
go test ./... -coverprofile=coverage.out -v go test ./... -coverprofile=coverage.out -v
go tool cover -func=coverage.out > coverage.txt go tool cover -func=coverage.out > coverage.txt
fi fi
@@ -154,11 +135,12 @@ echo "✅ Tests passed"
echo "" echo ""
# 8. Build binaries # 8. Build binaries
echo "8. Building binaries..."
if [ "$USE_DOCKER_CACHE" = true ]; then if [ "$USE_DOCKER_CACHE" = true ]; then
echo "8. Building binaries..."
echo "Running in Docker Compose container..." echo "Running in Docker Compose container..."
docker compose -f docker-compose.build.yml run -w /workspace build-cache sh -c "./scripts/build.sh" docker compose -f docker-compose.build.yml run -w /workspace build-cache sh -c "./scripts/build.sh"
else else
echo "8. Building binaries..."
echo "Running natively..." echo "Running natively..."
./scripts/build.sh ./scripts/build.sh
fi fi
@@ -190,174 +172,170 @@ CURRENT_VERSION="$MAJOR.$MINOR.$PATCH${PRERELEASE:+-$PRERELEASE}"
echo "📊 Current version: $CURRENT_VERSION" echo "📊 Current version: $CURRENT_VERSION"
echo "" echo ""
# 7. Local Docker build instructions # 10. Local Docker build instructions
if [ "$HAS_DOCKER" = true ]; then echo "🐳 LOCAL DOCKER BUILD INSTRUCTIONS"
echo "🐳 LOCAL DOCKER BUILD INSTRUCTIONS" echo "================================"
echo "================================" echo ""
echo ""
echo "1. Build Docker image locally (development):" echo "1. Build Docker image locally (development):"
echo " docker build -t dance-lessons-coach:$CURRENT_VERSION ." echo " docker build -t dance-lessons-coach:$CURRENT_VERSION ."
echo "" echo ""
echo "2. Build production image using docker/Dockerfile.prod:" echo "2. Build production image using docker/Dockerfile.prod:"
echo " # Note: Local docker/Dockerfile.prod uses 'latest' tag for testing" echo " # Note: Local docker/Dockerfile.prod uses 'latest' tag for testing"
echo " docker build -t dance-lessons-coach-prod:$CURRENT_VERSION -f docker/Dockerfile.prod ." echo " docker build -t dance-lessons-coach-prod:$CURRENT_VERSION -f docker/Dockerfile.prod ."
echo " # For CI/CD, the workflow generates correct docker/Dockerfile.prod with dependency hash" echo " # For CI/CD, the workflow generates correct docker/Dockerfile.prod with dependency hash"
echo "" echo ""
echo "3. Compare image sizes:" echo "3. Compare image sizes:"
echo " docker images | grep dance-lessons-coach" echo " docker images | grep dance-lessons-coach"
echo "" echo ""
echo "4. Tag the image:" echo "4. Tag the image:"
echo " docker tag dance-lessons-coach:$CURRENT_VERSION dance-lessons-coach:latest" echo " docker tag dance-lessons-coach:$CURRENT_VERSION dance-lessons-coach:latest"
echo "" echo ""
echo "3. Test the local image (check port availability first):" echo "5. Test the local image (check port availability first):"
echo " docker run -d -p 8080:8080 dance-lessons-coach:$CURRENT_VERSION" echo " docker run -d -p 8080:8080 dance-lessons-coach:$CURRENT_VERSION"
echo " # Or use alternative port if 8080 is in use:" echo " # Or use alternative port if 8080 is in use:"
echo " docker run -d -p 8081:8080 dance-lessons-coach:$CURRENT_VERSION" echo " docker run -d -p 8081:8080 dance-lessons-coach:$CURRENT_VERSION"
echo "" echo ""
echo "4. Branch-specific container naming (recommended):" echo "6. Branch-specific container naming (recommended):"
echo " BRANCH=\"(git rev-parse --abbrev-ref HEAD | tr '/' '-')" echo " BRANCH=\"$(git rev-parse --abbrev-ref HEAD | tr '/' '-')\""
echo " docker run -d -p 8080:8080 --name dance-lessons-coach-\"$BRANCH\" dance-lessons-coach:$CURRENT_VERSION" echo " docker run -d -p 8080:8080 --name dance-lessons-coach-\"$BRANCH\" dance-lessons-coach:$CURRENT_VERSION"
echo "" echo ""
echo "5. Test API endpoints:" echo "7. Test API endpoints:"
echo " curl http://localhost:8080/api/health" echo " curl http://localhost:8080/api/health"
echo " curl http://localhost:8080/api/v1/greet/YourName" echo " curl http://localhost:8080/api/v1/greet/YourName"
echo "" echo ""
echo "5. Clean up:" echo "8. Clean up:"
echo " docker stop <container_id> && docker rm <container_id>" echo " docker stop <container_id> && docker rm <container_id>"
echo "" echo ""
echo "💡 Tip: Use 'docker images' to see your built images" echo "💡 Tip: Use 'docker images' to see your built images"
echo "💡 Use 'docker ps' to see running containers" echo "💡 Use 'docker ps' to see running containers"
echo "" echo ""
# Ask if user wants to build Docker image now # Ask if user wants to build Docker image now
read -p "🚀 Do you want to build the Docker image now? (y/n): " -n 1 -r read -p "🚀 Do you want to build the Docker image now? (y/n): " -n 1 -r
echo "" echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "🐳 Building Docker image..." echo "🐳 Building Docker image..."
read -p "📋 Build (d)development or (p)production image? [d/p]: " -n 1 -r read -p "📋 Build (d)development or (p)production image? [d/p]: " -n 1 -r
echo "" echo ""
if [[ $REPLY =~ ^[Pp]$ ]]; then if [[ $REPLY =~ ^[Pp]$ ]]; then
echo "🏗️ Building production image with docker/Dockerfile.prod..." echo "🏗️ Building production image with docker/Dockerfile.prod..."
docker build -t dance-lessons-coach-prod:$CURRENT_VERSION -f docker/Dockerfile.prod . docker build -t dance-lessons-coach-prod:$CURRENT_VERSION -f docker/Dockerfile.prod .
docker tag dance-lessons-coach-prod:$CURRENT_VERSION dance-lessons-coach-prod:latest docker tag dance-lessons-coach-prod:$CURRENT_VERSION dance-lessons-coach-prod:latest
echo "✅ Production Docker image built: dance-lessons-coach-prod:$CURRENT_VERSION" echo "✅ Production Docker image built: dance-lessons-coach-prod:$CURRENT_VERSION"
CONTAINER_IMAGE="dance-lessons-coach-prod:$CURRENT_VERSION" CONTAINER_IMAGE="dance-lessons-coach-prod:$CURRENT_VERSION"
else
echo "🏗️ Building development image with Dockerfile..."
docker build -t dance-lessons-coach:$CURRENT_VERSION .
docker tag dance-lessons-coach:$CURRENT_VERSION dance-lessons-coach:latest
echo "✅ Development Docker image built: dance-lessons-coach:$CURRENT_VERSION"
CONTAINER_IMAGE="dance-lessons-coach:$CURRENT_VERSION"
fi
echo ""
# Check if port 8080 is available
echo "🔍 Checking port availability..."
if lsof -i :8080 > /dev/null 2>&1; then
echo "⚠️ Port 8080 is already in use"
read -p "🚀 Do you want to use a different port? (y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
read -p "Enter port number (e.g., 8081): " CUSTOM_PORT
echo ""
PORT=$CUSTOM_PORT
else else
echo "🏗 Building development image with Dockerfile..." echo " Using port 8080 anyway (may fail if service is running)"
docker build -t dance-lessons-coach:$CURRENT_VERSION .
docker tag dance-lessons-coach:$CURRENT_VERSION dance-lessons-coach:latest
echo "✅ Development Docker image built: dance-lessons-coach:$CURRENT_VERSION"
CONTAINER_IMAGE="dance-lessons-coach:$CURRENT_VERSION"
fi
echo ""
# Check if port 8080 is available
echo "🔍 Checking port availability..."
if lsof -i :8080 > /dev/null 2>&1; then
echo "⚠️ Port 8080 is already in use"
read -p "🚀 Do you want to use a different port? (y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
read -p "Enter port number (e.g., 8081): " CUSTOM_PORT
echo ""
PORT=$CUSTOM_PORT
else
echo " Using port 8080 anyway (may fail if service is running)"
PORT=8080
fi
else
echo "✅ Port 8080 is available"
PORT=8080 PORT=8080
fi fi
else
read -p "🚀 Do you want to run the container now on port $PORT? (y/n): " -n 1 -r echo "✅ Port 8080 is available"
echo "" PORT=8080
if [[ $REPLY =~ ^[Yy]$ ]]; then fi
# Get current branch name for container naming
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD | tr '/' '-') read -p "🚀 Do you want to run the container now on port $PORT? (y/n): " -n 1 -r
CONTAINER_NAME="dance-lessons-coach-$BRANCH_NAME" echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "🐳 Preparing container '$CONTAINER_NAME' on port $PORT..." # Get current branch name for container naming
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD | tr '/' '-')
# Remove existing container if it exists CONTAINER_NAME="dance-lessons-coach-$BRANCH_NAME"
if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME$"; then
echo "⚠️ Container '$CONTAINER_NAME' already exists - removing it..." echo "🐳 Preparing container '$CONTAINER_NAME' on port $PORT..."
docker stop "$CONTAINER_NAME" > /dev/null 2>&1 || true
docker rm "$CONTAINER_NAME" > /dev/null 2>&1 || true # Remove existing container if it exists
echo "✅ Old container removed" if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME$"; then
fi echo "⚠️ Container '$CONTAINER_NAME' already exists - removing it..."
docker stop "$CONTAINER_NAME" > /dev/null 2>&1 || true
# Also remove the generic test container if it exists docker rm "$CONTAINER_NAME" > /dev/null 2>&1 || true
if docker ps -a --format '{{.Names}}' | grep -q "^dance-lessons-coach-test$"; then echo "✅ Old container removed"
echo "⚠️ Generic test container exists - removing it..." fi
docker stop dance-lessons-coach-test > /dev/null 2>&1 || true
docker rm dance-lessons-coach-test > /dev/null 2>&1 || true # Also remove the generic test container if it exists
echo "✅ Old generic container removed" if docker ps -a --format '{{.Names}}' | grep -q "^dance-lessons-coach-test$"; then
fi echo "⚠️ Generic test container exists - removing it..."
docker stop dance-lessons-coach-test > /dev/null 2>&1 || true
echo "🐳 Starting container '$CONTAINER_NAME' on port $PORT..." docker rm dance-lessons-coach-test > /dev/null 2>&1 || true
docker run -d -p $PORT:8080 --name "$CONTAINER_NAME" "$CONTAINER_IMAGE" echo "✅ Old generic container removed"
echo "✅ Container '$CONTAINER_NAME' started on port $PORT" fi
echo ""
echo "🐳 Starting container '$CONTAINER_NAME' on port $PORT..."
# Wait for container to be ready docker run -d -p $PORT:8080 --name "$CONTAINER_NAME" "$CONTAINER_IMAGE"
echo "🕒 Waiting for container to be ready..." echo "✅ Container '$CONTAINER_NAME' started on port $PORT"
MAX_ATTEMPTS=10 echo ""
ATTEMPT=1
READY=false # Wait for container to be ready
echo "🕒 Waiting for container to be ready..."
while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do MAX_ATTEMPTS=10
if curl -s http://localhost:$PORT/api/health | grep -q "healthy"; then ATTEMPT=1
READY=true READY=false
break
fi while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do
sleep 1 if curl -s http://localhost:$PORT/api/health | grep -q "healthy"; then
ATTEMPT=$((ATTEMPT + 1)) READY=true
echo "🕒 Attempt $ATTEMPT/$MAX_ATTEMPTS..." break
done fi
sleep 1
if [ "$READY" = true ]; then ATTEMPT=$((ATTEMPT + 1))
echo "✅ Container is ready!" echo "🕒 Attempt $ATTEMPT/$MAX_ATTEMPTS..."
else done
echo "❌ Container failed to start properly"
echo "📋 Container logs:" if [ "$READY" = true ]; then
docker logs dance-lessons-coach-test echo "✅ Container is ready!"
echo "" else
echo "💡 Check container status with: docker ps -a" echo " Container failed to start properly"
echo "💡 View full logs with: docker logs dance-lessons-coach-test" echo "📋 Container logs:"
continue # Skip endpoint testing docker logs dance-lessons-coach-test
fi echo ""
echo "💡 Check container status with: docker ps -a"
echo "📋 Testing endpoints..." echo "💡 View full logs with: docker logs dance-lessons-coach-test"
continue # Skip endpoint testing
if curl -s http://localhost:$PORT/api/health | grep -q "healthy"; then fi
echo "✅ Health check passed"
else echo "📋 Testing endpoints..."
echo "❌ Health check failed"
fi if curl -s http://localhost:$PORT/api/health | grep -q "healthy"; then
echo "✅ Health check passed"
if curl -s http://localhost:$PORT/api/v1/greet/ | grep -q "Hello"; then else
echo "✅ Greet endpoint working" echo "❌ Health check failed"
else fi
echo "❌ Greet endpoint failed"
fi if curl -s http://localhost:$PORT/api/v1/greet/ | grep -q "Hello"; then
echo "✅ Greet endpoint working"
echo "" else
echo "📖 Swagger UI available at: http://localhost:$PORT/swagger/" echo "❌ Greet endpoint failed"
echo "💡 Press Ctrl+C to stop the container when done" fi
echo " Or run: docker stop $CONTAINER_NAME && docker rm $CONTAINER_NAME"
fi echo ""
echo "📖 Swagger UI available at: http://localhost:$PORT/swagger/"
echo "💡 Press Ctrl+C to stop the container when done"
echo " Or run: docker stop $CONTAINER_NAME && docker rm $CONTAINER_NAME"
fi fi
else
echo " Docker not available - skipping Docker build instructions"
fi fi
echo "" echo ""
@@ -374,9 +352,7 @@ echo " ✅ Code compilation"
echo " ✅ Unit tests with coverage" echo " ✅ Unit tests with coverage"
echo " ✅ Binary build" echo " ✅ Binary build"
echo " ✅ Version bump simulation" echo " ✅ Version bump simulation"
if [ "$HAS_DOCKER" = true ]; then echo " ✅ Docker build (development and/or production if chosen)"
echo " ✅ Docker build (development and/or production if chosen)"
fi
echo "" echo ""
echo "🎯 When ready for production:" echo "🎯 When ready for production:"
echo " Push to main branch to trigger full CI/CD pipeline" echo " Push to main branch to trigger full CI/CD pipeline"