From 546b0e058c87088c1c4a1b2984f77db15d41ce03 Mon Sep 17 00:00:00 2001 From: continuist Date: Sat, 5 Jul 2025 12:12:25 -0400 Subject: [PATCH] Updated to use correct Forgejo env variables and do checkout in DinD container --- .forgejo/workflows/ci.yml | 44 ++++++------- CI_CD_PIPELINE_SETUP_GUIDE.md | 4 +- scripts/test_dind_setup.sh | 113 ++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 23 deletions(-) create mode 100755 scripts/test_dind_setup.sh diff --git a/.forgejo/workflows/ci.yml b/.forgejo/workflows/ci.yml index 2e7ef89..816f0c9 100644 --- a/.forgejo/workflows/ci.yml +++ b/.forgejo/workflows/ci.yml @@ -15,12 +15,9 @@ jobs: test: name: Run Tests (DinD) runs-on: ci - if: github.ref == 'refs/heads/main' + if: env.GITEA_REF == 'refs/heads/main' steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Setup DinD Environment run: | # Check if DinD container already exists and is running @@ -53,13 +50,13 @@ jobs: docker exec ci-dind docker version fi - - name: Setup Containerized Testing Environment + - name: Checkout code to DinD container run: | - # Copy docker-compose.test.yml to DinD container - docker cp docker-compose.test.yml ci-dind:/workspace/ - docker cp backend ci-dind:/workspace/ - docker cp frontend ci-dind:/workspace/ - docker cp scripts ci-dind:/workspace/ + # Checkout code directly into DinD container using the Forgejo repository that triggered the build + docker exec ci-dind sh -c "cd /workspace && git clone ${{ env.GITEA_SERVER_URL }}/${{ env.GITEA_REPOSITORY }}.git . || (cd /workspace && git fetch && git reset --hard origin/${{ env.GITEA_REF }})" + + # Copy docker-compose.test.yml to DinD container (in case it's not in the repo) + docker cp docker-compose.test.yml ci-dind:/workspace/ || true # Start testing environment using dedicated compose file inside DinD docker exec ci-dind docker compose -f /workspace/docker-compose.test.yml up -d @@ -123,41 +120,44 @@ jobs: name: Build and Push Docker Images (DinD) needs: [test] runs-on: ci - if: github.ref == 'refs/heads/main' + if: env.GITEA_REF == 'refs/heads/main' steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Set up Docker Buildx in DinD run: | # Set up Docker Buildx inside the existing DinD container docker exec ci-dind docker buildx create --use --name ci-builder || true docker exec ci-dind docker buildx inspect --bootstrap + + # Ensure code is available in DinD (reuse from test job) + docker exec ci-dind sh -c "cd /workspace && git fetch && git reset --hard origin/${{ env.GITEA_REF }}" + + # Verify we have the correct repository + docker exec ci-dind sh -c "cd /workspace && git remote -v" - name: Build and push backend image run: | # Build and push backend image using DinD docker exec ci-dind docker buildx build \ --platform linux/amd64 \ - --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/backend:${{ github.sha }} \ + --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/backend:${{ env.GITEA_SHA }} \ --push \ --cache-from type=gha \ --cache-to type=gha,mode=max \ - -f ./backend/Dockerfile \ - ./backend + -f /workspace/backend/Dockerfile \ + /workspace/backend - name: Build and push frontend image run: | # Build and push frontend image using DinD docker exec ci-dind docker buildx build \ --platform linux/amd64 \ - --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/frontend:${{ github.sha }} \ + --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/frontend:${{ env.GITEA_SHA }} \ --push \ --cache-from type=gha \ --cache-to type=gha,mode=max \ - -f ./frontend/Dockerfile \ - ./frontend + -f /workspace/frontend/Dockerfile \ + /workspace/frontend - name: Cleanup Testing Environment if: always() @@ -183,7 +183,7 @@ jobs: name: Deploy to Production needs: build-and-push runs-on: prod - if: github.ref == 'refs/heads/main' + if: env.GITEA_REF == 'refs/heads/main' steps: - name: Setup deployment directory @@ -209,7 +209,7 @@ jobs: - name: Create environment file for deployment run: | # Create environment file for this deployment - echo "IMAGE_TAG=${{ github.sha }}" > .env + echo "IMAGE_TAG=${{ env.GITEA_SHA }}" > .env echo "REGISTRY=${{ secrets.CI_HOST }}:443" >> .env echo "IMAGE_NAME=${{ secrets.APP_NAME || 'sharenet' }}" >> .env echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD || 'your_secure_password_here' }}" >> .env diff --git a/CI_CD_PIPELINE_SETUP_GUIDE.md b/CI_CD_PIPELINE_SETUP_GUIDE.md index ae1fbf0..8697198 100644 --- a/CI_CD_PIPELINE_SETUP_GUIDE.md +++ b/CI_CD_PIPELINE_SETUP_GUIDE.md @@ -1053,17 +1053,19 @@ The CI/CD pipeline uses a three-stage approach with dedicated environments for e **Job 1 (Testing) - `docker-compose.test.yml`:** - **Purpose**: Comprehensive testing with multiple containers - **Environment**: DinD with PostgreSQL, Rust, and Node.js containers +- **Code Checkout**: Code is checked out directly into the DinD container at `/workspace` from the Forgejo repository that triggered the build - **Services**: - PostgreSQL database for backend tests - Rust toolchain for backend testing and migrations - Node.js toolchain for frontend testing - **Network**: All containers communicate through `ci-cd-test-network` -- **Setup**: DinD container created, Harbor certificate installed, Docker login performed +- **Setup**: DinD container created, Harbor certificate installed, Docker login performed, code cloned into DinD from Forgejo - **Cleanup**: Testing containers removed, DinD container kept running **Job 2 (Building) - Direct Docker Commands:** - **Purpose**: Image building and pushing to Harbor - **Environment**: Same DinD container from Job 1 +- **Code Access**: Reuses code from Job 1, updates to latest commit - **Process**: - Uses Docker Buildx for efficient building - Builds backend and frontend images separately diff --git a/scripts/test_dind_setup.sh b/scripts/test_dind_setup.sh new file mode 100755 index 0000000..ea7e89e --- /dev/null +++ b/scripts/test_dind_setup.sh @@ -0,0 +1,113 @@ +#!/bin/bash + +# Test DinD Setup Script +# This script verifies that the DinD container is properly configured +# and can perform the operations needed by the CI workflow + +set -e + +echo "๐Ÿงช Testing DinD Setup..." + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Function to print colored output +print_status() { + local status=$1 + local message=$2 + if [ "$status" = "PASS" ]; then + echo -e "${GREEN}โœ… $message${NC}" + elif [ "$status" = "FAIL" ]; then + echo -e "${RED}โŒ $message${NC}" + else + echo -e "${YELLOW}โš ๏ธ $message${NC}" + fi +} + +# Test 1: Check if DinD container exists and is running +echo "1. Checking DinD container status..." +if docker ps --format "table {{.Names}}" | grep -q "^ci-dind$"; then + print_status "PASS" "DinD container is running" +else + print_status "FAIL" "DinD container is not running" + echo "Starting DinD container..." + docker run -d \ + --name ci-dind \ + --privileged \ + -p 2375:2375 \ + -e DOCKER_TLS_CERTDIR="" \ + docker:dind + + # Wait for DinD to be ready + echo "Waiting for DinD to be ready..." + timeout 60 bash -c 'until docker exec ci-dind docker version; do sleep 2; done' + print_status "PASS" "DinD container started successfully" +fi + +# Test 2: Check Docker functionality inside DinD +echo "2. Testing Docker functionality inside DinD..." +if docker exec ci-dind docker version > /dev/null 2>&1; then + print_status "PASS" "Docker is working inside DinD" +else + print_status "FAIL" "Docker is not working inside DinD" + exit 1 +fi + +# Test 3: Check if Harbor certificate is installed +echo "3. Checking Harbor certificate installation..." +if docker exec ci-dind test -f /usr/local/share/ca-certificates/registry.crt; then + print_status "PASS" "Harbor certificate is installed" +else + print_status "WARN" "Harbor certificate not found - will be installed during CI run" +fi + +# Test 4: Test git functionality inside DinD +echo "4. Testing git functionality inside DinD..." +if docker exec ci-dind git --version > /dev/null 2>&1; then + print_status "PASS" "Git is available inside DinD" +else + print_status "FAIL" "Git is not available inside DinD" + echo "Installing git in DinD..." + docker exec ci-dind apk add --no-cache git + print_status "PASS" "Git installed successfully" +fi + +# Test 5: Test workspace directory +echo "5. Testing workspace directory..." +if docker exec ci-dind test -d /workspace; then + print_status "PASS" "Workspace directory exists" +else + print_status "WARN" "Workspace directory does not exist - will be created during CI run" +fi + +# Test 6: Test basic Docker operations +echo "6. Testing basic Docker operations..." +if docker exec ci-dind docker run --rm alpine:latest echo "test" > /dev/null 2>&1; then + print_status "PASS" "Basic Docker operations work" +else + print_status "FAIL" "Basic Docker operations failed" + exit 1 +fi + +# Test 7: Test Docker Compose +echo "7. Testing Docker Compose..." +if docker exec ci-dind docker compose version > /dev/null 2>&1; then + print_status "PASS" "Docker Compose is available" +else + print_status "FAIL" "Docker Compose is not available" + exit 1 +fi + +echo "" +echo "๐ŸŽ‰ DinD Setup Test Complete!" +echo "" +echo "If all tests passed, your DinD environment is ready for CI/CD operations." +echo "The CI workflow will:" +echo " 1. Checkout code directly into the DinD container from your Forgejo repository" +echo " 2. Run tests in isolated containers" +echo " 3. Build and push images to Harbor" +echo "" +echo "To run the CI workflow, push changes to your main branch." \ No newline at end of file