From da0dc0e30f139af21a9ee9a09f8a2201077fe150 Mon Sep 17 00:00:00 2001 From: Gabriel Radureau Date: Tue, 7 Apr 2026 23:20:52 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20chore:=20align=20local=20CI/CD?= =?UTF-8?q?=20script=20with=20Gitea=20workflow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add dependency hash calculation matching CI workflow - Add Docker cache detection and usage - Add PostgreSQL service with Docker Compose - Execute commands in Docker Compose containers when cache available - Update test summary to reflect new workflow matching Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- scripts/test-local-ci-cd.sh | 139 ++++++++++++++++++++++++++++++------ 1 file changed, 117 insertions(+), 22 deletions(-) diff --git a/scripts/test-local-ci-cd.sh b/scripts/test-local-ci-cd.sh index 78c2bd3..5a4af3e 100755 --- a/scripts/test-local-ci-cd.sh +++ b/scripts/test-local-ci-cd.sh @@ -21,48 +21,139 @@ if ! command -v docker >/dev/null 2>&1; then HAS_DOCKER=false else HAS_DOCKER=true + + # Check for docker compose plugin + if ! docker compose version >/dev/null 2>&1; then + echo "⚠️ Docker Compose plugin not found. Installing..." + sudo apt-get update && sudo apt-get install -y docker-compose-plugin + fi fi echo "✅ Environment ready" echo "" -# 2. Install dependencies -echo "2. Installing dependencies..." -go mod tidy -echo "✅ Dependencies installed" +# 2. Calculate dependency hash (match CI workflow) +echo "2. Calculating dependency hash..." +DEPS_HASH=$(sha256sum go.mod go.sum | sha256sum | cut -d' ' -f1 | head -c 12) +echo "Dependency hash: $DEPS_HASH" +echo "✅ Dependency hash calculated" echo "" -# 3. Install swag and generate docs -echo "3. Generating Swagger documentation..." -if [ ! -f pkg/server/docs/swagger.json ]; then - echo "📝 Generating Swagger docs..." - go install github.com/swaggo/swag/cmd/swag@latest - cd pkg/server && go generate - cd ../.. - echo "✅ Swagger documentation generated" +# 3. Check for Docker 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" + + # Try to pull the cache image + if docker pull "$IMAGE_NAME" >/dev/null 2>&1; then + echo "✅ Cache hit - using existing build cache" + USE_DOCKER_CACHE=true + else + echo "⚠️ Cache miss - will build without cache" + USE_DOCKER_CACHE=false + fi else - echo "✅ Swagger documentation already exists" + echo "⚠️ Docker not available - running natively" + USE_DOCKER_CACHE=false fi echo "" -# 4. Build and test -echo "4. Building and testing..." -go build ./... +# 4. Start PostgreSQL with Docker Compose +echo "4. Starting PostgreSQL..." +if [ "$HAS_DOCKER" = true ]; then + docker compose -f docker-compose.yml up -d postgres + + # Wait for PostgreSQL to be ready + echo "Waiting for PostgreSQL to be ready..." + for i in {1..30}; do + if docker exec dance-lessons-coach-postgres pg_isready -U postgres; then + echo "✅ PostgreSQL is ready!" + break + fi + echo "Waiting for PostgreSQL... ($i/30)" + sleep 2 + done + + # Set PostgreSQL environment variables + export PGHOST="dance-lessons-coach-postgres" + export PGPORT=5432 + export PGUSER=postgres + export PGPASSWORD=postgres + export PGDATABASE=dance_lessons_coach_bdd_test +else + echo "⚠️ Docker not available - PostgreSQL tests will be skipped" +fi +echo "" + +# 5. Install dependencies +echo "5. Installing dependencies..." +if [ "$USE_DOCKER_CACHE" = true ]; then + echo "Running in Docker Compose container..." + docker compose -f docker-compose.build.yml exec -w /workspace build-cache sh -c "go mod tidy" +else + echo "Running natively..." + go mod tidy +fi +echo "✅ Dependencies installed" +echo "" + +# 6. Generate Swagger Docs +echo "6. Generating Swagger documentation..." +if [ "$USE_DOCKER_CACHE" = true ]; then + echo "Running in Docker Compose container..." + docker compose -f docker-compose.build.yml exec -w /workspace/pkg/server build-cache sh -c "go generate" +else + echo "Running natively..." + cd pkg/server && go generate + cd ../.. +fi +echo "✅ Swagger documentation generated" +echo "" + +# 7. Build and test +echo "7. Building and testing..." +if [ "$USE_DOCKER_CACHE" = true ]; then + echo "Running in Docker Compose container..." + docker compose -f docker-compose.build.yml exec -w /workspace build-cache sh -c "go build ./..." +else + echo "Running natively..." + go build ./... +fi echo "✅ Code compiled successfully" -go test ./... -cover -v +if [ "$USE_DOCKER_CACHE" = true ]; then + echo "Running in Docker Compose container with PostgreSQL..." + docker compose -f docker-compose.build.yml exec \ + -e PGHOST=dance-lessons-coach-postgres \ + -e PGPORT=5432 \ + -e PGUSER=postgres \ + -e PGPASSWORD=postgres \ + -e PGDATABASE=dance_lessons_coach_bdd_test \ + -w /workspace \ + build-cache \ + sh -c "go test ./... -coverprofile=coverage.out -v && go tool cover -func=coverage.out > coverage.txt" +else + echo "Running natively..." + go test ./... -cover -v +fi echo "✅ Tests passed" echo "" -# 5. Build binaries -echo "5. Building binaries..." -./scripts/build.sh +# 8. Build binaries +echo "8. Building binaries..." +if [ "$USE_DOCKER_CACHE" = true ]; then + echo "Running in Docker Compose container..." + docker compose -f docker-compose.build.yml exec -w /workspace build-cache sh -c "./scripts/build.sh" +else + echo "Running natively..." + ./scripts/build.sh +fi echo "✅ Binaries built" ls -la bin/ echo "" -# 6. Version bump simulation -echo "6. Version bump simulation..." +# 9. Version bump simulation +echo "9. Version bump simulation..." LAST_COMMIT=$(git log -1 --pretty=%B | head -1) echo "Last commit: $LAST_COMMIT" @@ -260,6 +351,9 @@ echo "✅ LOCAL CI/CD TEST COMPLETE" echo "===========================" echo "" echo "📋 What was tested:" +echo " ✅ Dependency hash calculation (matching CI workflow)" +echo " ✅ Docker cache detection and usage" +echo " ✅ PostgreSQL service with Docker Compose" echo " ✅ Go dependencies installation" echo " ✅ Swagger documentation generation" echo " ✅ Code compilation" @@ -275,5 +369,6 @@ echo " Push to main branch to trigger full CI/CD pipeline" echo " Docker image will be built and pushed to Gitea Container Registry" echo "" echo "💡 Local testing complete! Your changes are ready for CI/CD." +echo "💡 This script now matches the Gitea workflow structure and behavior." # ⚠️ IMPORTANT: Local Dockerfile.prod uses 'latest' tag for testing only # ✅ CI/CD workflow generates correct Dockerfile.prod with dependency hash