Updated to use correct Forgejo env variables and do checkout in DinD container
All checks were successful
All checks were successful
This commit is contained in:
parent
cfaac6b308
commit
546b0e058c
3 changed files with 138 additions and 23 deletions
|
@ -15,12 +15,9 @@ jobs:
|
||||||
test:
|
test:
|
||||||
name: Run Tests (DinD)
|
name: Run Tests (DinD)
|
||||||
runs-on: ci
|
runs-on: ci
|
||||||
if: github.ref == 'refs/heads/main'
|
if: env.GITEA_REF == 'refs/heads/main'
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Setup DinD Environment
|
- name: Setup DinD Environment
|
||||||
run: |
|
run: |
|
||||||
# Check if DinD container already exists and is running
|
# Check if DinD container already exists and is running
|
||||||
|
@ -53,13 +50,13 @@ jobs:
|
||||||
docker exec ci-dind docker version
|
docker exec ci-dind docker version
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Setup Containerized Testing Environment
|
- name: Checkout code to DinD container
|
||||||
run: |
|
run: |
|
||||||
# Copy docker-compose.test.yml to DinD container
|
# Checkout code directly into DinD container using the Forgejo repository that triggered the build
|
||||||
docker cp docker-compose.test.yml ci-dind:/workspace/
|
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 }})"
|
||||||
docker cp backend ci-dind:/workspace/
|
|
||||||
docker cp frontend ci-dind:/workspace/
|
# Copy docker-compose.test.yml to DinD container (in case it's not in the repo)
|
||||||
docker cp scripts ci-dind:/workspace/
|
docker cp docker-compose.test.yml ci-dind:/workspace/ || true
|
||||||
|
|
||||||
# Start testing environment using dedicated compose file inside DinD
|
# Start testing environment using dedicated compose file inside DinD
|
||||||
docker exec ci-dind docker compose -f /workspace/docker-compose.test.yml up -d
|
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)
|
name: Build and Push Docker Images (DinD)
|
||||||
needs: [test]
|
needs: [test]
|
||||||
runs-on: ci
|
runs-on: ci
|
||||||
if: github.ref == 'refs/heads/main'
|
if: env.GITEA_REF == 'refs/heads/main'
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx in DinD
|
- name: Set up Docker Buildx in DinD
|
||||||
run: |
|
run: |
|
||||||
# Set up Docker Buildx inside the existing DinD container
|
# 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 create --use --name ci-builder || true
|
||||||
docker exec ci-dind docker buildx inspect --bootstrap
|
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
|
- name: Build and push backend image
|
||||||
run: |
|
run: |
|
||||||
# Build and push backend image using DinD
|
# Build and push backend image using DinD
|
||||||
docker exec ci-dind docker buildx build \
|
docker exec ci-dind docker buildx build \
|
||||||
--platform linux/amd64 \
|
--platform linux/amd64 \
|
||||||
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/backend:${{ github.sha }} \
|
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/backend:${{ env.GITEA_SHA }} \
|
||||||
--push \
|
--push \
|
||||||
--cache-from type=gha \
|
--cache-from type=gha \
|
||||||
--cache-to type=gha,mode=max \
|
--cache-to type=gha,mode=max \
|
||||||
-f ./backend/Dockerfile \
|
-f /workspace/backend/Dockerfile \
|
||||||
./backend
|
/workspace/backend
|
||||||
|
|
||||||
- name: Build and push frontend image
|
- name: Build and push frontend image
|
||||||
run: |
|
run: |
|
||||||
# Build and push frontend image using DinD
|
# Build and push frontend image using DinD
|
||||||
docker exec ci-dind docker buildx build \
|
docker exec ci-dind docker buildx build \
|
||||||
--platform linux/amd64 \
|
--platform linux/amd64 \
|
||||||
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/frontend:${{ github.sha }} \
|
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/frontend:${{ env.GITEA_SHA }} \
|
||||||
--push \
|
--push \
|
||||||
--cache-from type=gha \
|
--cache-from type=gha \
|
||||||
--cache-to type=gha,mode=max \
|
--cache-to type=gha,mode=max \
|
||||||
-f ./frontend/Dockerfile \
|
-f /workspace/frontend/Dockerfile \
|
||||||
./frontend
|
/workspace/frontend
|
||||||
|
|
||||||
- name: Cleanup Testing Environment
|
- name: Cleanup Testing Environment
|
||||||
if: always()
|
if: always()
|
||||||
|
@ -183,7 +183,7 @@ jobs:
|
||||||
name: Deploy to Production
|
name: Deploy to Production
|
||||||
needs: build-and-push
|
needs: build-and-push
|
||||||
runs-on: prod
|
runs-on: prod
|
||||||
if: github.ref == 'refs/heads/main'
|
if: env.GITEA_REF == 'refs/heads/main'
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Setup deployment directory
|
- name: Setup deployment directory
|
||||||
|
@ -209,7 +209,7 @@ jobs:
|
||||||
- name: Create environment file for deployment
|
- name: Create environment file for deployment
|
||||||
run: |
|
run: |
|
||||||
# Create environment file for this deployment
|
# 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 "REGISTRY=${{ secrets.CI_HOST }}:443" >> .env
|
||||||
echo "IMAGE_NAME=${{ secrets.APP_NAME || 'sharenet' }}" >> .env
|
echo "IMAGE_NAME=${{ secrets.APP_NAME || 'sharenet' }}" >> .env
|
||||||
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD || 'your_secure_password_here' }}" >> .env
|
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD || 'your_secure_password_here' }}" >> .env
|
||||||
|
|
|
@ -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`:**
|
**Job 1 (Testing) - `docker-compose.test.yml`:**
|
||||||
- **Purpose**: Comprehensive testing with multiple containers
|
- **Purpose**: Comprehensive testing with multiple containers
|
||||||
- **Environment**: DinD with PostgreSQL, Rust, and Node.js 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**:
|
- **Services**:
|
||||||
- PostgreSQL database for backend tests
|
- PostgreSQL database for backend tests
|
||||||
- Rust toolchain for backend testing and migrations
|
- Rust toolchain for backend testing and migrations
|
||||||
- Node.js toolchain for frontend testing
|
- Node.js toolchain for frontend testing
|
||||||
- **Network**: All containers communicate through `ci-cd-test-network`
|
- **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
|
- **Cleanup**: Testing containers removed, DinD container kept running
|
||||||
|
|
||||||
**Job 2 (Building) - Direct Docker Commands:**
|
**Job 2 (Building) - Direct Docker Commands:**
|
||||||
- **Purpose**: Image building and pushing to Harbor
|
- **Purpose**: Image building and pushing to Harbor
|
||||||
- **Environment**: Same DinD container from Job 1
|
- **Environment**: Same DinD container from Job 1
|
||||||
|
- **Code Access**: Reuses code from Job 1, updates to latest commit
|
||||||
- **Process**:
|
- **Process**:
|
||||||
- Uses Docker Buildx for efficient building
|
- Uses Docker Buildx for efficient building
|
||||||
- Builds backend and frontend images separately
|
- Builds backend and frontend images separately
|
||||||
|
|
113
scripts/test_dind_setup.sh
Executable file
113
scripts/test_dind_setup.sh
Executable file
|
@ -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."
|
Loading…
Add table
Reference in a new issue