diff --git a/.forgejo/workflows/ci.yml b/.forgejo/workflows/ci.yml index 22b2a32..689f45e 100644 --- a/.forgejo/workflows/ci.yml +++ b/.forgejo/workflows/ci.yml @@ -78,11 +78,68 @@ jobs: docker exec ci-dind rm -rf /workspace/* /workspace/.* 2>/dev/null || true docker cp /tmp/ci-workspace/. ci-dind:/workspace/ + - name: Check and prepare Harbor base images + run: | + # Set environment variables + export CI_HOST="${{ secrets.CI_HOST }}" + export APP_NAME="${{ secrets.APP_NAME || 'sharenet' }}" + export HARBOR_CI_USER="${{ secrets.HARBOR_CI_USER }}" + export HARBOR_CI_PASSWORD="${{ secrets.HARBOR_CI_PASSWORD }}" + + # Login to Harbor + echo "Logging into Harbor registry..." + echo "$HARBOR_CI_PASSWORD" | docker exec -i ci-dind docker login "$CI_HOST:443" -u "$HARBOR_CI_USER" --password-stdin + + # Check if base images exist in Harbor, pull from Docker Hub if not + BASE_IMAGES=("rust:1.75-slim" "node:20-slim" "postgres:15-alpine") + + for image in "${BASE_IMAGES[@]}"; do + image_name=$(echo "$image" | cut -d: -f1) + image_tag=$(echo "$image" | cut -d: -f2) + harbor_image="$CI_HOST:443/$APP_NAME/$image_name:$image_tag" + + echo "Checking if $harbor_image exists in Harbor..." + + # Try to pull from Harbor first + if docker exec ci-dind docker pull "$harbor_image" 2>/dev/null; then + echo "✓ Found $harbor_image in Harbor" + else + echo "✗ $harbor_image not found in Harbor, pulling from Docker Hub..." + + # Pull from Docker Hub + if docker exec ci-dind docker pull "$image"; then + echo "✓ Successfully pulled $image from Docker Hub" + + # Tag for Harbor + docker exec ci-dind docker tag "$image" "$harbor_image" + + # Push to Harbor + if docker exec ci-dind docker push "$harbor_image"; then + echo "✓ Successfully pushed $harbor_image to Harbor" + else + echo "✗ Failed to push $harbor_image to Harbor" + exit 1 + fi + else + echo "✗ Failed to pull $image from Docker Hub" + exit 1 + fi + fi + done + + echo "All base images are ready in Harbor!" + - name: Start testing environment run: | # Start testing environment using dedicated compose file inside DinD echo "Starting testing environment..." - docker exec ci-dind docker compose -f /workspace/docker-compose.test.yml up -d --build + + # Set environment variables for docker-compose + export CI_HOST="${{ secrets.CI_HOST }}" + export APP_NAME="${{ secrets.APP_NAME || 'sharenet' }}" + + # Start the testing environment with build args + docker exec ci-dind sh -c "export CI_HOST='$CI_HOST' && export APP_NAME='$APP_NAME' && docker compose -f /workspace/docker-compose.test.yml up -d --build" # Wait for all services to be ready with better error handling echo "Waiting for testing environment to be ready..." diff --git a/CI_CD_PIPELINE_SETUP_GUIDE.md b/CI_CD_PIPELINE_SETUP_GUIDE.md index 7642698..d7b4d24 100644 --- a/CI_CD_PIPELINE_SETUP_GUIDE.md +++ b/CI_CD_PIPELINE_SETUP_GUIDE.md @@ -1741,7 +1741,7 @@ Go to your Forgejo repository and add these secrets in **Settings → Secrets an - `DOMAIN`: Your domain name (e.g., `example.com`) - `EMAIL`: Your email for SSL certificate notifications -**Note**: This setup uses custom Dockerfiles for testing environments instead of pulling from external registries, eliminating Docker Hub rate limiting issues and providing better control over the testing environment. +**Note**: This setup uses custom Dockerfiles for testing environments with base images stored in Harbor registry. The CI pipeline automatically checks if base images exist in Harbor and pulls them from Docker Hub only when needed, eliminating rate limiting issues and providing better control over the testing environment. ### Step 18: Test Complete Pipeline diff --git a/backend/Dockerfile.test-postgres b/backend/Dockerfile.test-postgres index bdfde94..0bf1912 100644 --- a/backend/Dockerfile.test-postgres +++ b/backend/Dockerfile.test-postgres @@ -1,5 +1,7 @@ # PostgreSQL testing environment for CI/CD -FROM postgres:15-alpine +ARG CI_HOST=localhost +ARG APP_NAME=sharenet +FROM ${CI_HOST}:443/${APP_NAME}/postgres:15-alpine # Install additional tools if needed RUN apk add --no-cache curl diff --git a/backend/Dockerfile.test-rust b/backend/Dockerfile.test-rust index fb0746b..d6cc901 100644 --- a/backend/Dockerfile.test-rust +++ b/backend/Dockerfile.test-rust @@ -1,5 +1,7 @@ # Rust testing environment for CI/CD -FROM rust:1.75-slim +ARG CI_HOST=localhost +ARG APP_NAME=sharenet +FROM ${CI_HOST}:443/${APP_NAME}/rust:1.75-slim # Install additional tools needed for testing RUN apt-get update && apt-get install -y \ diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 77df40c..7b357a2 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -6,6 +6,9 @@ services: build: context: ./backend dockerfile: Dockerfile.test-postgres + args: + CI_HOST: ${CI_HOST:-localhost} + APP_NAME: ${APP_NAME:-sharenet} container_name: ci-cd-test-postgres restart: unless-stopped environment: @@ -27,6 +30,9 @@ services: build: context: ./backend dockerfile: Dockerfile.test-rust + args: + CI_HOST: ${CI_HOST:-localhost} + APP_NAME: ${APP_NAME:-sharenet} container_name: ci-cd-test-rust restart: unless-stopped volumes: @@ -44,6 +50,9 @@ services: build: context: ./frontend dockerfile: Dockerfile.test-node + args: + CI_HOST: ${CI_HOST:-localhost} + APP_NAME: ${APP_NAME:-sharenet} container_name: ci-cd-test-node restart: unless-stopped volumes: diff --git a/frontend/Dockerfile.test-node b/frontend/Dockerfile.test-node index 3e4675c..f0c868a 100644 --- a/frontend/Dockerfile.test-node +++ b/frontend/Dockerfile.test-node @@ -1,5 +1,7 @@ # Node.js testing environment for CI/CD -FROM node:20-slim +ARG CI_HOST=localhost +ARG APP_NAME=sharenet +FROM ${CI_HOST}:443/${APP_NAME}/node:20-slim # Install additional tools needed for testing RUN apt-get update && apt-get install -y \